Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
12
Task/Anagrams/Python/anagrams-1.py
Normal file
12
Task/Anagrams/Python/anagrams-1.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
>>> import urllib.request
|
||||
>>> from collections import defaultdict
|
||||
>>> words = urllib.request.urlopen('http://wiki.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
>>> anagram = defaultdict(list) # map sorted chars to anagrams
|
||||
>>> for word in words:
|
||||
anagram[tuple(sorted(word))].append( word )
|
||||
|
||||
|
||||
>>> count = max(len(ana) for ana in anagram.values())
|
||||
>>> for ana in anagram.values():
|
||||
if len(ana) >= count:
|
||||
print ([x.decode() for x in ana])
|
||||
25
Task/Anagrams/Python/anagrams-2.py
Normal file
25
Task/Anagrams/Python/anagrams-2.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
>>> import urllib
|
||||
>>> from collections import defaultdict
|
||||
>>> words = urllib.urlopen('http://wiki.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
>>> len(words)
|
||||
25104
|
||||
>>> anagram = defaultdict(list) # map sorted chars to anagrams
|
||||
>>> for word in words:
|
||||
anagram[tuple(sorted(word))].append( word )
|
||||
|
||||
|
||||
>>> count = max(len(ana) for ana in anagram.itervalues())
|
||||
>>> for ana in anagram.itervalues():
|
||||
if len(ana) >= count:
|
||||
print ana
|
||||
|
||||
|
||||
['angel', 'angle', 'galen', 'glean', 'lange']
|
||||
['alger', 'glare', 'lager', 'large', 'regal']
|
||||
['caret', 'carte', 'cater', 'crate', 'trace']
|
||||
['evil', 'levi', 'live', 'veil', 'vile']
|
||||
['elan', 'lane', 'lean', 'lena', 'neal']
|
||||
['abel', 'able', 'bale', 'bela', 'elba']
|
||||
>>> count
|
||||
5
|
||||
>>>
|
||||
22
Task/Anagrams/Python/anagrams-3.py
Normal file
22
Task/Anagrams/Python/anagrams-3.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
>>> import urllib, itertools
|
||||
>>> words = urllib.urlopen('http://wiki.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
>>> len(words)
|
||||
25104
|
||||
>>> anagrams = [list(g) for k,g in itertools.groupby(sorted(words, key=sorted), key=sorted)]
|
||||
|
||||
|
||||
>>> count = max(len(ana) for ana in anagrams)
|
||||
>>> for ana in anagrams:
|
||||
if len(ana) >= count:
|
||||
print ana
|
||||
|
||||
|
||||
['abel', 'able', 'bale', 'bela', 'elba']
|
||||
['caret', 'carte', 'cater', 'crate', 'trace']
|
||||
['angel', 'angle', 'galen', 'glean', 'lange']
|
||||
['alger', 'glare', 'lager', 'large', 'regal']
|
||||
['elan', 'lane', 'lean', 'lena', 'neal']
|
||||
['evil', 'levi', 'live', 'veil', 'vile']
|
||||
>>> count
|
||||
5
|
||||
>>>
|
||||
111
Task/Anagrams/Python/anagrams-4.py
Normal file
111
Task/Anagrams/Python/anagrams-4.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
'''Largest anagram groups found in list of words.'''
|
||||
|
||||
from os.path import expanduser
|
||||
from itertools import groupby
|
||||
from operator import eq
|
||||
|
||||
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Largest anagram groups in local unixdict.txt'''
|
||||
|
||||
print(unlines(
|
||||
largestAnagramGroups(
|
||||
lines(readFile('unixdict.txt'))
|
||||
)
|
||||
))
|
||||
|
||||
|
||||
# largestAnagramGroups :: [String] -> [[String]]
|
||||
def largestAnagramGroups(ws):
|
||||
'''A list of the anagram groups of
|
||||
of the largest size found in a
|
||||
given list of words.
|
||||
'''
|
||||
|
||||
# wordChars :: String -> (String, String)
|
||||
def wordChars(w):
|
||||
'''A word paired with its
|
||||
AZ sorted characters
|
||||
'''
|
||||
return (''.join(sorted(w)), w)
|
||||
|
||||
groups = list(map(
|
||||
compose(list)(snd),
|
||||
groupby(
|
||||
sorted(
|
||||
map(wordChars, ws),
|
||||
key=fst
|
||||
),
|
||||
key=fst
|
||||
)
|
||||
))
|
||||
|
||||
intMax = max(map(len, groups))
|
||||
return list(map(
|
||||
compose(unwords)(curry(map)(snd)),
|
||||
filter(compose(curry(eq)(intMax))(len), groups)
|
||||
))
|
||||
|
||||
|
||||
# GENERIC -------------------------------------------------
|
||||
|
||||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
def compose(g):
|
||||
'''Right to left function composition.'''
|
||||
return lambda f: lambda x: g(f(x))
|
||||
|
||||
|
||||
# curry :: ((a, b) -> c) -> a -> b -> c
|
||||
def curry(f):
|
||||
'''A curried function derived
|
||||
from an uncurried function.'''
|
||||
return lambda a: lambda b: f(a, b)
|
||||
|
||||
|
||||
# fst :: (a, b) -> a
|
||||
def fst(tpl):
|
||||
'''First member of a pair.'''
|
||||
return tpl[0]
|
||||
|
||||
|
||||
# lines :: String -> [String]
|
||||
def lines(s):
|
||||
'''A list of strings,
|
||||
(containing no newline characters)
|
||||
derived from a single new-line delimited string.'''
|
||||
return s.splitlines()
|
||||
|
||||
|
||||
# from os.path import expanduser
|
||||
# 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()
|
||||
|
||||
|
||||
# snd :: (a, b) -> b
|
||||
def snd(tpl):
|
||||
'''Second member of a pair.'''
|
||||
return tpl[1]
|
||||
|
||||
|
||||
# unlines :: [String] -> String
|
||||
def unlines(xs):
|
||||
'''A single string derived by the intercalation
|
||||
of a list of strings with the newline character.'''
|
||||
return '\n'.join(xs)
|
||||
|
||||
|
||||
# unwords :: [String] -> String
|
||||
def unwords(xs):
|
||||
'''A space-separated string derived from
|
||||
a list of words.'''
|
||||
return ' '.join(xs)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue