Jump to content

Largest int from concatenated ints: Difference between revisions

Line 261:
 
===Python: Compare repeated string method===
<lang python>def maxnumxmaxnum(x):
maxlen = len(str(max(x)))
return ''.join(sorted((str(n) for n in x), reverse=True,
key=lambda i: i*(maxlen // len(i) + 1)))
 
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]:
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</lang>
 
;Output as above.
 
{{works with|Python|2.6+}}
<lang python>from fractions import Fraction
from math import log10
 
def maxnum(x):
return ''.join(str(n) for n in sorted(x, reverse=True,
key=lambda i: Fraction(i, 10**(int(log10(i))+1)-1)))
 
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]:
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.