Chris received the following algorithm written in Python :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
It takes in an integer list and initially and . What does this algorithm returns?
Details and Assumptions
INF
is larger than any integer in
.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Running through the code:
Line 2: If there is now only 1 item in L, return a very small number
Line 7+8: Get the maximum number in each side of the list L
Line 9: If there are two of the same largest numbers, then return it. This paired with Line 2 are already good indications that a some maximum number is returned, but not the most maximum number.
Line 13: If the left side had the larger number, make sure there is no number smaller than that but larger than y (the largest on the right side) At this point saying that the function takes the second largest number makes sense by the recursive use here.