Codecademy Forums

FAQ: Code Challenge: Loops - Max Num

Thank you very much for the prompt and clear reply.

1 Like

For some silliness, that does employ a list comprehension…

a =  [95, 39, 76, 84, 19, 97, 26, 34]
while len(a) > 1:
    a = [x for x in a if x > a[0]]
print (a)    # [97]

This code is frought with flaws, which means it is unreliable. As a skeleton, it is useful, but only as a that. More logic is required.

a =  [19, 98, 95, 39, 76, 84, 19, 97, 26, 34, 98]
while len(a) > 1:
    a = [x for x in a if x > a[0]] or [a[0]]
print (a)    # [98]

Test the above. It may have less flaws, but I’m not yet convinced it is letter perfect. Remove 19 from the first position, and run again. That was the flaw of the first version, which now seems repaired. We need more test scenarios to be sure that actually addresses the issue. Logically it is sound, to my mind, but we’re not standing on solid ground, as it were.

As complexity is concerned, we would be iterating a list which is getting progressively shorter each time, but it would still mean re-iteration, which would be a factor of more than 1 on N values in the original list. Since it is a constant, Big O would remain, O(N), but we can see it would still take more steps than once through the list.


Addendum

a =  [19, 98, 95, 39, 76, 84, 19, 97, 26, 34, 98]
while len(a) > 1:
  u, v = a[0], a[1]    # symbolize as early as possible
  if v > u:
    a.pop(0)
  else:
    a.pop(1)
b =  [19, 98, 95, 39, 76, 84, 19, 97, 26, 34, 98]
while len(b) > 1:
  u, v = b[0], b[1]
  if v > u:
    b.remove(u)
  else:
    b.remove(v)
>>> a, b
([98], [98])
>>> 

Further Addendum

a =  [19, 98, 95, 39, 76, 84, 19, 97, 26, 34, 98]
while len(a) > 1:
  u, v = a[0], a[1]
  a.pop(0 if v > u else 1)

b =  [19, 98, 95, 39, 76, 84, 19, 97, 26, 34, 98]
while len(b) > 1:
  u, v = b[0], b[1]
  b.remove(u if v > u else v)

def max(sample):
  s = sample[:]
  while len(s) > 1:
    u, v = s[0], s[1]
    s.remove(u if v > u else v)
  return s[0]

Addendum: June 17 0400 UTC

This last function has everything we need, given we’ve called upon a built-in. It chomps down a list in N - 1 iterations. Safely an O(N) as complexity goes.

As we can see, there is no suggestion of equality. When two numbers are equal, the one is not greater than the other so is removed. One cannot think how to tinker with this code any further.

def min(sample):
  s = sample[:]
  while len(s) > 1:
    u, v = s[0], s[1]
    s.remove(u if v < u else v)
  return s[0]

But using no methods at all we get to the bottom very quickly…

def maximum(sample):
  s = sample[0]
  for x in sample:
    s = x if x > s else s
  return s
# maximum([19, 98, 95, 39, 76, 84, 19, 97, 26, 34, 98])
# 98
def minimum(sample):
  s = sample[0]
  for x in sample:
    s = x if x < s else s
  return s
# minimum([19, 98, 95, 39, 76, 84, 19, 97, 26, 34, 98])
# 19

Still O(N) but much faster, I would conclude. Time it with large lists and see how the two compare.

Bottom line, list comprehensions don’t really fit this type of problem. Comprehension and reduction are one way to see the picture. Brute force is the other. MIN and MAX seem to fit the brute force template.

My first thought was simply:

def max_num(nums):
nums.sort()
return nums[-1]

It results in the correct answer no matter how many times I change the numbers in the list. Is this acceptable or have I missed something in the question?

I did figure my own way as well without using built in functions like .sort() but for all intents and purposes would my original function be okay outside of the question?

1 Like

It’s not as much as how we can do this task, but how we can do it without resorting to built in methods. It is intended as practice in writing algorithms that get to the core of a task.

def max_num(nums):
    max = nums[0]
    for x in nums:
        if x > max:
            max = x
    return max

Outside of the question, we could just as easily use the max() function and save ourselves writing more code than we need. That’s what those functions are for… To save us time and code. When we understand what and how they work, it makes it easier to appreciate why they are part of the language.

Algorithms are more about expressing ideas than just code. Never discount the value of a good algorithm, no matter how naive it may be. It’s nice to know that if all we have is a screwdriver and thumb wrench we can still do the job.

Hello all,

for some reason my code returns the value 50 but I am not sure why. In the end, I requested to see the solution and it turns out, it is the same as mine (except for the name of my variables)

#Write your function here
def max_num(nums):
maxNum = nums[0]

for i in nums:
if i > maxNum:
maxNum = i
return maxNum

#Uncomment the line below when your function is done
print(max_num([50, -10, 0, 75, 20]))

Could anyone please assist in identifying why the output is 50 (and of course marked wrong)?

We cannot tell from looking at the code but I suspect the return statement is inside the loop body.

1 Like

Thank you very much for your reply my. The code in the message didn’t display as it did when I pasted it in. I have another check over my return statement.

Andrea Hopkins

1 Like