DEV Community

loading...

Discussion on: Daily Challenge #260 - Subtract the Sum

Collapse
boris profile image
Boris Quiroz • Edited

Some python-that-can-be-improved:

elements = [
        "1-kiwi",
        "2-pear",
        "3-kiwi",
        "4-banana",
        "5-melon",
        "6-banana",
        "7-melon",
        "8-pineapple",
        "9-apple",
        "10-pineapple",
        "...",
        "100-pineapple"
        ]

def substractSum(n):
    if 10 <= n <= 10000:
        sum_n = 0
        for digit in str(n):
            sum_n += int(digit)

        result = n - sum_n

        while result > 100:
            for digit in str(n):
                sum_n += int(digit)
            result = n - sum_n

        print(n, result, elements[result-1])
    else:
        print("{}: Use value between 10 and 10000".format(n))

substractSum(9)
substractSum(10)
substractSum(31)
substractSum(110)
substractSum(111)
substractSum(325)
substractSum(1204)
substractSum(1316)
substractSum(2888)
substractSum(10000)
substractSum(10001)

Output of the above:

❯ python 260.py
9: Use value between 10 and 10000
10 9 9-apple
31 27 27-apple
110 100 100-pineapple
111 99 99-apple
325 95 95-melon
1204 98 98-pineapple
1316 95 95-melon
2888 80 80-cucumber
10000 100 100-pineapple
10001: Use value between 10 and 10000