Cerinta completa
Given the time in numerals we may convert it into words, as shown below:
At , use o’ clock. For , use past, and for use to. Note the space between the apostrophe and clock in o’ clock. Write a program which prints the time in words for the input given in the format described.
Function Description
Complete the timeInWords function in the editor below.
timeInWords has the following parameter(s):
- int h: the hour of the day
- int m: the minutes after the hour
Returns
- string: a time string as described
Input Format
The first line contains , the hours portion
The second line contains , the minutes portion
Constraints
Sample Input 0
5
47
Sample Output 0
thirteen minutes to six
Sample Input 1
3
00
Sample Output 1
three o' clock
Sample Input 2
7
15
Sample Output 2
quarter past seven
Limbajul de programare folosit: python3
Cod:
#!/bin/python3
nums = {
1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',10:'ten',
11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'fifteen',16:'sixteen',17:'seventeen',
18:'eighteen',19:'nineteen',20:'twenty',21:'twenty one',22:'twenty two',23:'twenty three',
24:'twenty four',25:'twenty five',26:'twenty six',27:'twenty seven',28:'twenty eight',29:'twenty nine'
}
def timeInWords(h, m):
if m == 0:
return f"{nums[h]} o' clock"
if m == 15:
return f"quarter past {nums[h]}"
if m == 30:
return f"half past {nums[h]}"
if m == 45:
return f"quarter to {nums[(h % 12) + 1]}"
if m < 30:
minute = 'minute' if m == 1 else 'minutes'
return f"{nums[m]} {minute} past {nums[h]}"
rem = 60 - m
minute = 'minute' if rem == 1 else 'minutes'
return f"{nums[rem]} {minute} to {nums[(h % 12) + 1]}"
if __name__ == '__main__':
h = int(input().strip())
m = int(input().strip())
print(timeInWords(h, m))
Scor obtinut: 1.0
Submission ID: 464588786
Link challenge: https://www.hackerrank.com/challenges/the-time-in-words/problem
