Find words with alternating vowels and consonants: Difference between revisions

Content deleted Content added
Chunes (talk | contribs)
Add Plain English
Wherrera (talk | contribs)
Line 342: Line 342:


=={{header|Julia}}==
=={{header|Julia}}==
See [[Alternade_words#Julia] for the foreachword function.
There have been a <i>lot</i> of "filter the dictionary" tasks almost identical to this one added to the wiki already. All call for filtering a word list, where the results of the filtering test depend on the characters in the word and its length, and may optionally also depend on what other words are in the same dictionary. Something like the generic function below can be used in most of these tasks.
<lang julia>isvowel(c) = c in ['a', 'e', 'i', 'o', 'u'] # NB. leaves out 'α' and similar unicode vowels, and what about y?
<lang julia>function foreachword(wordfile::String, condition::Function; minlen = 0, colwidth = 15, numcols = 6, toshow = 0)
println("Word source: $wordfile\n")
words = split(read(wordfile, String), r"\s+")
dict, shown = Dict(w => 1 for w in words), 0
for word in words
if (output = condition(word, dict)) != "" && (minlen < 1 || length(word) >= minlen)
shown += 1
print(rpad(output, colwidth), shown % numcols == 0 ? "\n" : "")
toshow > 0 && toshow < shown && break
end
end
end

isvowel(c) = c in ['a', 'e', 'i', 'o', 'u'] # NB. leaves out 'α' and similar unicode vowels, and what about y?
onlyodds(f, s) = all(c -> f(c), s[1:2:length(s)]) && !any(c -> f(c), s[2:2:length(s)])
onlyodds(f, s) = all(c -> f(c), s[1:2:length(s)]) && !any(c -> f(c), s[2:2:length(s)])
onlyevens(f, s) = !any(c -> f(c), s[1:2:length(s)]) && all(c -> f(c), s[2:2:length(s)])
onlyevens(f, s) = !any(c -> f(c), s[1:2:length(s)]) && all(c -> f(c), s[2:2:length(s)])