Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recursive solution in Clear category for Determine the Order by Leonix
def checkio(data):
""" Determine first letter of the alphabet, then call self recursively to build the rest """
# No input words - empty alphabet. This ends the recursion.
if not data:
return ""
# First letter in alphabet can not occur in position other than the first in any word
rejected = set()
candidates = set()
for word in data:
candidates.add(word[0])
rejected.update(set(word[1:]) - set([word[0]]))
candidates.difference_update(rejected)
# Among several matching candidates, order according to english alphabet
first_alphabet_letter = min(candidates)
# remove letter we found from all words to prepare data for recursive call
new_data = []
for word in data:
word = word.lstrip(first_alphabet_letter)
if word:
new_data.append(word)
return first_alphabet_letter + checkio(new_data)
April 4, 2019
Comments: