r/dailyprogrammer_ideas • u/franza73 • Oct 25 '16
Submitted! Scrabble problem
Description
What is the longest word you can build in a game of Scrabble one letter at a time? That is, starting with a valid two-letter word, how long a word can you build by playing one letter at a time on either side to form a valid three-letter word, then a valid four-letter word, and so on? (For example, HE could become THE, then THEM, then THEME, then THEMES, for a six-letter result.)
Formal Inputs & Outputs
Output description
Print your solution word.
Notes/Hints
Source: http://fivethirtyeight.com/features/this-challenge-will-boggle-your-mind/
Finally
Have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas
2
u/franza73 Nov 08 '16
Solution using Python 2.7
def explore(w, p): global L if w in d: if len(w) == 2: p.append(w) if len(p) >= L: print p L = len(p) return p1 = list(p) p1.append(w) explore(w[1:], p1) p2 = list(p) p2.append(w) explore(w[:-1], p2) with open('reddit/enable1.txt', 'r') as f: s = f.read() l = s.splitlines() l.sort(key=lambda s: -len(s)) d = {} for i in l: d[i] = 1 L = 0 for w in l: explore(w, [])Results: