-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestnumber.py
More file actions
37 lines (29 loc) · 824 Bytes
/
largestnumber.py
File metadata and controls
37 lines (29 loc) · 824 Bytes
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
"""
Task:
Given a list of non negative integers, arrange them such that they form the
largest number. For example, given [3, 30, 34, 5, 9], the largest formed
number is 9534330.
>>> largest_number([3, 30, 34, 5, 9])
'9534330'
>>> largest_number([0, 0])
'0'
"""
from functools import cmp_to_key # compatible with python3
def largest_number(numbers):
numbers = sorted((str(i) for i in numbers),
key=cmp_to_key(my_cmp_func),
reverse=True)
result = ''.join(numbers).lstrip('0') # remove leading '0'
return result or '0' # incase empty str
def my_cmp_func(x, y):
"""
>>> my_cmp_func('2', '21')
1
"""
if x + y >= y + x:
return 1
else:
return -1
if __name__ == '__main__':
import doctest
doctest.testmod()