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
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()
Comments
Leave a comment