Jump to content

Find words with alternating vowels and consonants: Difference between revisions

m
syntax highlighting fixup automation
(J)
m (syntax highlighting fixup automation)
Line 14:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">V vowels = ‘aeiou’
 
V count = 0
Line 30:
count++
print(word.rjust(14), end' I count % 7 == 0 {"\n"} E ‘ ’)
print()</langsyntaxhighlight>
 
{{out}}
Line 48:
=={{header|Action!}}==
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/unixdict.txt unixdict.txt] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
<langsyntaxhighlight Actionlang="action!">BYTE FUNC IsVovel(CHAR c)
IF c='a OR c='e OR c='i OR c='o OR c='u THEN
RETURN (1)
Line 97:
 
FindWords(fname)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_words_with_alternating_vowels_and_consonants.png Screenshot from Atari 8-bit computer]
Line 112:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Strings.Fixed;
 
Line 144:
end loop;
Close (File);
end Find_Alternating;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># read the list of words and show words with alternating vowels and #
# consonants #
IF FILE input file;
Line 203:
close( input file );
print( ( newline, whole( alternating count, 0 ), " words of alternating vowels and consonants found", newline ) )
FI</langsyntaxhighlight>
{{out}}
<pre>
Line 225:
The handler here is case- and diacritical-insensitive and has an option to treat "y" as a vowel. With the text file located on the computer's startup disk, it performs the set task in just under a third of a second on my machine, which is probably good enough for such a silly task.
 
<langsyntaxhighlight lang="applescript">(*
With AppleScript's text item delimiters set to all the vowels, the 'text items' of a word with alternating
vowels and consonants are single-character strings (the consonants), with the possibility of an empty
Line 267:
local theText
set theText to (read file ((path to desktop as text) & "unixdict.txt") as «class utf8»)
findWordsWithAlternatingVowelsAndConsonants from theText without yAsVowel given minLength:10</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{"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"}</langsyntaxhighlight>
 
Or if you want to treat "y" as a vowel:
 
<langsyntaxhighlight lang="applescript">-- Task code:
local theText
set theText to (read file ((path to desktop as text) & "unixdict.txt") as «class utf8»)
findWordsWithAlternatingVowelsAndConsonants from theText with yAsVowel given minLength:10</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{"aboriginal", "apologetic", "bimolecular", "borosilicate", "calorimeter", "capacitate", "capacitive", "capitoline", "capitulate", "caricature", "cohomology", "colatitude", "coloratura", "colorimeter", "debilitate", "decelerate", "decolonize", "definitive", "degeneracy", "degenerate", "dehumidify", "deliberate", "demodulate", "denominate", "denotative", "depositary", "depository", "deregulate", "deregulatory", "derogatory", "desiderata", "desideratum", "dicotyledon", "dilapidate", "diminutive", "epigenetic", "facilitate", "generosity", "hemosiderin", "hereditary", "heretofore", "heterodyne", "hexadecimal", "homogenate", "hypotenuse", "inoperative", "judicatory", "judicature", "laboratory", "latitudinal", "latitudinary", "legitimacy", "legitimate", "lepidolite", "literature", "locomotive", "locomotory", "luminosity", "manipulate", "metabolite", "mineralogy", "monocotyledon", "musicology", "nicotinamide", "numerology", "oratorical", "paragonite", "paramilitary", "pejorative", "peridotite", "peripatetic", "polarimeter", "polymerase", "pyrimidine", "pyroxenite", "recitative", "recuperate", "regulatory", "rehabilitate", "rejuvenate", "remunerate", "repetitive", "repository", "reticulate", "revelatory", "savonarola", "similitude", "solicitude", "solidarity", "tananarive", "telekinesis", "teratogenic", "teratology", "topologize", "toxicology", "unilateral", "unimodular", "uninominal", "verisimilitude", "veterinary", "vocabulary"}</langsyntaxhighlight>
 
===Functional===
 
Listing 'alternating' words for both '''{a,e,i,o,u}''' and '''{a,e,i,o,u,y}''' interpretations of 'vowel':
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 613:
set my text item delimiters to dlm
return s
end unwords</langsyntaxhighlight>
{{Out}}
<pre>Assuming aeiou – 67 matches:
Line 666:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">words: read.lines relative "unixdict.txt"
vowels: [`a` `e` `i` `o` `u`]
alternatingVC?: function [w][
Line 685:
print word
]
]</langsyntaxhighlight>
 
{{out}}
Line 758:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">FileRead, db, % A_Desktop "\unixdict.txt"
vowels := {"a":1, "e":1, "i":1, "o":1, "u":1}, c := 0
 
Line 781:
}
MsgBox, 262144, , % result
return</langsyntaxhighlight>
{{out}}
<pre>aboriginal apologetic bimolecular borosilicate calorimeter capacitate capacitive capitoline capitulate
Line 793:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">( length( $1 ) >= 10 ) \
{
# have an appropriate length word
Line 815:
{
printf( "\n%d words with alternating vowels and consonants found\n", alternatingCount );
} # END</langsyntaxhighlight>
{{out}}
<pre>
Line 834:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 877:
fclose(fp);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 951:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cstdlib>
#include <fstream>
#include <iomanip>
Line 990:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 1,064:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Find words with alternating vowels and consonants. Nigel Galloway: January 20th., 2020
let vowels=set['a';'e';'i';'o';'u']
Line 1,071:
seq{use n=System.IO.File.OpenText("unixdict.txt") in while not n.EndOfStream do yield n.ReadLine()}
|>Seq.filter(fun g->g.Length>9 && fN g)|>Seq.iter(printfn "%s")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,145:
<code>group-by</code> is used to group words by vowels and consonants. For example, <code>"hello" [ "aeiou" member? ] group-by</code> produces a sequence like <code>{ 'h' 'e' 'll' 'o' }</code>. The number of elements in this grouping is the same as the length of the original word if and only if it has alternating vowels and consonants.
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: grouping.extras io.encodings.ascii io.files kernel math
prettyprint sequences ;
 
"unixdict.txt" ascii file-lines
[ length 9 > ] filter
[ dup [ "aeiou" member? ] group-by [ length ] same? ] filter .</langsyntaxhighlight>
{{out}}
<pre style="height:20em">
Line 1,226:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: vowel? ( c -- ? )
case
'a' of true endof
Line 1,263:
 
main
bye</langsyntaxhighlight>
 
{{out}}
Line 1,337:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim as string s, c
dim as boolean v = false, lv = false
dim as integer i, n, a=0
Line 1,362:
if a mod 10 = 0 then print
wend
close #1</langsyntaxhighlight>
{{out}}<pre>
aboriginal apologetic bimolecular borosilicate calorimeter capacitate capacitive capitoline capitulate caricature
Line 1,373:
</pre>
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,424:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,500:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bifunctor (bimap)
import Data.List.Split (chunksOf)
Line 1,536:
 
justifyLeft :: Int -> Char -> String -> String
justifyLeft n c s = take n (s <> replicate n c)</langsyntaxhighlight>
{{Out}}
<pre>67 matching words:
Line 1,560:
=={{header|J}}==
We're looking for words which have more than 9 letters and where any adjacent pair of letters has both a vowel and a consonant.
<langsyntaxhighlight Jlang="j"> >(#~ (*/@(2 ~:/\ e.&'aeiou')*9<#)@>) cutLF fread'unixdict.txt'
aboriginal
apologetic
Line 1,627:
unimodular
uninominal
verisimilitude</langsyntaxhighlight>
=={{header|JavaScript}}==
As a standard for an embedded language which may have no access to a file-system,
Line 1,635:
macOS osascript instance of a JSContext.
 
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,813:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>67 matches:
Line 1,838:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq"># input: an array
# output: if the $parity elements are all in $array then true, else false
def check($parity; $array):
Line 1,858:
| $word ;
 
alternating</langsyntaxhighlight>
{{out}}
Invocation example: jq -nrR -f program.jq unixdict.txt
Line 1,933:
=={{header|Julia}}==
See [[Alternade_words#Julia]] for the foreachword function.
<langsyntaxhighlight lang="julia">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)])
onlyevens(f, s) = !any(c -> f(c), s[1:2:length(s)]) && all(c -> f(c), s[2:2:length(s)])
Line 1,939:
 
