Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading