Skip to content

Instantly share code, notes, and snippets.

@ChristopherBilg
Created March 15, 2018 13:49
Show Gist options
  • Save ChristopherBilg/fe3449d8d315be0df486f8f7574826da to your computer and use it in GitHub Desktop.
Save ChristopherBilg/fe3449d8d315be0df486f8f7574826da to your computer and use it in GitHub Desktop.
r/DailyProgrammer Talking Clock
#!usr/bin/env python3
"""
This script is the r/DailyProgrammer Easy Challenge #321 titled
Talking Clock.
Author: Christopher Bilger
"""
WORD_HOURS = [
"twelve",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven"
]
WORD_HOURS_TRUNC = [
"",
"",
"twenty",
"thirty",
"fourty",
"fifty"
]
WORD_MINUTES_TEENS = [
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
]
class Clock():
"""
This class handles the clocks raw input and has an output method.
Sphinx markup below
:param str time: The current time in a 24-hour format
"""
def __init__(self, time):
if int(time.split(":")[0]) > 11:
self.hours = int(time.split(":")[0])-12
self.night = True
else:
self.hours = int(time.split(":")[0])
self.night = False
self.minutes = int(time.split(":")[1])
def get_hours_in_words(self):
"""
Getter method for the Clock.hours in human-readable format
:return The clock hours
:rtype: str
"""
return WORD_HOURS[self.hours]
def get_minutes_in_words(self):
"""
Getter method for the Clock.minutes in human-readable format
:return The clock minutes
:rtype: str
"""
if self.minutes == 0:
return ""
elif self.minutes in range(1, 9):
return "oh " + WORD_HOURS[self.minutes] + " "
elif self.minutes in range(10, 19):
return WORD_MINUTES_TEENS[self.minutes-10] + " "
else:
ret = WORD_HOURS_TRUNC[int(str(self.minutes)[0])]
if int(str(self.minutes)[1]) == 0:
return ret + " "
else:
return ret + " " + WORD_HOURS[int(str(self.minutes)[1])] + " "
def get_meridiem(self):
"""
Getter method for the meridiem (Clock.night boolean)
:return am or pm
:rtype: str
"""
return "pm" if self.night else "am"
def main():
"""
The main method that will run the program.
"""
time = Clock("21:06")
print("It's " + time.get_hours_in_words() + " "
+ time.get_minutes_in_words() + time.get_meridiem()) \
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment