-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlongest_number_formed.py
55 lines (34 loc) · 1.12 KB
/
longest_number_formed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Given a list of non negative integers, arrange them in such a manner that they form the largest number possible.
# The result is going to be very large, hence return the result in the form of a string.
# Input:
# The first line of input consists number of the test cases. The description of T test cases is as follows:
# The first line of each test case contains the size of the array, and the second line has the elements of the array.
# Output:
# In each separate line print the largest number formed by arranging the elements of the array in the form of a string.
# Constraints:
# 1 ≤ T ≤ 70
# 1 ≤ N ≤ 100
# 0 ≤ A[i] ≤ 1000
# Example:
# Input:
# 2
# 5
# 3 30 34 5 9
# 4
# 54 546 548 60
# Output:
# 9534330
# 6054854654
import functools
def compare(a,b):
ab = int(str(a)+str(b))
ba = int(str(b)+str(a))
return ba-ab
def largestNumber(A):
sorted_a = sorted(A,key=functools.cmp_to_key(compare))
return int("".join([str(i) for i in sorted_a]))
t = int(input())
for i in range(0,t):
n = int(input())
arr = [int(x) for x in input().strip().split(" ")]
print(largestNumber(arr))