Answer to Question #318947 in Python for Aakash

Question #318947

give a number having 10 digits , convert it to a string following the below mentioned rules.rules for conversion . seperate set of numbers to four "-three" "-three" digits.use the following prefixes for successive digits. single numbers: just read them separately .two consecutive numbers: double. three consecutive numbers: triple.four consecutive numbers: quadruple in assignment expert in python

1
Expert's answer
2022-03-27T05:47:02-0400
def ddd2str(ddd):
    """Convert a string of several digits into wods
    """
    prefix = ['', 'double ', 'triple ', 'quadruple ']
    digits = ['zero', 'one', 'two', 'three', 'four', 
         'five', 'six', 'seven', 'eight', 'nine']
    res = ''


    d = ddd[0]
    n = 1
    for i in range(1,len(ddd)):
        if d == ddd[i]:
            n += 1
        else:
            res += prefix[n-1] + digits[int(d)] + ' '
            d = ddd[i]
            n = 1
    res += prefix[n-1] + digits[int(d)]
    return res


def n2str(n):
    """Convert a number to a string"""
    dddd = f'{n//1000000:04}'
    n %= 1000000
    res = ddd2str(dddd)


    ddd = f'{n//1000:03}'
    n %= 1000
    res += ' - ' + ddd2str(ddd)


    ddd = f'{n:03}'
    res += ' - ' + ddd2str(ddd)
    return res


def main():
    n = int(input('Enter 10-digits number: '))
    s = n2str(n)
    print(s)


main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS