Find words with alternating vowels and consonants: Difference between revisions
Content deleted Content added
m →{{header|Haskell}}: Pruned out an unused import. |
→{{header|Python}}: Added a draft in Python. |
||
Line 827: | Line 827: | ||
verisimilitude |
verisimilitude |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
Is it too late to fix the slightly ill-formed title of this task ? |
|||
One word is missing, grammatically, but it's already rather too long. |
|||
<lang python>'''Words with no successive vowels or consonants''' |
|||
# isLongAlternator :: String -> Bool |
|||
def isLongAlternator(s): |
|||
'''True if s has 10 or more characters, |
|||
and no two successive characters that are |
|||
both vowels, or both consonants. |
|||
''' |
|||
def p(a, b): |
|||
return isVowel(a) != isVowel(b) |
|||
return 9 < len(s) and all(map(p, s, s[1:])) |
|||
def isVowel(c): |
|||
'''True if c is in the set {a, e, i, o, u} |
|||
''' |
|||
return c in 'aeiou' |
|||
# ------------------------- TEST ------------------------- |
|||
# main :: IO () |
|||
def main(): |
|||
'''Words without successive vowels or consonants |
|||
found in the file 'unixdict.txt' |
|||
''' |
|||
matches = [ |
|||
x for x in readFile('unixdict.txt').splitlines() |
|||
if isLongAlternator(x) |
|||
] |
|||
w = max(len(x) for x in matches) |
|||
print(str(len(matches)) + ' matching words:\n') |
|||
print( |
|||
'\n'.join( |
|||
' '.join(s) for s in chunksOf(4)([ |
|||
x.ljust(w, ' ') for x in matches |
|||
]) |
|||
) |
|||
) |
|||
# ----------------------- GENERIC ------------------------ |
|||
# chunksOf :: Int -> [a] -> [[a]] |
|||
def chunksOf(n): |
|||
'''A series of lists of length n, subdividing the |
|||
contents of xs. Where the length of xs is not evenly |
|||
divible, the final list will be shorter than n. |
|||
''' |
|||
def go(xs): |
|||
return ( |
|||
xs[i:n + i] for i in range(0, len(xs), n) |
|||
) if 0 < n else None |
|||
return go |
|||
# justifyLeft :: Int -> Char -> String -> String |
|||
def justifyLeft(n): |
|||
'''A string padded at right to length n, |
|||
using the padding character c. |
|||
''' |
|||
return lambda c: lambda s: s.ljust(n, c) |
|||
# readFile :: FilePath -> IO String |
|||
def readFile(fp): |
|||
'''The contents of any file at the path. |
|||
''' |
|||
with open(fp, 'r', encoding='utf-8') as f: |
|||
return f.read() |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</lang> |
|||
{{Out}} |
|||
<pre>67 matching words: |
|||
aboriginal apologetic bimolecular borosilicate |
|||
calorimeter capacitate capacitive capitoline |
|||
capitulate caricature colatitude coloratura |
|||
colorimeter debilitate decelerate decolonize |
|||
definitive degenerate deliberate demodulate |
|||
denominate denotative deregulate desiderata |
|||
desideratum dilapidate diminutive epigenetic |
|||
facilitate hemosiderin heretofore hexadecimal |
|||
homogenate inoperative judicature latitudinal |
|||
legitimate lepidolite literature locomotive |
|||
manipulate metabolite nicotinamide oratorical |
|||
paragonite pejorative peridotite peripatetic |
|||
polarimeter recitative recuperate rehabilitate |
|||
rejuvenate remunerate repetitive reticulate |
|||
savonarola similitude solicitude tananarive |
|||
telekinesis teratogenic topologize unilateral |
|||
unimodular uninominal verisimilitude</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |