Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
27
Task/Word-wheel/Python/word-wheel-1.py
Normal file
27
Task/Word-wheel/Python/word-wheel-1.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import urllib.request
|
||||
from collections import Counter
|
||||
|
||||
|
||||
GRID = """
|
||||
N D E
|
||||
O K G
|
||||
E L W
|
||||
"""
|
||||
|
||||
|
||||
def getwords(url='http://wiki.puzzlers.org/pub/wordlists/unixdict.txt'):
|
||||
"Return lowercased words of 3 to 9 characters"
|
||||
words = urllib.request.urlopen(url).read().decode().strip().lower().split()
|
||||
return (w for w in words if 2 < len(w) < 10)
|
||||
|
||||
def solve(grid, dictionary):
|
||||
gridcount = Counter(grid)
|
||||
mid = grid[4]
|
||||
return [word for word in dictionary
|
||||
if mid in word and not (Counter(word) - gridcount)]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
chars = ''.join(GRID.strip().lower().split())
|
||||
found = solve(chars, dictionary=getwords())
|
||||
print('\n'.join(found))
|
||||
70
Task/Word-wheel/Python/word-wheel-2.py
Normal file
70
Task/Word-wheel/Python/word-wheel-2.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
'''Word wheel'''
|
||||
|
||||
from os.path import expanduser
|
||||
|
||||
|
||||
# gridWords :: [String] -> [String] -> [String]
|
||||
def gridWords(grid):
|
||||
'''The subset of words in ws which contain the
|
||||
central letter of the grid, and can be completed
|
||||
by single uses of some or all of the remaining
|
||||
letters in the grid.
|
||||
'''
|
||||
def go(ws):
|
||||
cs = ''.join(grid).lower()
|
||||
wheel = sorted(cs)
|
||||
wset = set(wheel)
|
||||
mid = cs[4]
|
||||
return [
|
||||
w for w in ws
|
||||
if 2 < len(w) and (mid in w) and (
|
||||
all(c in wset for c in w)
|
||||
) and wheelFit(wheel, w)
|
||||
]
|
||||
return go
|
||||
|
||||
|
||||
# wheelFit :: String -> String -> Bool
|
||||
def wheelFit(wheel, word):
|
||||
'''True if a given word can be constructed
|
||||
from (single uses of) some subset of
|
||||
the letters in the wheel.
|
||||
'''
|
||||
def go(ws, cs):
|
||||
return True if not cs else (
|
||||
False if not ws else (
|
||||
go(ws[1:], cs[1:]) if ws[0] == cs[0] else (
|
||||
go(ws[1:], cs)
|
||||
)
|
||||
)
|
||||
)
|
||||
return go(wheel, sorted(word))
|
||||
|
||||
|
||||
# -------------------------- TEST --------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Word wheel matches for a given grid in a copy of
|
||||
http://wiki.puzzlers.org/pub/wordlists/unixdict.txt
|
||||
'''
|
||||
print('\n'.join(
|
||||
gridWords(['NDE', 'OKG', 'ELW'])(
|
||||
readFile('~/unixdict.txt').splitlines()
|
||||
)
|
||||
))
|
||||
|
||||
|
||||
# ------------------------ GENERIC -------------------------
|
||||
|
||||
# readFile :: FilePath -> IO String
|
||||
def readFile(fp):
|
||||
'''The contents of any file at the path
|
||||
derived by expanding any ~ in fp.
|
||||
'''
|
||||
with open(expanduser(fp), 'r', encoding='utf-8') as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue