upd
This commit is contained in:
22
algorithms/labs/lab8/code/greedy/3.py
Normal file
22
algorithms/labs/lab8/code/greedy/3.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from functools import cmp_to_key
|
||||
|
||||
|
||||
def largest_number(nums):
|
||||
arr = list(map(str, nums))
|
||||
|
||||
def cmp(a, b):
|
||||
if a + b < b + a:
|
||||
return 1
|
||||
if a + b > b + a:
|
||||
return -1
|
||||
return 0
|
||||
|
||||
arr.sort(key=cmp_to_key(cmp))
|
||||
res = "".join(arr)
|
||||
return "0" if res[0] == "0" else res
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(largest_number([10, 2]))
|
||||
print(largest_number([3, 30, 34, 5, 9]))
|
||||
print(largest_number([0, 0]))
|
||||
Reference in New Issue
Block a user