Sort numbers based on count of letters required to represent them in words | letters [ i ] stores the count of letters required to represent the digit i ; Function to return the sum of letters required to represent N ; Function to sort the array according to the sum of letters to represent n ; List to store the digit sum with respective elements ; Inserting digit sum with elements in the list pair ; Making the vector pair ; Sort the list , this will sort the pair according to the sum of letters to represent n ; Print the sorted list content ; Driver code
letters = [ 4 , 3 , 3 , 3 , 4 , 4 , 3 , 5 , 5 , 4 ] NEW_LINE def sumOfLetters ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += letters [ n % 10 ] NEW_LINE n = n // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT vp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( [ sumOfLetters ( arr [ i ] ) , arr [ i ] ] ) NEW_LINE DEDENT vp . sort ( key = lambda x : x [ 0 ] ) NEW_LINE for i in vp : NEW_LINE INDENT print ( i [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 10 , 31 , 18 ] NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n ) NEW_LINE DEDENT