In [19]:
# https://www.codewars.com/kata/write-number-in-expanded-form/train/python
import functools # needed for code wars apparently
def expanded_form(num):
str_form = str(num)[::-1]
valArr = []
for i in range(0, len(str_form)):
if str_form[i] == "0": continue
valArr.insert(0, str_form[i] + ("0" * i))
# this also works
# return functools.reduce(lambda x,y: x + " + " + y, valArr)
return ' + '.join(valArr)
In [21]:
expanded_form(10072)
Out[21]:
In [18]:
# this was the top answer
def expanded_form_CodeWars(n):
result = []
for a in range(len(str(n)) - 1, -1, -1):
current = 10 ** a
quo, n = divmod(n, current)
if quo:
result.append(str(quo * current))
return ' + '.join(result)
In [ ]: