Sort an Array alphabetically when each number is converted into words | Variable to store the word form of units digit and up to twenty ; Variable to store the word form of tens digit ; Function to convert a two digit number to the word by using the above defined arrays ; If n is more than 19 , divide it ; If n is non - zero ; Function to print a given number in words ; Stores the word representation of the given number n ; Handles digits at ten millions and hundred millions places ; Handles digits at hundred thousands and one millions places ; Handles digits at thousands and tens thousands places ; Handles digit at hundreds places ; Call the above function to convert the number into words ; Function to sort the array according to the albhabetical order ; Vector to store the number in words with respective elements ; Inserting number in words with respective elements in vector pair ; Sort the vector , this will sort the pair according to the alphabetical order . ; Print the sorted vector content ; Driver code
one = [ " " , " one ▁ " , " two ▁ " , " three ▁ " , " four ▁ " , " five ▁ " , " six ▁ " , " seven ▁ " , " eight ▁ " , " nine ▁ " , " ten ▁ " , " eleven ▁ " , " twelve ▁ " , " thirteen ▁ " , " fourteen ▁ " , " fifteen ▁ " , " sixteen ▁ " , " seventeen ▁ " , " eighteen ▁ " , " nineteen ▁ " ] NEW_LINE ten = [ " " , " " , " twenty ▁ " , " thirty ▁ " , " forty ▁ " , " fifty ▁ " , " sixty ▁ " , " seventy ▁ " , " eighty ▁ " , " ninety ▁ " ] NEW_LINE def numToWords ( n , s ) : NEW_LINE INDENT st = " " NEW_LINE if ( n > 19 ) : NEW_LINE INDENT st += ten [ n // 10 ] + one [ n % 10 ] NEW_LINE DEDENT else : NEW_LINE INDENT st += one [ n ] NEW_LINE DEDENT if ( n ) : NEW_LINE INDENT st += s NEW_LINE DEDENT return st NEW_LINE DEDENT def convertToWords ( n ) : NEW_LINE INDENT out = " " NEW_LINE out += numToWords ( ( n // 10000000 ) , " crore ▁ " ) NEW_LINE out += numToWords ( ( ( n // 100000 ) % 100 ) , " lakh ▁ " ) NEW_LINE out += numToWords ( ( ( n // 1000 ) % 100 ) , " thousand ▁ " ) NEW_LINE out += numToWords ( ( ( n // 100 ) % 10 ) , " hundred ▁ " ) NEW_LINE if ( n > 100 and n % 100 ) : NEW_LINE INDENT out += " and ▁ " NEW_LINE DEDENT out += numToWords ( ( n % 100 ) , " " ) NEW_LINE return out NEW_LINE DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT vp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( ( convertToWords ( arr [ i ] ) , arr [ i ] ) ) ; NEW_LINE DEDENT vp . sort ( ) NEW_LINE for i in range ( len ( vp ) ) : NEW_LINE INDENT print ( vp [ i ] [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 10 , 102 , 31 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n ) NEW_LINE DEDENT