Answer to Question #296876 in Python for Ram

Question #296876

Convert the Number

Given a number having ten digits, convert it to a string following the below-mentioned rules.


Rules for conversion:

1.separate the number into a set of four-three-three digits

2.Use the following prefixes for successive digits

a.Single numbers: just read them separately

b.Two consecutive numbers: double

c.Three consecutive numbers: triple

d.Four consecutive numbers: quadruple


Input

The first line of input is a string of ten digits.



Sample Input1

9887666668

Sample Output1

nine double eight seven triplesix double six eight


Sample Input2

9090407368

Sample Output2

nine zero nine zero four zero seven three six eight


1
Expert's answer
2022-02-12T04:34:15-0500
def digits2tsr(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.strip()


def main():
    n = int(input())
    s = f'{n:010d}'
    res = digits2tsr(s[:4]) + ' '
    res += digits2tsr(s[4:7]) + ' '
    res += digits2tsr(s[7:])
    print(res)
    
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