• + 1 comment

    Here is a section of code you can use just for converting the numbers 1-29 into words in python. Normally you could use :

    pip install num2words
    from num2words import num2words as n2w
    

    but the environment on hackerrank doesn't allow the "pip install num2words" command. So I put together this to use instead for this challenge.

    def n2w(n):
        words = {
            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"
        }
        if n <= 19:
            return words[n]
        base = "twenty"
        return base if n == 20 else base + " " + words[n - 20]