🐍Python/Python_알고리즘

[알고리즘] 54. The Time in Words

728x90
반응형

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. It should return a time string as described.

timeInWords has the following parameter(s):

  • h: an integer representing hour of the day
  • m: an integer representing minutes after the hour

Input Format

The first line contains , the hours portion The second line contains , the minutes portion

Constraints

Output Format

Print the time in words as described.

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



답 : 



#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the timeInWords function below.
if __name__ == '__main__':

h = int(input())
m = int(input())

minutes = ["zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
"twenty",
"twenty one",
"twenty two",
"twenty three",
"twenty four",
"twenty five",
"twenty six",
"twenty seven",
"twenty eight",
"twenty nine"]

if m <= 30 and m > 1:
if m == 15:
print("quarter past {0}".format(minutes[h]))
elif m == 30:
print("half past {0}".format(minutes[h]))
else:
print("{0} minutes past {1}".format(minutes[m],minutes[h]))
elif m >30:
if m == 45:
print("quarter to {0}".format(minutes[h+1]))
else:
print("{0} minutes to {1}".format(minutes[60-m],minutes[h+1]))

elif m == 0:
print("{0} o' clock".format(minutes[h]))
elif m == 1:
print("one minute past one")



굉~~~~장히 귀찮은 문제... 각 시간대 별로 1/4, 1/2지점에서 말을 바꿔줘야하고, 1분은 단수형, 0일땐 정각을 표시하고 30 이하일 때는 past로 30 초과일 때는 to로 표현해줘야 한다. 

728x90
반응형