-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThe Time in Words.py
50 lines (40 loc) · 1.35 KB
/
The Time in Words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'''
Problem Statement: https://www.hackerrank.com/challenges/the-time-in-words/problem
@Coded by TSG, 2020
'''
import math
import os
import random
import re
import sys
# Complete the timeInWords function below.
def timeInWords(h, m):
res = ''
numbers = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve', 'thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty']
minute = 'minute'
if m != 1:
minute += 's'
if m == 0:
res = numbers[h] + " o' clock"
elif m == 30:
res = "half past " + numbers[h]
elif m == 15:
res = "quarter past " + numbers[h]
elif m == 45:
res = "quarter to " + numbers[h + 1]
elif m < 20:
res = numbers[m] + ' ' + minute + ' past ' + numbers[h]
elif m < 30:
res = numbers[-1] + ' ' + numbers[int(m%10)] + ' ' + minute + ' past ' + numbers[h]
elif m > 45:
res = numbers[60 - m] + ' ' + minute + ' to ' + numbers[h + 1]
elif m > 30:
res = numbers[-1] + ' ' + numbers[int(m%10)] + ' ' + minute + ' to ' + numbers[h + 1]
return res.replace(' ', ' ')
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
h = int(input())
m = int(input())
result = timeInWords(h, m)
fptr.write(result + '\n')
fptr.close()