fork download
  1. from functools import reduce
  2.  
  3. lis = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]
  4.  
  5. f = lambda num:reduce(lambda x,y:(x[0]%y[0], x[1]+y[1]*(x[0]//y[0])), lis, (num, ''))
  6.  
  7. numbers = [4100, 2345, 658, 900, 23, 6, 239, 244]
  8.  
  9. for number in numbers:
  10. print(f"{number} : {f(number)}")
Success #stdin #stdout 0.02s 9172KB
stdin
Standard input is empty
stdout
4100 : (0, 'MMMMC')
2345 : (0, 'MMCCCXLV')
658 : (0, 'DCLVIII')
900 : (0, 'CM')
23 : (0, 'XXIII')
6 : (0, 'VI')
239 : (0, 'CCXXXIX')
244 : (0, 'CCXLIV')