-8

I'd like to create a function that will print the max value within a list of numbers.

1 Answers1

-1

I dont know python but the pseudocode would look something like this

max = –2147483648

for each integer n in collection
   if n > max
      max = n

It loops through every number in a collection. If the number (n) is bigger than the previous maximum, the maximum is set to be equal to n.

Tokfrans
  • 1,939
  • 2
  • 24
  • 56
  • doesn't work if all values < 0 – m.wasowski Oct 05 '14 at 01:20
  • @m.wasowski It does now ;) – Tokfrans Oct 05 '14 at 01:23
  • @m.wasowski I'm sorry if I'm not the best programmer in the world. But I'm pretty sure the OP got what he was asking for. – Tokfrans Oct 05 '14 at 01:26
  • 3
    he should do this on his own. And general algorithm is: 1) if collection is empty, return None or raise error. 2) max := first element 3) ... then your code X)... return max – m.wasowski Oct 05 '14 at 01:29
  • @m.wasowski Very well, then. Glad you wanted to help him some more =) – Tokfrans Oct 05 '14 at 01:30
  • 2
    Integers in Python [can be of arbitrary size](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex) - any solution which assumes they're limited to 32 bits will fail for some valid inputs. – Zero Piraeus Oct 05 '14 at 02:18
  • 2
    ... and in any case, the question specifies "numbers" rather than "integers". What happens with the list `[-1.0e10, -1.0e12, -1.0e11]`? – Zero Piraeus Oct 05 '14 at 02:24