Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Project: Peter's Files
Views: 3893
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu1804
Kernel: Python 3 (system-wide)
import numpy as np import re
SouthWest = np.array([['l','b','a','g','s','f','l','y','f','r','e','e','s','d'], ['o','w','v','k','l','n','u','g','l','r','z','m','i','c'], ['w','i','n','t','e','r','n','a','t','i','o','n','a','l'], ['f','r','a','s','a','j','a','n','b','u','y','v','o','z'], ['a','w','u','v','k','z','x','b','s','u','c','d','g','a'], ['r','a','p','i','d','r','e','w','a','r','d','s','k','x'], ['e','t','i','o','a','f','f','g','h','s','r','y','t','a'], ['s','n','o','c','h','a','n','g','e','f','e','e','s','a']])
class word_search(object): def __init__(self, array): self.array = array self.height = len(array) self.width = len(array[0]) self.words = [] self.dictionary = [str[:-1] for str in open('dictionary.txt').readlines()] self.found = False def _check_word(self, word): if word != '': if word in self.dictionary: self.words += [word] if word[::-1] in self.dictionary: self.words += [word[::-1]] def find_words(self): for i in range(self.height): for j in range(self.width): # right for num_letters in range(1, self.width - j + 1): word = ''.join(self.array[i][j + k] for k in range(num_letters)) self._check_word(word) # down for num_letters in range(1, self.height - i + 1): word = ''.join(self.array[i + k][j] for k in range(num_letters)) self._check_word(word) # right - down diagonals for num_letters in range(min(self.width - j, self.height - i) + 1): word = ''.join(self.array[i + k][j + k] for k in range(num_letters)) self._check_word(word) # right - up diagonals for num_letters in range(min(i +1, self.width - j) + 1): word = ''.join(self.array[i - k][j + k] for k in range(num_letters)) self._check_word(word) # condense self.words = list(set(self.words)) self.words = sorted(self.words) self.found = True def print_words(self): if self.found: print(''.join(word + ', ' for word in self.words)[:-2]) else: print('run\`find_words()` first')
SW = word_search(SouthWest) SW.find_words() SW.print_words()
abase, ah, am, an, are, as, at, awe, axe, bag, bags, bar, base, be, biz, bud, buy, car, change, con, cons, cut, din, dip, do, dr, draw, drawer, drew, dub, ebb, eg, eh, els, em, era, err, errs, far, fare, fares, fee, fees, fly, free, frees, fro, gab, go, gun, ha, had, hang, has, he, ho, hop, id, if, im, in, inter, intern, international, ion, is, it, lea, leak, lie, low, luna, me, ms, nag, nation, national, ne, no, nu, oaf, odd, odds, oh, on, or, ow, pa, par, pi, rap, rapid, raw, re, reward, rewards, see, seer, sen, tan, tern, to, up, us, use, war, ward, wards, we, win, winter, ye, yrs
axe, con, fees, gun, hang, leak, oaf, war