Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Determine the Order by marcopunteri
def checkio(data):
letters = set([l for word in data for l in word ])
order_dict = {letter:set() for letter in letters}
for word in data:
for pos,letter in enumerate(word):
for l in [ w for w in word[pos+1:] if w!=letter]:
order_dict[letter].add(l)
res = ''
while order_dict:
last_letters = [letter for letter,values in order_dict.items() if values == set()]
letter = last_letters[0] if len(last_letters) == 1 else sorted(last_letters, reverse=True)[0]
res += letter
del order_dict[letter]
for key in order_dict:
order_dict[key].discard(letter)
return res[::-1]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
'''print(checkio(["acb", "bd", "zwa"]))
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"
April 21, 2021