Jump to content

Odd words: Difference between revisions

→‎{{header|Python}}: Added a functionally composed variant
(Added Python implementation)
(→‎{{header|Python}}: Added a functionally composed variant)
Line 826:
 
=={{header|Python}}==
===Procedural===
This implementation prints only the unique odd words which appear in unixdict.txt unlike other implementations on this page which print some words twice. The minimum base word length has to be 9 since the problem statement says that odd word lengths must be greater than 4.
 
Line 851 ⟶ 852:
</lang>
{{out}}
<pre>brain
brain
cider
cried
Line 863:
slain
spree
trial</pre>
 
</pre>
===Functional===
Defining a dictionary of oddWords, keyed by their sources:
<lang python>'''Odd words'''
 
from os.path import expanduser
from json import dumps
 
 
# oddPairs :: Int -> [String] -> Dict
def oddPairs(minLength):
'''A dictionary of (word::oddWord) pairs
in which the words are drawn from a given wordList,
and the oddWords have at least a minimum length.
'''
def go(wordList):
lexicon = set(wordList)
allPairs = [(w, w[::2]) for w in wordList]
return {
k: v for k, v in allPairs
if minLength <= len(v) and v in lexicon
}
return go
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Odd words of 5 or more characters, paired with their
sources, which are drawn from a local unixdict.txt
'''
print(dumps(
oddPairs(5)(
readFile('~/unixdict.txt').split('\n')
),
indent=4
))
 
 
# ----------------------- 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()</lang>
{{Out}}
<pre>{
"barbarian": "brain",
"childbear": "cider",
"corrigenda": "cried",
"gargantuan": "grata",
"headdress": "hades",
"palladian": "plain",
"propionate": "point",
"salvation": "slain",
"siltation": "slain",
"slingshot": "sight",
"statuette": "saute",
"supersede": "spree",
"supervene": "spree",
"terminable": "trial"
}</pre>
 
=={{header|Quackery}}==
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.