Created
July 29, 2015 19:34
-
-
Save viveksyngh/02ac94219d4bd004b799 to your computer and use it in GitHub Desktop.
Largest number that can be formed using list of non negative integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
def comp(X, Y) : | |
""" | |
:param: X, Y two integers | |
:return : -1, 0 or 1 | |
""" | |
XY = str(X) + str(Y) | |
YX = str(Y) + str(X) | |
if XY < YX : | |
return -1 | |
if XY == YX : | |
return 0 | |
if XY > YX : | |
return 1 | |
def largestNumber(A) : | |
""" | |
:param: tuple of non negative integers | |
:return: Largest number that can be formed using integers | |
""" | |
A.sort(cmp=comp, reverse=True) | |
if max(A) == 0 : | |
return 0 | |
return ''.join(str(i) for i in A) | |
print(largestNumber([3, 30, 34, 5, 9])) #Will Return 9534330 | |
print(largestNumber([0, 0, 0, 0, 0])) #Will return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment