Answer to Question #292089 in Python for Aparna

Question #292089

You are given a string containing ten digits, write a program to print these digits in a human readable format.



There are some rules to how to read a number,



1. Divide the ten digit number into set of four-three-three digits



2.use the following prefixes for consecutive digits



# Single numbers just read them seperately



# 2 successive numbers use double



# 3 successive numbers use triple



# 4 successive numbers use quadruple


1
Expert's answer
2022-01-30T05:12:44-0500
def digist2tsr(s):
    res = ''
    digits = ['zero', 'one', 'two', 'three', 'four', 
              'five', 'six', 'seven', 'eight', 'nine']
    prefix = ['', 'double', 'triple', 'quadruple']
    d_prev = -1
    count = 0
    for ch in s:
        d = ord(ch) - ord('0')
        if d != d_prev:
            if d_prev >= 0:
                res += prefix[count-1] + ' ' + digits[d_prev] + ' '
            d_prev = d
            count = 0
        count += 1
    res += prefix[count-1] + ' ' + digits[d]
    return res

def num2str(n):
    s = f'{n:010d}'
    res = digist2tsr(s[:4]) + ' - '
    res += digist2tsr(s[4:7]) + ' - '
    res += digist2tsr(s[7:])
    return res

def main():
    while True:
        n = int(input("Enter a number: "))
        if n == 0:
            break
        s = num2str(n)
        print(s)

if __name__ == '__main__':
    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