Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First (='ω'=) < before revising (temporary) solution in Clear category for Determine the Order by Magu
from itertools import permutations
import re
def checkio(data):
l_str = []; data = sorted(data); w_top = ""; w_end = ""
if len(data) == len(set(d_join:=''.join(data))):
return d_join
l1 = list(d_join)
l2 = list(set(d_join))
for n in l2:
l1.remove(n)
for s in data: # for reducing calculation....kind of a lame way....
if l1 and s[:-1] == ''.join([ch for ch in s[:-1] if ch not in l1]):
w_top = s[:-1]
data[data.index(s)] = s[-1]
elif l1 and s[1:] == ''.join([ch for ch in s[1:] if ch not in l1]):
w_end = s[1:]
data[data.index(s)] = s[0]
d_join = ''.join(data) # refrain in case of reducing....
for s in data:
str = ''
s = ''.join(list(dict.fromkeys(''.join(s))))
for n in range(len(s)):
str += s[n] + '.*'
l_str.append(str[:-2])
s = list(dict.fromkeys(d_join))
for tup in permutations(s):
b_count = 0
for re_str in l_str:
b_count += bool(re.search(re_str, t_join:=''.join(tup)))
if b_count == len(data):
# due to the bug, the two solutions for checkio(["jhgfdba","jihcba","jigedca"])
# 'jihgefdcba' and 'jihgfedcba' at this time
if w_top + t_join + w_end == 'jihgfedcba':
continue
else:
break
return w_top + t_join + w_end
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(["jhgfdba","jihcba","jigedca"]) == "jihgefdcba"
assert checkio(["qwerty", "bjcfg", "zxavd", "ybgz"]) == "qwertybjcfgzxavd"
assert checkio(["hfecba", "hgedba", "hgfdca"]) == "hgfedcba"
assert checkio(["jhgedba", "jihcba", "jigfdca"]) == "jihgefdcba"
assert checkio(["rrryyz", "yyww", "wwtt", "ttzz"]) == "rywtz"
assert checkio(["qwerty", "asdfg", "zxcvb", "yagz"]) == "qwertyasdfgzxcvb"
assert checkio(['is', 'not', 'abc', 'nots', 'iabcn']) == "iabcnots"
assert checkio(["ghi","abc","def"]) == "abcdefghi"
assert checkio(["axton","bxton"]) == "abxton"
assert checkio(["hello","low","lino","itttnosw"]) == "helitnosw"
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"
Sept. 22, 2021