Created
October 9, 2016 15:33
-
-
Save Zedmor/c4af5637b9e0f2ac91e9c64e5dca255f to your computer and use it in GitHub Desktop.
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
#Uses python3 | |
import sys | |
import itertools | |
from random import randrange | |
def largest_number(a): | |
#write your code here | |
def numCmp(a,b): | |
''' | |
:param a: | |
:param b: a and b - strings of int | |
:return: if a >= b: true else false | |
''' | |
acp,bcp = list(a),list(b) | |
minlen = min(len(a),len(b)) | |
for i in range(minlen): | |
topa= acp.pop(0) | |
topb = bcp.pop(0) | |
if int(topa) != int(topb): return int(topa) > int(topb) | |
# return len(a)<=len(b) | |
if acp==bcp==[]: return False | |
if acp==[]: | |
try: | |
while bcp[-1] == '0': bcp = bcp[:-1] | |
except IndexError: | |
return False | |
for ll in bcp: | |
if int(ll)<int(a[0]): return True | |
return False | |
#if int(''.join(bcp))> int(a[0]) and bcp[0]!='0': return False | |
#else: return True | |
if bcp==[]: | |
try: | |
while acp[-1] == '0': acp = acp[:-1] | |
except IndexError: | |
return False | |
if acp == []: return False | |
for ll in acp: | |
if int(ll)<int(b[0]): return False | |
return True | |
# if int(''.join(acp))> int(b[0]) and acp[0]!='0': return True | |
# else: return False | |
def qsort(inlist): | |
if inlist == []: | |
return [] | |
else: | |
pivot = inlist[0] | |
lesser = qsort([x for x in inlist[1:] if numCmp(x,pivot)]) | |
greater = qsort([x for x in inlist[1:] if not(numCmp(x,pivot))]) | |
return lesser + [pivot] + greater | |
return qsort(a) | |
def RandTest(): | |
for m in range(500): | |
a = [] | |
for z in range(3): | |
ran = randrange(0, 500) | |
a.append(str(ran)) | |
# print(a) | |
# print(largest_number(a)) | |
z = list(map(''.join, list(itertools.permutations(a)))) | |
z = max(list(map(int, z))) | |
x = int(''.join(largest_number(a))) | |
if x != z: print("Failure", x, z, a) | |
if __name__ == '__main__': | |
# input = sys.stdin.read() | |
input = '3 109 202 342 130 1' | |
data = input.split() | |
a = data[1:] | |
# x = int(''.join(largest_number(a))) | |
x = largest_number(a) | |
RandTest() | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment