Comments on: Python from scratch – lists https://go.hodspot.com/python-from-scratch-lists/ Personal blog about technology Sat, 01 Jun 2019 10:16:36 +0000 hourly 1 https://wordpress.org/?v=6.8.3 By: Hod https://go.hodspot.com/python-from-scratch-lists/#comment-19 Tue, 17 Apr 2012 11:09:48 +0000 #comment-19 It did.
Good advise.

]]>
By: Cem GULER https://go.hodspot.com/python-from-scratch-lists/#comment-24 Wed, 21 Mar 2012 20:03:10 +0000 #comment-24 def linear_merge(list1, list2): return sorted(list1 + list2)

can do the job also.

]]>
By: Hod https://go.hodspot.com/python-from-scratch-lists/#comment-40 Thu, 02 Feb 2012 11:40:33 +0000 #comment-40 Thanks Arik for your answer.
At this point of my studies- complexity is not something i should deal with.
Still of course you are right, Thanks for your answer.

]]>
By: Arik https://go.hodspot.com/python-from-scratch-lists/#comment-41 Thu, 02 Feb 2012 09:33:41 +0000 #comment-41 One of the reasons one would chose Python is for the tendency of the code to be clearer to read (compared to other languages).

Your form, however valid, will render the code less readable.

]]>
By: Arik https://go.hodspot.com/python-from-scratch-lists/#comment-42 Thu, 02 Feb 2012 09:31:42 +0000 #comment-42 I believe Google's solution is right.

Your solution has an O(nlogn) complexity while Google's solution has an O(n) time complexity limit (also known as "linear time"). If you work with large lists yours will run considerably slower.

The exercise calls for it: "Ideally, the solution should work in "linear" time, making a single pass of both lists" – and the name of the function hints at it.

— Arik

]]>
By: Anonymous https://go.hodspot.com/python-from-scratch-lists/#comment-45 Mon, 30 Jan 2012 15:16:06 +0000 #comment-45 The solution for the first problem you mention is to use the key parameter which allows you to map each item of the list to a key value which is then used for sorting. The following sorts a list xs of tuples by the last item in each pair as required:

def sort_by_last(xs):
return sorted(xs, key=lambda x: x[-1])

As far as the Google sort method given they're implementing a version of merge sort in python. Your version is a little bit dodgy as you're passing in the result of list.extend as the cmp parameter to list.sort. Fortunately the result of list.extend is None so you get away with it, but I wouldn't encourage that kind of code in general.

]]>
By: Anonymous https://go.hodspot.com/python-from-scratch-lists/#comment-47 Mon, 30 Jan 2012 14:59:05 +0000 #comment-47 There's a one-liner:

def linear_merge (list1, list2): return list (sorted (list1.extend (list2)))

🙂

]]>
By: Hod https://go.hodspot.com/python-from-scratch-lists/#comment-43 Mon, 30 Jan 2012 13:29:45 +0000 #comment-43 I got you.
Do you think this code is better?

def linear_merge(list1, list2):
list1.extend (list2)
list1.sort ()
return list1

]]>
By: Hod https://go.hodspot.com/python-from-scratch-lists/#comment-44 Mon, 30 Jan 2012 13:24:58 +0000 #comment-44 lambda is a bit advanced for me. I just read about it and it sounds cool. Anyway yeah you are right and that was my understanding eventually.

As for your second answer, i will re-exam again your point and see if i get it right. Thanks Man.

]]>
By: Hod https://go.hodspot.com/python-from-scratch-lists/#comment-46 Mon, 30 Jan 2012 13:08:25 +0000 #comment-46 Thanks, I didn't know its possible to return with a code (Now i know).
So you agree with my solution? and do you understand why Google's answer is so long?

]]>