Integer to English Words


In [1]:
import sys; sys.path.append('../..')
from puzzles import leet_puzzle
leet_puzzle('integer-to-english-words')


Source : https://leetcode.com/problems/integer-to-english-words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)


In [62]:
import logging


def integer_to_english_words(number):
    """
    Convert a non-negative integer to its english words representation:
    
    >>> integer_to_english_words(123)
    One Hundred Twenty Three
    
    >>> integer_to_english_words(12345)
    Twelve Thousand Three Hundred Forty Five
    
    >>> integer_to_english_words(1234567
    One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven
    
    NOTE : The input number should be in the range 0..pow(2, 31)-1
    
    :param number: The number to get the english words representation of.
    :type number: int
    
    :return: English words representation of the input number
    :rtype: str
    """
    
    assert 0 <= number <= pow(2, 31) - 1
    
    if number == 0:
        return 'Zero'
    
    result = []
            
    def process_scale(number, scale, name):
        scale_in_number = number / scale
        number -= scale_in_number * scale
        if scale_in_number:
            if scale >= 100 or scale_in_number > 1:
                result.append(integer_to_english_words(scale_in_number))
            result.append(name)
        return number
        
    scales = [
        (1000000000, 'Billion'),
        (1000000, 'Million'),
        (1000, 'Thousand'),
        (100, 'Hundred'),
        (90, 'Ninety'),
        (80, 'Eighty'),
        (70, 'Seventy'),
        (60, 'Sixty'),
        (50, 'Fifty'),
        (40, 'Forty'),
        (30, 'Thirty'),
        (20, 'Twenty'),
        (19, 'Nineteen'),
        (18, 'Eighteen'),
        (17, 'Seventeen'),
        (16, 'Sixteen'),
        (15, 'Fifteen'),
        (14, 'Fourteen'),
        (13, 'Thirteen'),
        (12, 'Twelve'),
        (11, 'Eleven'),
        (10, 'Ten'),
        (9, 'Nine'),
        (8, 'Eight'),
        (7, 'Seven'),
        (6, 'Six'),
        (5, 'Five'),
        (4, 'Four'),
        (3, 'Three'),
        (2, 'Two'),
        (1, 'One'),
    ]
    
    for scale, name in scales:
        number = process_scale(number, scale, name)
        
    return ' '.join(result)


def verify(number, expected):
    """
    Verify the integer_to_english_words function against expected input.
    """
    actual = integer_to_english_words(number)
    if actual != expected:
        logging.error('integer_to_english_words(%d) == "%s" != "%s"', number, actual, expected)
        
        
def verify_fails(number):
    """
    Verify the integer_to_english_words function fails against expected incorrect input.
    """
    try:
        integer_to_english_words(number)
        logging.error('%d did not fail but a failure was expected', number)
    except:
        pass
        

verify(1000000000, 'One Billion')
verify(1000000, 'One Million')
verify(1000, 'One Thousand')
verify(199, 'One Hundred Ninety Nine')
verify(100, 'One Hundred')
verify(90, 'Ninety')
verify(80, 'Eighty')
verify(70, 'Seventy')
verify(60, 'Sixty')
verify(50, 'Fifty')
verify(40, 'Forty')
verify(30, 'Thirty')
verify(20, 'Twenty')
verify(10, 'Ten')
verify(9, 'Nine')
verify(8, 'Eight')
verify(7, 'Seven')
verify(6, 'Six')
verify(5, 'Five')
verify(4, 'Four')
verify(3, 'Three')
verify(2, 'Two')
verify(1, 'One')
verify(123, "One Hundred Twenty Three")
verify(12345, "Twelve Thousand Three Hundred Forty Five")
verify(1234567, "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven")
verify(1000010, 'One Million Ten')
verify(2147483647, 'Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven')
verify_fails(-1)
verify_fails(2147483648)
verify_fails('hello')