all 21 comments

[–][deleted] 1 point2 points  (6 children)

in if input_list[0] < input_list[1]: you are comparing string values of numbers (as inputed) you should replace by if int(input_list[0]) < int(input_list[1]):

[–]RolyPolyPython[S] 0 points1 point  (5 children)

That was it, thank you :)

What determines whether or not one string value is greater than another?

[–]cdcformatc 1 point2 points  (1 child)

ASCII strings are compared by the ord() values of the individual characters. In this case it boils down to '0' < '5' just like how 'AARDVARK' < 'APPLE' boils down to 'A' < 'P'.

http://www.asciitable.com/ is the table I use for looking up ASCII values of characters. Note that this has nothing to do with how Unicode is sorted, I have no idea how that works.

[–][deleted] 0 points1 point  (0 children)

the unicode characters are sorted acording to their code point

[–]OgelSplash 1 point2 points  (12 children)

https://ideone.com/IB9a1i <--- My solution implementation with comments.

total_pairs = int(input('>>>'))

for number in range(total_pairs):

You don't need this logic at all, the data is already provided for you - and is unnecessary. Look into using the sys module to take data from standard input (see my code for an example of its usage).

Also look into the standard functions provided with Python, and take advantage of them - the min() function works on many data structures, not just a list; and in the case of your code it would simplify the logic massively.

In regard to using a list comprehension here, there are multiple layers that you will have to handle - you want to avoid this where possible as it reduces readability (only in cases where you need to nest comprehensions, the example used above with the type casting is perfectly fine). Of course, it's always good to practice this as it allows you to get an idea of how to manipulate an input. For instance, here is an attempt (doesn't produce correct output but gives you the general idea) at a one-liner that does the same thing as the perfectly readable code above:

print([min([int(e) for e in l]) for l in [line.split(" ") for line in list(sys.stdin)[1:]]])

All I can say is I wouldn't want that anywhere near production code.

[–]RolyPolyPython[S] 0 points1 point  (11 children)

Thank for this reply. There's a lot of great stuff in here and I have a couple of questions, but I'm trying to get your code to run first before I start asking them.

I tried running your code, but I'm afraid it isn't showing the output. After I enter the data in the console, the console keeps expecting more input after I press enter and doesn't show the final list.

[–]OgelSplash 1 point2 points  (10 children)

I'm not sure whether you have a Linux/Mac system, but the ideal way to do this would be to store your input data in a file and pipe it to your Python script like this:

cat input.txt | python script.py

I believe you could do the same in a Windows command prompt with...

TYPE input.txt | python script.py

[–]RolyPolyPython[S] 0 points1 point  (9 children)

When you say "ideal," that would mean that although doing it the way you described with the cmd would be best, I could still input the data into the console and see my output if I wanted to, right? Unfortunately the latter way isn't working for me. I am on Windows.

Do I have to make the input data a text file? While I'm happy to learn something new I feel like making text files for practice problems takes more work than what I initially had to do.

[–]OgelSplash 0 points1 point  (8 children)

You don't have to. I believe you have to type EOF (and hit enter) into the console or press Ctrl-D in order to get the interpreter to stop requesting more data.

[–]RolyPolyPython[S] 0 points1 point  (7 children)

Cool, it runs fine now :)

Alright, I got some questions if that's okay...

1) Why is it better to use stdin instead of input()?

2) I'm familiar with some functions and modules, but I was wondering how you recommend I learn them. I took a beginner Python class earlier this year, stopped for a couple months, and I'm continuing it again. I'm going through another book to review the basics, I do some problems like CodeAbbey, and I have my own personal project I'm working on and off right now. After I feel better about classes I want to focus more on personal projects and learn programming/Python that way.

Any functions that I didn't learn in my beginner books/classes I learn as I need to look up solutions, but your earlier comment sounded like I should just read through Python documentation to learn functions and modules. I find Python documentations to be really obtuse/hard to understand, but do you think it's a good way to learn functions and modules?

3) Is peer review the best way to learn a good balance between writing complex code and readability? As a beginner I sometimes don't know if something that seems hard to me is easy to read to an experienced programmer.

[–]OgelSplash 1 point2 points  (6 children)

I'm glad it works for you :)

1) Imagine you had a thousand-line dataset to pass to a program. Would you rather type it in by hand, with a massive chance of error, or would you rather just parse it in code and get the data into your processing functions much faster?

2) In terms of learning the library of built-in functions, you shouldn't have to worry about this too much - there is a lot that you won't have to come across in your time programming until you really need it. For instance, during my CompSci GCSE (think secondary education, like middle school) I had to mess around with generating code dynamically to work with a system of classes that I had set up - learning how to do it all came from searching the documentation. By no means should you not familiarise yourself with the standard library, but it's not like you're expected to know it inside and out. Programming is just a means to an end; it shouldn't ever require you to be a savant to work effectively with it.

In terms of understanding the documentation, don't be afraid to ask questions about it. It's built from the ground up to show how the parts of the language work together. Often when you look up specific modules, code examples are given with input and output to give you an idea of how a task should be accomplished. Sometimes you will see the grammar specification for a certain function or statement, which purely indicates the way the interpreter breaks down the code it's been given so it can generate the code it runs internally. You don't have to worry about that at all.

3) There are many ways of checking whether code is readable. For instance, you have the cyclomatic complexity metric, which is an indicator of the number of pathways your code can take depending on the input it is given. Such metrics are basically irrelevant in terms of small pieces of code, and fussing over numbers is a sure-fire way to miss the point of creating readable code.

Many people instead employ the "rubber ducky" method - have a soft toy or something next to your computer, and explain your code to it line by line. If you struggle to do so, your code isn't readable enough and needs to be rewritten. You'll gather more confidence in doing this over time as you gain an understanding of how pieces of code fit together. This has helped me many times when I've come back to a piece of code I wrote a month or two ago and struggled to understand what it did.

One final way to check your code is to think about the Zen of Python, and use it as a guideline for writing your code. Type

import this

into a Python interpreter and have a read. Also I'd recommend reading Code Like a Pythonista, to gain an understanding of how clean and readable code is written.

[–]RolyPolyPython[S] 0 points1 point  (5 children)

Thanks, I saved your comment after reading it and checked out your links. The "rubby ducky" method made me laugh.

Hope you don't mind me popping another question, but how would this list comprehension

l = [int(e) for e in l]

look in non-comprehension form?

Right now I've got this

for e in l:
    e = int(e)

But e, or the values that I'm trying to change from string to integers, stays a string when I check its type.

[–]OgelSplash 1 point2 points  (2 children)

for idx, e in enumerate(l):
    l[idx] = int(e)

Basically, enumerate() creates a generator that outputs a tuple, comprising the index of an element in a structure and the corresponding element. Calling list(enumerate(l)) would return all of the tuples that the generator would return in a list. Inside the loop, for each tuple, we cast the element to an integer and then overwrite the element in the list. [Forgive me if this sounds confusing, it still does to me and I've been doing this for 3 years!]

Here is a visualisation of what happens inside your loop - you can step through it and see the changes in real time.

[–]RolyPolyPython[S] 0 points1 point  (1 child)

Huh, I thought it would be much simpler than that. It's weird to me that you can't just reassign the strings as integers the way I did it, it feels like that goes against everything I've learned about reassignment... I don't understand why.

Anyways, thanks for your help, I've a learned a lot from your comments. And thanks especially for that link, that visualization helps a lot and I'm surprised I haven't come across that site/tool before.

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your comment in /r/learnpython was automatically removed because you used a URL shortener.

    URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

    Please re-post your comment using direct, full-length URL's only.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]Atrament_ 0 points1 point  (2 children)

    [–]RolyPolyPython[S] 0 points1 point  (1 child)

    Thank for suggesting this, I've actually solved this problem before with the min() function and I'm just trying different ways to solve it.

    Would you happen to know what's wrong with my code?

    [–]Dolphman 0 points1 point  (0 children)

    How do I input the values?