Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Speedy category for The End of Other by jcg
def checkio(words_set):
# {"hello", "lo", "he"}
l = sorted(words_set, key=lambda x : x[::-1]) #sort by reverse lexical order and by size (in n log(n) if n is size of words_set )
# ['he', 'lo', 'hello']
for i in range(1,len(l)) : # just one loop => + n
if l[i].endswith(l[i-1]) : #if current is suffix of precedent
return True #
return False # worst : n log(n) . For ten words very faster than two nested loops (n**2)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio({"hello", "lo", "he"}) == True, "helLO"
assert checkio({"hello", "la", "hellow", "cow"}) == False, "hellow la cow"
assert checkio({"walk", "duckwalk"}) == True, "duck to walk"
assert checkio({"one"}) == False, "Only One"
Feb. 20, 2014
Comments: