fork(1) download
  1. def f(d,w):
  2. a=[[w]]
  3. while a:b=a;a=[x+[y]for x in a for y in set(d)-set(x)if x[-1][-1]==y[0]]
  4. return b
  5.  
  6.  
  7. print f(['hello', 'turtle', 'eat', 'cat', 'people'], 'artistic')
  8. print f(['lemonade', 'meatball', 'egg', 'grape'], 'ham' )
  9. print f(['cat', 'cute', 'ewok'], 'attic')
  10. print f(['cat', 'cute', 'ewok', 'kilo', 'to', 'otter'], 'attic')
  11. print f(['cat', 'today', 'yoda', 'attic'], 'ferret')
  12. print f(['cancel', 'loitering', 'gnocchi', 'improv', 'vivic', 'child', 'despair', 'rat', 'tragic', 'chimney', 'rex', 'xylophone'], 'attic')
  13. print f(['cat', 'today', 'yoda', 'artistic', 'cute', 'ewok', 'kilo', 'to', 'otter'], 'attic')
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
[['artistic', 'cat', 'turtle', 'eat']]
[['ham', 'meatball', 'lemonade', 'egg', 'grape']]
[['attic', 'cute', 'ewok']]
[['attic', 'cute', 'ewok', 'kilo', 'otter']]
[['ferret', 'today', 'yoda', 'attic', 'cat']]
[['attic', 'child', 'despair', 'rat', 'tragic', 'cancel', 'loitering', 'gnocchi', 'improv', 'vivic', 'chimney'], ['attic', 'cancel', 'loitering', 'gnocchi', 'improv', 'vivic', 'child', 'despair', 'rat', 'tragic', 'chimney']]
[['attic', 'cat', 'today', 'yoda', 'artistic', 'cute', 'ewok', 'kilo', 'otter']]