Skip to content

Instantly share code, notes, and snippets.

@jmichalicek
Last active December 10, 2015 01:18
Show Gist options
  • Save jmichalicek/4356916 to your computer and use it in GitHub Desktop.
Save jmichalicek/4356916 to your computer and use it in GitHub Desktop.
reddit dailyprogrammer exercises will live here
"""
The basic solution to http://www.reddit.com/r/dailyprogrammer/comments/149kec/1242012_challenge_114_easy_word_ladder_steps/
in Python. Neither of the bonuses at the moment
"""
from sys import argv
testword = argv[1]
# wordlist from http://pastebin.com/zY4Xt7iB
for word in open('114_wordlist.txt', 'r'):
difference = [i for i in zip(testword, word.strip()) if i[0] != i[1]]
if len(difference) == 1:
print word.strip()
"""
Solution to bonus 2 of http://www.reddit.com/r/dailyprogrammer/comments/149kec/1242012_challenge_114_easy_word_ladder_steps/
in Python.
Loops through the wordlist comparing each word in the list to the specified word. For each match, recursively calls
the function to do the same for the match, up to a specified depth, and appends the results. A depth of 3 is used per the exercise parameters.
"""
from sys import argv
def find_neighbors(testword, wordlist, depth):
"""Find the neighbors of a word in a list"""
if depth == 0:
return []
neighbors = []
for word in wordlist:
difference = [i for i in zip(testword, word.strip()) if i[0] != i[1]]
if len(difference) == 1:
neighbors.append(word.strip())
neighbors += find_neighbors(word, wordlist, depth-1)
return neighbors
testword = argv[1]
with open('114_wordlist.txt', 'r') as f:
words = f.readlines()
neighbors = find_neighbors(testword, words, 3)
neighbors = set(neighbors)
print len(neighbors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment