Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
functools.cmp_to_key solution in Clear category for Bigger Together by fokusd
from functools import cmp_to_key
def bigger_together(ints):
"""
Returns difference between the largest and smallest values
that can be obtained by concatenating the integers together.
"""
def num_comp(x, y):
return int(f'{x}{y}') - int(f'{y}{x}')
min_arr = sorted(ints, key=cmp_to_key(num_comp))
max_arr = min_arr[::-1]
return int(''.join(map(str, max_arr))) - int(''.join(map(str, min_arr)))
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')
Sept. 18, 2018