A Programmer Quit His Job And Left This Message In Pseudocode

It's your first day at work as an intern, and your boss explains a major problem: "Our only developer quit yesterday, and he left this note on his desk. We need to know what it does. If you can figure it out, we'll promote you to his job."

#PSEUDOCODE    
mystery(list):
    if list is empty then return the empty list
    otherwise:
        let list1 consist of all elements of list that are <= first element, excluding the first element itself
        let list2 consist of only the first element of list
        let list3 consist of all elements of list that are > first element
        return mystery(list1) + list2 + mystery(list3) # + indicates list concatenation

thelist = [1, 4, 2, 3, 3, 45, 6, 7, 8, 5, 4, 3, 2, 21, 2, 3, 4, 5, 6, 7]
newlist = mystery(thelist)
total = 0
for i in range(5):
    total += newlist[i]
print total

So, what is this code supposed to print?

Details and assumptions
1. To make it clear what is meant by list concatenation, here is an example: \[[2, 7] + [8, 9, 3] = [2, 7, 8, 9, 3].\] 2. Note that mystery is a recursive function (it calls itself at the last step).

Image credit: oldcomputers.com
×

Problem Loading...

Note Loading...

Set Loading...