Skip to content
Advertisement

Detect multi-digit numbers seperated by spaces in a string

I’m trying to write a function that returns the largest and smallest number in a string. However, not all numbers in the string are 1 digit numbers – some are 2 or 3 digits long.

Examples:

high_and_low("1 2 3 4 5")      # return "5 1"
high_and_low("1 2 -3 4 5")     # return "5 -3"
high_and_low("12 9 314 4 -57") # return "314 -57"

This exercice originates from codewars.com

Advertisement

Answer

It’s really easy to do with a list comprehension and int conversion

def high_and_low(nums)
    nums = [int(n) for n in nums.split()]
    return max(nums), min(nums)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement