Skip to content

Instantly share code, notes, and snippets.

@mikhail-01
Last active June 2, 2018 17:26
Show Gist options
  • Save mikhail-01/512f5a072a49d50929fdbf3ee2372952 to your computer and use it in GitHub Desktop.
Save mikhail-01/512f5a072a49d50929fdbf3ee2372952 to your computer and use it in GitHub Desktop.
Решение задачи 99 на UniLecs
"""
Задача 99 на UniLecs
Решение. Сортируем массив с использованием функции сравнения двух элементов,
которая перед сравнением строк разной длины взаимно дополняет их друг другом.
"""
from functools import cmp_to_key
def cmp_elems(x, y):
if len(x) != len(y): x, y = x+y, y+x
return int(y) - int(x)
def solve(arr):
return "".join(sorted(arr, key=cmp_to_key(cmp_elems)))
test = [ ["123", "124", "56", "90"],
["1", "9", "950", "96", "960", "940", "9000", "1", "2"],
["5", "56", "560", "570", "550", "540", "7", "22"] ]
for arr in test:
print(f'arr = {arr}\nAnswer = {solve(arr)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment