mistahchris

r/dailyprogrammer [2017-06-27] Challenge #321 [Easy] Talking

Jun 28th, 2017
184
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # talking_clock.py
  2. from sys import argv
  3.  
  4.  
  5. def read_input(filepath='input.txt'):
  6.     with open(filepath, 'r') as f:
  7.         return [l.strip() for l in f.readlines()]
  8.  
  9.  
  10. def num_to_words(num):
  11.     num = int(num)
  12.     if num > 60:
  13.         raise NotImplementedError('num_to_words is not implemented for non-time numbers')
  14.  
  15.     lows = {0: 'oh', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven',
  16.                8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve'}
  17.     highs = {2: 'twenty', 3: 'thirty', 4: 'fourty', 5: 'fifty', 6: 'sixty'}
  18.  
  19.     result = ''
  20.     if num <= 12:
  21.         result = lows[num]
  22.     elif num <= 19:
  23.         prefix = lows[num % 10] if num != 15 else 'fif'
  24.         result = '{}teen'.format(prefix)
  25.     else:
  26.         result = highs[num // 10]
  27.         low = num % 10
  28.  
  29.         if low != 0:
  30.             result += ' {}'.format(lows[low])
  31.  
  32.     return result
  33.  
  34.  
  35. def hour_to_words(hour_string):
  36.     hour = int(hour_string)
  37.     pm = hour >= 12
  38.  
  39.     if pm and hour != 12:
  40.         hour = hour - 12
  41.     elif not pm and hour == 0:
  42.         hour = 12
  43.  
  44.     am_or_pm = 'pm' if pm else 'am'
  45.  
  46.     return (num_to_words(hour), am_or_pm)
  47.  
  48.  
  49. def minutes_to_words(minutes_string):
  50.     minutes = int(minutes_string)
  51.     result = ''
  52.  
  53.     if minutes > 0:
  54.         oh = 'oh ' if minutes < 10 else ''
  55.         result = '{}{}'.format(oh, num_to_words(minutes))
  56.     return result
  57.  
  58.  
  59. def time_to_words(time_string):
  60.     hours, minutes = time_string.strip().split(':')
  61.     hour_string, am_or_pm = hour_to_words(hours)
  62.     minutes_string = minutes_to_words(minutes)
  63.  
  64.     return ' '.join([hour_string, minutes_string, am_or_pm]).replace('  ', ' ')
  65.  
  66.  
  67. if __name__ == '__main__':
  68.     input_time = []
  69.     if len(argv) > 1:
  70.         input_time = read_input(filepath=argv[1])
  71.     else:
  72.         input_time.extend(read_input())
  73.  
  74.     for t in input_time:
  75.         print('It\'s {}'.format(time_to_words(t)))
  76.  
  77. # ------------------------------------
  78. # test file
  79. # test_talking_clock.py
  80.  
  81. import pytest
  82. import talking_clock as T
  83.  
  84.  
  85. def test_num_to_words():
  86.     assert T.num_to_words('1') == 'one'
  87.     assert T.num_to_words('11') == 'eleven'
  88.     assert T.num_to_words('15') == 'fifteen'
  89.     assert T.num_to_words('44') == 'fourty four'
  90.     assert T.num_to_words('60') == 'sixty'
  91.  
  92.     with pytest.raises(NotImplementedError):
  93.         T.num_to_words('69')
  94.  
  95.  
  96. def test_time_to_words():
  97.     assert T.time_to_words('11:11') == 'eleven eleven am'
  98.     assert T.time_to_words('12:59') == 'twelve fifty nine pm'
  99.     assert T.time_to_words('13:09') == 'one oh nine pm'
  100.     assert T.time_to_words('00:00') == 'twelve am'
RAW Paste Data