Odd words: Difference between revisions

Content added Content deleted
(Added Python implementation)
(→‎{{header|Python}}: Added a functionally composed variant)
Line 826: Line 826:


=={{header|Python}}==
=={{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.
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: Line 852:
</lang>
</lang>
{{out}}
{{out}}
<pre>
<pre>brain
brain
cider
cider
cried
cried
Line 863: Line 863:
slain
slain
spree
spree
trial
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}}==
=={{header|Quackery}}==