My Journey into Python from Scratch - Part Two
Join the DZone community and get the full member experience.Join For Free
My first post My Journey into Python from Scratch was a huge hit, I got all this attention and even now, several days later, I still get visits and comments and am being followed via Email and RSS. I have read each comment and each referral to a site which led me to many python sites that came to help and found myself in a veritable heaven for a newbie like me.
I also met this guy from Costa-Rica who is going through the same journey as me and we are exchanging ideas by email. My post was published on some great sites and if you search Google for “Python from Scratch” I am in the Top 5 global results. All of this caused me to lose focus, and on top of it, my young son is still sick, so I have had some sleepless night lately.
Last night I decided that no matter what I would continue on my journey and I have started to read the next page in the Google python class- lists. I read the entire chapter, which explains how lists work in Python, a bit tricky at some points yet easy to follow. Then the chapter moved on to how to use the FOR var IN list (which is different from C++). It took me some time to get used to it, but once you get it- wow! Towards the end, you become familiar with some lists method which will be handy later on and voila: the exercise.
So here I am solving the tests and on the first 2 problems I was doing fine, only to bump into the third problem which took me about an hour. Here it is:
"Given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple." example- (1, 3), (3, 2), (2, 1) should return- (2, 1), (3, 2), (1, 3).
It's easy to see the obstacle here is how to sort using the last element, I really tried to make my code as simple as possible (KISS) but no matter what I did, I was writing more and more rows and taking care of more and more corner cases until I decide that I was doing something wrong and surely there was a faster solution hidden in the learning subject.
So I read the lists page course again until I discovered the problem- Google has a Doc BUG in this page- an important method is mentioned but not explained. It says:
list.sort() -- sorts the list in place (does not return it).
(The sorted() function shown below is preferred.)
But there is no sorted() below and this method was the method to solve the issue easily. In fact list.sort() can also handle this case but I couldn’t understand it from reading over here.
After spending too much time on this and it being so late at night, I went to sleep. The following night, I spent another 30 minutes try and solve the advanced exercise and again I ran into a strange situation: the second exercise was very easy to do and took me only 3 rows to complete, yet Google solution was about 10 rows (doesn't make sense, right?)
So I decide to show it to you and ask you where I am wrong in my solution:
The question is:
"Given two lists sorted in increasing order, create and return a merged list of all the elements in sorted order." example- ['aa', 'xx', 'zz'], ['bb', 'cc']) should return- ['aa', 'bb', 'cc', 'xx', 'zz'])
My 3 row solution:
def linear_merge(list1, list2): list1.sort (list1.extend (list2)) return list1
Google's 10 row solution:
def linear_merge(list1, list2): result =  while len(list1) and len(list2): if list1 < list2: result.append(list1.pop(0)) else: result.append(list2.pop(0)) result.extend(list1) result.extend(list2) return resultI was running several lists to test my code and it works just fine. This also goes fine with The Zen of Python “Simple is better than complex.” So I am asking you, the Python people, can you find the reason why my code is only three rows while Google’s is much bigger? Do I have a bug? Can you do it better?
Anyway, that's it for this subject and I am all fired-up for the next one as I continue on my journey into Python from Scratch.
Published at DZone with permission of Hod Benbinyamin. See the original article here.
Opinions expressed by DZone contributors are their own.