Answer to Question #297104 in Python for Ram

Question #297104

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 triple six double six eight


Sample Input2

9090407368

Sample Output2

nine zero nine zero four zero seven three six eight


1
Expert's answer
2022-02-13T09:43:00-0500
import itertools

digits = {'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven',
          '8': 'eight', '9': 'nine'}
prefix = {2: 'double', 3: 'triple', 4: 'quadruple'}
tel = input()
rez = []
for substr in [tel[:4], tel[4:7], tel[7:]]:
    for k, g in itertools.groupby(substr):
        n = len(list(g))
        if n > 1:
            rez.append(prefix[n])
        rez.append(digits[k])
print(' '.join(rez))

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
APPROVED BY CLIENTS