Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Last active January 28, 2020 18:19
Show Gist options
  • Save iKlotho/6664129085ac12e7a79d to your computer and use it in GitHub Desktop.
Save iKlotho/6664129085ac12e7a79d to your computer and use it in GitHub Desktop.
blog.svpino.com Solution to Problem 4 Python
#Write a function that given a list of non negative integers, arranges them such that they form the largest possible number.
#For example, given [50, 2, 1, 9], the largest formed number is 95021
# question url https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
numbers = [5, 51, 56, 50]
result = ''
def check(number):
if len(str(number)) == 1:
return int(str(number)*2) # if number = 2 then return 22
else:
return number
for i in range(len(numbers)): # reverse sort for number list
for j in range(len(numbers)-1-i):
if check(numbers[j]) < check(numbers[j+1]):
numbers[j],numbers[j+1] = numbers[j+1],numbers[j]
for num in numbers:
result += str(num)
print result
#result = 5655150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment