Thursday, February 10, 2022

Write Number in Expanded Form Python Codewars

You will be given a number and you will need to return it as a string in Expanded Form. For example:

expanded_form(12) # Should return '10 + 2'
expanded_form(42) # Should return '40 + 2'
expanded_form(70304) # Should return '70000 + 300 + 4'

NOTE: All numbers will be whole numbers greater than 0.



def expanded_form(num):
    res = []
    for index,digit in enumerate(str(num)):
        if digit != '0':
            res.append(digit+'0'*(len(str(num)[index:])-1))
    return ' + '.join(res)

print(expanded_form(70304))

Training on Write Number in Expanded Form | Codewars



Related Posts


No comments :

Post a Comment