Created
May 10, 2015 07:58
-
-
Save Mattias-/b6ae45127dbc44997192 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
names = """ | |
audino bagon baltoy banette bidoof braviary bronzor carracosta charmeleon | |
cresselia croagunk darmanitan deino emboar emolga exeggcute gabite | |
girafarig gulpin haxorus heatmor heatran ivysaur jellicent jumpluff kangaskhan | |
kricketune landorus ledyba loudred lumineon lunatone machamp magnezone mamoswine | |
nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena porygon2 | |
porygonz registeel relicanth remoraid rufflet sableye scolipede scrafty seaking | |
sealeo silcoon simisear snivy snorlax spoink starly tirtouga trapinch treecko | |
tyrogue vigoroth vulpix wailord wartortle whismur wingull yamask | |
""" | |
starts = defaultdict(list) | |
for name in names.split(): | |
starts[name[0]].append(name) | |
def possible_next(w, used): | |
if w[-1] not in starts: | |
return [w] | |
#used.append(w) | |
used = used + [w] | |
maxlen = 0 | |
li = [] | |
for x in starts[w[-1]]: | |
if x not in used: | |
pn = possible_next(x, used) | |
if len(pn) > maxlen: | |
maxlen = len(pn) | |
li = pn | |
return [w] + li | |
maxlen = 0 | |
li = [] | |
for n in names.split(): | |
pn = possible_next(n, []) | |
if len(pn) >= maxlen: | |
maxlen = len(pn) | |
li = pn | |
print li | |
print len(li) | |
print ' '.join(li) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment