Участник:Polina Potapova/integerToEnglishWords

Материал из DISCOPAL
Перейти к: навигация, поиск

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

class Solution(object):
 
    orders_of_magnitude = [' Hundred', ' Thousand', ' Million', ' Billion']
 
    def units(self, number, s=''):
        """        
        for int number from 0 to 9 returns its english words representation
 
        number: int"""
        units = {0: 'Zero',
                1: 'One',
                2: 'Two', 
                3: 'Three',
                4: 'Four',
                5: 'Five',
                6: 'Six',
                7: 'Seven',
                8: 'Eight',
                9: 'Nine'}
        return s + units[number]
 
    def tens(self, number, s=''):
        """        
        for int number from 10 to 99 returns its english words representation
 
        number: int
        """
        teens = {10: 'Ten',
                11: 'Eleven',
                12: 'Twelve',
                13: 'Thirteen',
                14: 'Fourteen',
                15: 'Fifteen',
                16: 'Sixteen',
                17: 'Seventeen',
                18: 'Eighteen',
                19: 'Nineteen'}
        tens = {20: 'Twenty',
                30: 'Thirty',
                40: 'Forty',
                50: 'Fifty',
                60: 'Sixty',
                70: 'Seventy',
                80: 'Eighty',
                90: 'Ninety'}
        if number//10 == 1:
            return s + teens[number]
        elif number%10 == 0:
            return s + tens[number//10*10]
        else:
            return s + tens[number//10*10]  + ' ' + self.units(number%10)
 
 
    def hundreds(self, number, s=''):
        """        
        for int number from 000 to 999 returns its english words representation
 
        number: int
        """
        if number == 0: # numbers like 000
            return ''
        elif number//100 == 0 and number//10%10 == 0: # numbers like 001, 005, 007
            return s + self.units(number)
        elif number//100 == 0 and number//10%10 != 0: # numbers like 011, 025, 037
            return s + self.tens(number)
        elif number//10%10 == 0 and number%10 == 0:
            return s + self.units(number//100) + self.orders_of_magnitude[0]
        elif number//10%10 == 0 and number%10 == 0: # numbers like 100, 200
            return s + self.units(number//100) + self.orders_of_magnitude[0]
        elif number//10%10 == 0: # numbers like 101, 205, 307
            return s + self.units(number//100) + self.orders_of_magnitude[0] + ' ' + self.units(number%10, s='')
        else:
            return s + self.units(number//100) + self.orders_of_magnitude[0] + ' ' + self.tens(number%100, s='')
 
    def thousands(self, number, s=''):
        if number == 0:
            return ''
        else:
            return s + self.hundreds(number//10**3) + self.orders_of_magnitude[1] + self.hundreds(number%10**3, s=' ')
 
    def millions(self, number, s=''):
        if number == 0:
            return 0
        elif number%10**6 == 0:
            return s + self.hundreds(number//10**6) + self.orders_of_magnitude[2]
        elif number//10**3%10**3 == 0:
            return s + self.hundreds(number//10**6) + self.orders_of_magnitude[2] + ' ' + self.hundreds(number%10**6)
        else:
            return s + self.hundreds(number//10**6) + self.orders_of_magnitude[2] + self.thousands(number%10**6, s=' ')
 
    def billions(self, number):
        if number%10**9 == 0:
            return self.hundreds(number//10**9) + self.orders_of_magnitude[3]
        elif number//10**3%10**3 == 0 and number//10**6%10**3 == 0:
            return self.hundreds(number//10**9) + self.orders_of_magnitude[3] + self.hundreds(number%10**9, s=' ')
        else:
            return self.hundreds(number//10**9) + self.orders_of_magnitude[3] + self.millions(number%10**9, s=' ')
 
    def numberToWords(self, number):
        """        
        return english words representation of int number from 0 to max int
 
        number: int
        """
 
        len_of_number = len(str(number))
        if len_of_number >= 1 and len_of_number <= 3:
            if number == 0:
                return self.units(number)
            else:
                return self.hundreds(number)
        elif len_of_number >= 4 and len_of_number <= 6:
            return self.thousands(number)
        elif len_of_number >= 7 and len_of_number <= 9:
            return self.millions(number)
        else:
            return self.billions(number)