Print all numbers up to N in words in lexicographical order | Function to convert a number to words ; Stores the digits ; Base cases ; Stores strings of unit place ; Stores strings for corner cases ; Stores strings for ten 's place digits ; Stores strings for powers of 10 ; If given number contains a single digit ; Iterate over all the digits ; Represent first 2 digits in words ; Represent last 2 digits in words ; Explicitly handle corner cases [ 10 , 19 ] ; Explicitly handle corner case 20 ; For rest of the two digit numbers i . e . , 21 to 99 ; Function to print all the numbers up to n in lexicographical order ; Convert all numbers in words ; Sort all strings ; Print answer ; Driver Code
def convert_to_words ( n ) : NEW_LINE INDENT num = str ( n ) NEW_LINE length = len ( num ) NEW_LINE ans = " " NEW_LINE if ( length == 0 ) : NEW_LINE INDENT ans += " Empty ▁ String " NEW_LINE return ans NEW_LINE DEDENT single_digits = [ " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " ] NEW_LINE two_digits = [ " " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " ] NEW_LINE tens_multiple = [ " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " ] NEW_LINE tens_power = [ " hundred " , " thousand " ] NEW_LINE if ( length == 1 ) : NEW_LINE INDENT ans += single_digits [ ord ( num [ 0 ] ) - ord ( '0' ) ] NEW_LINE return ans NEW_LINE DEDENT x = 0 NEW_LINE while ( x < len ( num ) ) : NEW_LINE INDENT if ( length >= 3 ) : NEW_LINE INDENT if ( num [ x ] - '0' != 0 ) : NEW_LINE INDENT ans += single_digits [ ord ( num [ x ] ) - ord ( '0' ) ] NEW_LINE ans += " ▁ " NEW_LINE ans += tens_power [ len - 3 ] NEW_LINE ans += " ▁ " NEW_LINE DEDENT length -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( ord ( num [ x ] ) - ord ( '0' ) == 1 ) : NEW_LINE INDENT sum = ( ord ( num [ x ] ) - ord ( '0' ) + ord ( num [ x ] ) - ord ( '0' ) ) NEW_LINE ans += two_digits [ sum ] NEW_LINE return ans NEW_LINE DEDENT elif ( ord ( num [ x ] ) - ord ( '0' ) == 2 and ord ( num [ x + 1 ] ) - ord ( '0' ) == 0 ) : NEW_LINE INDENT ans += " twenty " NEW_LINE return ans NEW_LINE DEDENT else : NEW_LINE INDENT i = ( ord ( num [ x ] ) - ord ( '0' ) ) NEW_LINE if ( i > 0 ) : NEW_LINE INDENT ans += tens_multiple [ i ] NEW_LINE ans += " ▁ " NEW_LINE DEDENT else : NEW_LINE ans += " " NEW_LINE x += 1 NEW_LINE if ( ord ( num [ x ] ) - ord ( '0' ) != 0 ) : NEW_LINE INDENT ans += single_digits [ ord ( num [ x ] ) - ord ( '0' ) ] NEW_LINE DEDENT DEDENT DEDENT x += 1 NEW_LINE DEDENT return " " NEW_LINE DEDENT def lexNumbers ( n ) : NEW_LINE INDENT s = [ ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT s . append ( convert_to_words ( i ) ) NEW_LINE DEDENT s . sort ( ) NEW_LINE ans = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans . append ( s [ i ] ) NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT print ( ans [ i ] , end = " , ▁ " ) NEW_LINE DEDENT print ( ans [ n - 1 ] , end = " " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE lexNumbers ( n ) NEW_LINE DEDENT