I had a hard time finding accurate description of the max function (which might not even be necessary for this specific assignment, yet it bothers me). It acts in peculiar ways at times. What exactly does the max function do when comparing between two different lists? Does it compare their length? Does it compare the greatest component of the two?
I would appreciate a clarification on that matter.
Date: 21 Dec 2014 13:21
Number of posts: 7
RSS: New posts
So your question is not at all about max, it's about how Python compares lists.
I tried to look for it now, and surprisingly didn't find anything (well, I gave up after 4 minutes).
So you're all welcome to contribute if you find the answer.
Well I didn't need an accurate answer to solve Q2 after all, so it goes without saying that my curiosity somewhat decreased (dramatically). I'll repost the question into stackoverflow, if they come up with some meaningful answer i'll let us know.
running it a few times, it seems that the function returns the list which has the highest lexicographic order (in accordance with the fact that with lists it seems that a<b if a comes before b in lexicographic order)
Yet it deals with edge cases differently. Consider max([1,1,1],[1,1]), obviously it returns [1,1,1], but why? Since lexicographically all elements of both lists are of the same value, it suddenly switches to a length test (you'd expect that maybe, it sums the lexicographic value of all of the elements in list A and compares it to the parallel value in list B, but it doesn't happen either). The need in accurate documentation still holds (there are, perhaps, other edge cases to consider).
[1,1,1] > [1,1] holds according the definition of lexichographic order. In general, a sequence that is a prefix of another sequence, is smaller than it.
Recall that when comparing X= x1 x2 x3 … with Y= y1 y2 y3 … , one looks for the first index i such that xi is different from yi, and if xi < yi , then X < Y. But if no such index exists, then if the length of the two sequences is equal, they are equal, but if not, the shorter sequence is smaller.