Home > Net >  Python return largest number formed from an array
Python return largest number formed from an array

Time:07-02

def printLargest(self,arr):
  return "".join(sorted(arr,reverse=True,key=lambda _:_*18))

Need Explaination for the lambda statement used.

Input:- 3 30 34 5 9

Output:- 9534330

CodePudding user response:

Python interprets _ as any another variable name. Mainly it's used for an unused variable, which is not the case here.

If you understand this better, you can use this:

lambda x: x*18

So it just multiplies the given number by 18.

This is then used to compare the values in the array to sort. As discussed in the comments of this answer, the multiplication here is used to detect that 3 should come before 30. If they are duplicated you get this:

333333333333333333
303030303030303030303030303030303030

So it catches on the second character that 3 should be before 30.

Just to explain why this code works, the input is an array of strings that represent a number. And in Python strings get compared character by character given its Unicode code, in this case reversed since that is given as True.

ord('9') #returns unicode value 57
ord('5') #returns unicode value 53

If the number starts with the same character then the next one gets compared until they are not equal, or one is longer. So that is why you get this order

9 > 5 > 34 > 3 > 30

And because of the reverse being True, this means that the largest numbers will always be as much to the left as they can be. And by doing this the largest number possible gets created.

My guess is that 18 is used to catch very large numbers but this is not completely correct.

Given this example (second number is 19 characters long)

input: '1', '11111111111111111110'
output: '11111111111111111101'
expected output: '11111111111111111110'

My proposed solution is to substitute 18 with the length of the longest string in the array like this:

  def printLargest(self,arr):
      max_length = len(max(arr, key=len))
      return "".join(sorted(arr,reverse=True,key=lambda x: x*max_length))

CodePudding user response:

Basically, input is array of strings. You are calling the Sorted method for this array.

The definition of Sorted method is as given below:

sorted(iterable, /, *, key=None, reverse=False)

Return a new sorted list from the items in iterable.

Has two optional arguments which must be specified as keyword arguments.

key specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower). The default value is None (compare the elements directly).

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

Reference

Part 1: sorted
Now, in the case of sorted(arr,reverse=True,key=lambda :*18)`. The parameter Key is a function which returns value for comparing the elements. Here, we are having a lambda function, which replicates the item 18 times and then sorts them in the reverse order.

  • input : ['3', '30', '34', '5', '9']

  • Output of lambda function: lambda :*18. Here _ corresponds to input parameter

333333333333333333
303030303030303030303030303030303030
343434343434343434343434343434343434
555555555555555555
999999999999999999
  • Output of sorted: ['9', '5', '34', '3', '30']

Part 2: Join method
Now, you are having them joined together, using join function. "".join(['9', '5', '34', '3', '30']) The output is: 9534330

  • Related