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