Custom Sort in Python

52 Views Asked by At

In Python,using custom sort,concatenate array elements such that the result will be greatest value from those elements.

T = int(input("Enter number of test cases: "))

for _ in range(T):
    N = int(input())
    array = list(map(int, input().split()))
    
    array = list(map(str, array))
    array.sort(key=lambda x: (x * 3), reverse=True if '0' in array else False)
    
    largest_num = ''.join(array)
    print(largest_num)

# it is not solving for all test cases.
1

There are 1 best solutions below

0
Peter Parker On
T = int(input("Enter number of test cases: "))

for _ in range(T):
    N = int(input())
    array = list(map(int, input().split()))

    array = list(map(str, array))
    
    
    def custom_sort(x):
    
  
        return x * 3 if '0' not in x else 0
    
    array.sort(key=custom_sort, reverse=True)
    
    largest_num = ''.join(array)
    print(largest_num)