Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Determine the Order by m.kleinkranenbarg
from functools import cmp_to_key
from collections import defaultdict
def checkio(data):
def presort(letters):
result = []
for word in sorted(data, key=len, reverse=True):
for letter in word:
if not letter in result:
result.append(letter)
return result
def cmp(boolean):
return -1 if boolean else 1
def compare(c1, c2):
words1 = {word for word in data if c1 in word}
words2 = {word for word in data if c2 in word}
both = words1 & words2
if both:
word = both.pop()
return cmp(word.index(c1) < word.index(c2))
for word1 in words1:
others1 = set(word1.replace(c1, ''))
for word2 in words2:
others2 = set(word2.replace(c2, ''))
both = others1 & others2
while both:
char = both.pop()
cmp1 = word1.index(char) < word1.index(c1)
cmp2 = word2.index(char) < word2.index(c2)
if cmp1 != cmp2:
return cmp(cmp2)
return cmp(c1 < c2)
letters = presort(set().union(letter for word in data for letter in word))
result = ''.join(sorted(letters, key=cmp_to_key(compare)))
return result
# These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == "__main__":
assert checkio(["acb", "bd", "zwa"]) == "zwacbd", "Just concatenate it"
assert checkio(["klm", "kadl", "lsm"]) == "kadlsm", "Paste in"
assert (
checkio(["a", "b", "c"]) == "abc"
), "Cant determine the order - use english alphabet"
assert checkio(["aazzss"]) == "azs", "Each symbol only once"
assert checkio(["dfg", "frt", "tyg"]) == "dfrtyg", "Concatenate and paste in"
Dec. 3, 2022