알고리즘 문제/CodeWar

Write Number in Expanded Form

BEstyle 2022. 9. 21. 12:49

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):
    ans=""
    print(num)
    num=str(num)
    leng=len(num)
    print(leng)
    for i in range(len(num)):
        print (num[i])
        if num[i] !="0":
            ans+= str(int(num[i])*(10**(leng-i-1)))
            ans+= " + "
        print(ans)
    ans=ans[:-3]
    return ans