• + 0 comments

    I decided just to keep it simple and just use a list:

    def timeInWords(h: int, m: int):
        words = [
            "o' clock",
            "one",
            "two",
            "three",
            "four",
            "five",
            "six",
            "seven",
            "eight",
            "nine",
            "ten",
            "eleven",
            "twelve",
            "thirteen",
            "fourteen",
            "quarter",
            "sixteen",
            "seventeen",
            "eighteen",
            "nineteen",
            "twenty",
            "twenty one",
            "twenty two",
            "twenty three",
            "twenty four",
            "twenty five",
            "twenty six",
            "twenty seven",
            "twenty eight",
            "twenty nine",
            "half",
        ]
        
        assert len(words) == 31, "There's one number missing or something"
        
        hour = words[h]
        next_hour = 0
        if m == 0:
            return hour + " " + words[m]
        
        connector: str = ' minute'
        magic_word = ' '
        
        if 1 <= m <= 30:
            magic_word += 'past '
        else:
            magic_word += 'to '
            m = 60 - m
            next_hour = 1
            
        if m > 1:
            connector += 's'
            
        hour = words[h+next_hour]
        minutes = words[m]
            
        # Handle 15 and 30
        if m in (15, 30):
            return minutes + magic_word + hour
        
        return minutes + connector + magic_word + hour