Skip to content

Latest commit

 

History

History
63 lines (40 loc) · 1.03 KB

Simplify_the_number.md

File metadata and controls

63 lines (40 loc) · 1.03 KB

CodeWars Python Solutions


Simplify the number!

Given a positive integer as input, return the output as a string in the following format:

Each element, corresponding to a digit of the number, multiplied by a power of 10 in such a way that with the sum of these elements you can obtain the original number.

Examples

Input Output
0 ""
56 "5*10+6"
60 "6*10"
999 "9100+910+9"
10004 "1*10000+4"

Note: input >= 0


Given Code

def simplify(number):
    pass

Solution

def simplify(number):
    number = str(number)
    if len(number) == 1:
        if number == "0":
            return ""
        else:        
            return number

    l = len(number) -1
    n = []

    for c in number[:-1]:
        if c != "0":
            n.append("{}*1{}".format(c, "0"*l))
        l -= 1

    return "+".join(n) + "+" + number[-1] if number[-1] != "0" else "+".join(n)

See on CodeWars.com