Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Determine the Order by jcg
#determine-the-order
#recursive
# idea : the first element of order must be one of the initials of words
# we compute it, compute a new list of words wihout this first element,
# and recursively compute the remaining of order
# possibly speed up using lists in place of strings, filtering, ...
def checkio(data) :
# base case : no data
if not data :
return ''
#general case
# we compute the candidates for first element of result
candidates = set((word [0] for word in data)) # initials of words
for initial in candidates.copy() : # for each candidate
for word in data : # for each word
if word[0] != initial : # not beginning with that candidate
if initial in word : # if candidate in word, at least one letter is before
candidates.remove(initial) # so it is not a candidate
break
# if several candidates, we use lexical order
first = min(candidates)
# now we compute a new list of words, removing all occurences of "first" in current list
new_data = []
for word in data :
word = ''.join(x for x in word if x != first) # only the letters different of "first"
if word : # if at least one letter
new_data.append(word) # add to the new list
# we concatenate "first" with result of calling checkio with the new list
return first + checkio(new_data)
#These "asserts" using only for self-checking and not necessary for auto-testing
May 7, 2014
Comments: