15,665,718 members
1.00/5 (1 vote)
See more:
In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.

Example:
high_and_low("1 2 3 4 5") # return "5 1"
high_and_low("1 2 -3 4 5") # return "5 -3"

What I have tried:

Python
```def high_and_low(numbers):
li = numbers.split()
mx = max(li)
mn = min(li)
return str(mx)+" "+str(mn)
print(high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"))```

I am getting 6 -214
Posted
Updated 9-Nov-21 3:35am

## Solution 1

Quote:
I am getting 6 -214

Just a guess: you forgot to convert each element of `li` to numbers.
`numbers` is a string, after split, you get a list of strings. Since you want to compare integer values of strings, you need to covert the list of strings to a list of integers.
Quote:
Why in string "6" is greater than others? How max function works on string. In my opinion it uses the number of charaters in the string but this does seem to be the case here.

Sorted strings are in same order as words in a dictionary.
-24 < -52 < 1 < 11 < 15 < 2 < 25< 2614 < 3 < 33 < 4

v2
Member 14517556 13-Aug-19 1:48am
Why in string "6" is greater than others? How max function works on string. In my opinion it uses the number of charaters in the string but this does seem to be the case here.
Richard MacCutchan 13-Aug-19 4:28am
'6' is considered highest because it is the highest when the characters are sorted in alphabetical order.

## Solution 2

```def high_and_low(numbers):
li =[int(x) for x in list1.split(' ')]
mx = max(li)
mn = min(li)
return str(mx)+" "+str(mn)
print(high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"))```

## Solution 3

```def high_and_low(numbers):
numbers = numbers.split(" ")
numbers = list(map(int, numbers))
# so every number in numbers will be recognized as an integer
p = max(numbers)
q = min(numbers)
return str(p) + str(" ") + str(q)
print(high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"))
# Output will be: 542 -214
########################################################

def high_and_low(numbers):
return " ".join(i(numbers.split(), key=int) for i in (max, min))
print(high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"))```

v3
CHill60 9-Nov-21 8:49am
In what way is this an improvement on (or even different from) Solution 2?
Nandkumar Pawar 9-Nov-21 9:32am
It is different because I used map function instead for.
If you want improvement, here take this one-liner (though it is not different than 2nd solution. Just better.)