Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Bigger Together by pokryshkin
from typing import List
import itertools
### brute force
def bigger_together(ints: List[int]) -> int:
data = [str(x) for x in ints]
data = [ int(''.join(x)) for x in tuple(itertools.permutations(data)) ]
Min = min(data)
Max = max(data)
return Max-Min
### to swap
def bigger_together(ints: List[int]) -> int:
data = [str(x) for x in ints]
# swapping until sorted in ascending order
sorted = False
while not sorted:
sorted = True
for n in range(len(data)-1):
current = data[n]
next = data[n+1]
if current + next > next + current:
data[n] = next
data[n+1] = current
sorted = False
Min = int(''.join(data))
# swapping until sorted in descending order
sorted = False
while not sorted:
sorted = True
for n in range(len(data)-1):
current = data[n]
next = data[n+1]
if current + next < next + current:
data[n] = next
data[n+1] = current
sorted = False
Max = int(''.join(data))
return Max - Min
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert bigger_together([1,2,3,4]) == 3087, "4321 - 1234"
assert bigger_together([1,2,3,4, 11, 12]) == 32099877, "43212111 - 11112234"
assert bigger_together([0, 1]) == 9, "10 - 01"
assert bigger_together([100]) == 0, "100 - 100"
print('Done! I feel like you good enough to click Check')
April 7, 2021