foreachword("unixdict.txt", eitheronlyvowels, minlen = 10)
</langsyntaxhighlight>{{out}}
<pre>
Word source: unixdict.txt
Line 1,958:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 2,002:
(( $? )) && print "$REPLY"
done < ${dict}
</syntaxhighlight>
</lang>
{{out}}<pre>
aboriginal
Line 2,073:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">vowels = Characters["euioa"];
consonants = DeleteCases[Alphabet[], Alternatives @@ vowels];
dict = Once[Import["https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt"]];
Line 2,083:
Characters[StringTake[#, {2, -1, 2}]]]) || (ContainsAll[
consonants, Characters[StringTake[#, {1, -1, 2}]]] &&
ContainsAll[vowels, Characters[StringTake[#, {2, -1, 2}]]]) &]</langsyntaxhighlight>
{{out}}
<pre>{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|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
const Vowels = {'a', 'e', 'i', 'o', 'u'}
Line 2,103:
inc count
stdout.write word.align(14), if count mod 7 == 0: '\n' else: ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 2,121:
Used [https://metacpan.org/pod/App::a2p a2p].
{{trans|AWK}}
<langsyntaxhighlight Perllang="perl"># 20210104 added Perl programming solution
 
use strict;
Line 2,146:
}
 
printf "\n%d words with alternating vowels and consonants found\n", $alternatingCount;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,165:
=== Regex solution ===
I've heard rumors that there are more than 5 vowels...
<langsyntaxhighlight lang="perl">$vowels = 'aeiou';
$v = qr/[$vowels]/;
$c = qr/[^$vowels]/;
 
/^ ( ($c$v)+ $c? | ($v$c)+ $v? ) $/ix and say for grep { /.{10}/ } split "\n", do { (@ARGV, $/) = 'unixdict.txt'; <> };</langsyntaxhighlight>
{{out}}
<pre>aboriginal
Line 2,178:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">vowel</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"aeiou"</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 2,189:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">unix_dict</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">oddeven</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d avac words found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,196:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Put "c:\unixdict.txt" into a path.
Line 2,226:
If the string's last's target is any consonant, set another flag.
If the flag is the other flag, say yes.
Say no.</langsyntaxhighlight>
{{out}}
<pre style="height:20em">
Line 2,300:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">main:-
open("unixdict.txt", read, Stream),
main(Stream, 0),
Line 2,332:
alternating_vowels_and_consonants([Ch1, Ch2|Chars]):-
(vowel(Ch1) -> \+vowel(Ch2) ; vowel(Ch2)),
alternating_vowels_and_consonants([Ch2|Chars]).</langsyntaxhighlight>
 
{{out}}
Line 2,406:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Words with alternating vowels and consonants'''
 
 
Line 2,472:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>67 matching words:
Line 2,496:
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery">
<lang Quackery>
[ [] swap ]'[ swap
witheach
Line 2,560:
drop nest$
filter allcriteria
80 wrap$</langsyntaxhighlight>
 
{{out}}
Line 2,583:
Here's a bit of variety; the match-lambda for the regex version could also be simplified to regexp-match.
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define alternating?/re
Line 2,644:
(check-equal? words/re words/fold)
(check-equal? words/re words/loop)
(check-equal? words/re words/letrec))</langsyntaxhighlight>
 
{{out}}
Line 2,653:
Sigh. Yet another "Filter a word list" task. In a effort to make it a ''little'' more interesting, rather than just doing a one-liner, build a grammar and use that to filter.
 
<syntaxhighlight lang="raku" perl6line>grammar VOWCON {
token TOP { <|w> <vowel>? ( <consonant> <vowel> )* <consonant>? <|w> }
token vowel { <[aeiou]> }
Line 2,659:
}
 
say ( grep { VOWCON.parse: .lc }, grep { .chars > 9 }, 'unixdict.txt'.IO.words ).batch(6)».fmt('%-15s').join: "\n";</langsyntaxhighlight>
{{out}}
<pre>aboriginal apologetic bimolecular borosilicate calorimeter capacitate
Line 2,675:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds all the caseless words (within an identified dictionary) that */
/*──────────────────────────────────────── either have odd letters that are vowels, and */
/*──────────────────────────────────────── even letters that consonants, or vice versa.*/
Line 2,726:
end /*out*/
/*stick a fork in it, we're all done. */
if _\=='' then say substr(_, 2) /*Any residual words? Then show a line*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,744:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
cStr = read("unixdict.txt")
wordList = str2list(cStr)
Line 2,828:
 
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,907:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">VOWELS =%w(a e i o u).map(&:freeze)
 
res = File.open("unixdict.txt").each_line.select do |line|
Line 2,913:
word.size > 9 && word.chars.each_cons(2).all?{|c1, c2| VOWELS.include?(c1) != VOWELS.include?(c2) }
end
puts res</langsyntaxhighlight>
{{out}}
<pre style="height:30em">
Line 2,986:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$include "seed7_05.s7i";
 
const func boolean: vowel (in char: letter) is
Line 3,031:
close(dictionary);
end if;
end func;</langsyntaxhighlight>
{{out}}
<pre style="height:36em">
Line 3,104:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func isVowel(_ char: Character) -> Bool {
Line 3,127:
} catch {
print(error.localizedDescription)
}</langsyntaxhighlight>
 
{{out}}
Line 3,202:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "/fmt" for Fmt
 
Line 3,234:
Fmt.print("$2d: $s", count, word)
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,310:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \Use zero-terminated strings
int Cnt, I, Ch, Len;
char Word(100); \(longest word in unixdict.txt is 22 chars)
Line 3,349:
];
until Ch = EOF;
]</langsyntaxhighlight>
 
{{out}}
10,343

edits

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