Skip to content
Snippets Groups Projects
Commit 73e78381 authored by Ziyan Lu's avatar Ziyan Lu
Browse files

main

parent 0676be3f
Branches
No related tags found
No related merge requests found
from functools import cmp_to_key
def compare_first_digit(a, b):
a_first_digit = int(str(a)[0])
b_first_digit = int(str(b)[0])
if a_first_digit < b_first_digit:
return 1
elif a_first_digit > b_first_digit:
return -1
else:
if len(str(a)) == 1 and len(str(b)) > 1:
return compare_first_digit(a, int(str(b)[1:]))
elif len(str(a)) > 1 and len(str(b)) == 1:
return compare_first_digit(int(str(a)[1:]), b)
elif len(str(a)) > 1 and len(str(b)) > 1:
return compare_first_digit(int(str(a)[1:]), int(str(b)[1:]))
else:
return 0
def largest_number(numbers):
sorted_numbers = sorted(numbers, key=cmp_to_key(compare_first_digit))
return int(''.join(map(str, sorted_numbers)))
numbers = [5, 2, 1, 937, 9354]
print(largest_number(numbers))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment