fork download
  1. units = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ',
  2. 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ',
  3. 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen ']
  4. tens = ['twenty ', 'thirty ', 'forty ', 'fifty ']
  5.  
  6. def min_str(mins):
  7. if mins < 20:
  8. return (0 < mins < 10) * 'oh ' + units[mins]
  9. else:
  10. q, r = divmod(mins, 10)
  11. return tens[q-2] + min_str(r)[3:]
  12.  
  13. def time_to_words(timestr):
  14. hr, mins = map(int, timestr.split(':'))
  15. outstr = "It's " + (units[hr%12] or 'twelve ') + min_str(mins)
  16. outstr += 'am' if hr < 12 else 'pm'
  17. return outstr
  18.  
  19. for x in ['00:00', '01:30', '12:05', '14:01', '20:29', '21:00']:
  20. print time_to_words(x)
Success #stdin #stdout 0s 23352KB
stdin
Standard input is empty
stdout
It's twelve am
It's one thirty am
It's twelve oh five pm
It's two oh one pm
It's eight twenty nine pm
It's nine pm