38
\$\begingroup\$

Your task is to write a program that will output a readable list of every five letter words with the structure:

consonant - vowel - consonant - vowel - consonant

The output should be sorted alphabetically with one word per line and no words repeated twice. It can be lowercase or uppercase but not mixed. So the list could start and end like this:

babab  
babac  
babad  
...  
zyzyw  
zyzyx  
zyzyz 

Vowels are a-e-i-o-u-y, the other 20 English-alphabet letters are consonants.
Words don't have to be actual dictionary words.
Shortest code wins.

Note: A few years ago I stumbled upon a program on a university website that did exactly that. Turns out my first and last name fit the c-v-c-v-c constraint, and i'd been googling myself.

\$\endgroup\$
  • 7
    \$\begingroup\$ I see two things here. First, there are a ridiculous amount of words like this. How do you expect to test the entries? Also, there are many names that fit that constraint. The first that comes to mind is Jacob, although others like Lucas also fit \$\endgroup\$ – TrojanByAccident Jan 12 '17 at 12:10
  • 9
    \$\begingroup\$ @TrojanByAccident I believe the challenge requires all possible matching letter combinations, regardless of whether they are names/English words. "Words don't have to be actual dictionary words." \$\endgroup\$ – trichoplax Jan 12 '17 at 12:22
  • 3
    \$\begingroup\$ @wilks Relevant meta post -- Strict i/o formats aren't appreciated in this SE community. This challenge is focusing on generating c-v-c-v-c words, not inserting newlines between words, no? \$\endgroup\$ – JungHwan Min Jan 12 '17 at 14:41
  • 1
    \$\begingroup\$ @JungHwanMin Thanks for the link, first time here and I didn't know about this. I saw it as a readable and alphabetical list of all the c-v-c-v-c words. So I agree that upper or lowercase really doesn't matter, but I also think that one very long string or, say some nested arrays, whilst technically valid wouldn't be a great answer. \$\endgroup\$ – wilks Jan 12 '17 at 15:30
  • 2
    \$\begingroup\$ Having run a few of the upvoted answers to this question, they obviously do not comply with "The output should be incremental with each word on its own line". Has this requirement been relaxed and if so can you edit the question to reflect this? I reckon I could lose quite a few bytes if my answer wasn't restricted by the format. \$\endgroup\$ – ElPedro Jan 12 '17 at 18:33

45 Answers 45

2
\$\begingroup\$

Pyth, 25 23 bytes

s.n*F[K-GJ"aeiouy"JKJKb

In pseudocode:

'                       ' G, b = "abcdefghijklmnopqrstuvwxyz", "\n"
'         J"aeiouy"     ' J = "aeiouy"
'      K-GJ             ' K = "bcdfghjklmnpqrstvwxz"
's.n                    ' "".join( flatten(
'   *F                  '   reduce(cartesian_product,
'     [K           JKJKb'     [K,J,K,J,K,b]
'                       ' # b makes it so that every word ends with \n so that
'                       ' # flattening and concatenating automatically deals with
'                       ' # newlines

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Batch, 185 bytes

@set c=b c d f g h j k l m n p q r s t v w x z
@for %%i in (%c%)do @for %%j in (a e i o u y)do @for %%k in (%c%)do @for %%l in (a e i o u y)do @for %%m in (%c%)do @echo %%i%%j%%k%%l%%m

Batch is so verbose that it costs 4 bytes to put the vowels into a variable.

\$\endgroup\$
2
\$\begingroup\$

C#, 244 242 234 219 bytes

class P{static void Main(){string v="aeiouy",c="bcdfghjklmnpqrstvwxz";int i=0,j,k,l,m;for(;i<20;i++)for(j=0;j<6;j++)for(k=0;k<20;k++)for(l=0;l<6;l++)for(m=0;m<20;)System.Console.Write("\n"+c[i]+v[j]+c[k]+v[l]+c[m++]);}}

Ungolfed:

class P
{
    static void Main()
    {
        string v = "aeiouy", c = "bcdfghjklmnpqrstvwxz";
        int i=0, j, k, l, m;
        for (; i < 20; i++)
            for (j=0; j < 6; j++)
                for (k=0; k < 20; k++)
                    for (l=0; l < 6; l++)
                        for (m=0; m < 20; )
                            System.Console.Write("\n"+c[i]+v[j]+c[k]+v[l]+c[m++]);
    }
}

I doubt I can trim it any further. Done on C# 5, so no {c[i]}

\$\endgroup\$
  • \$\begingroup\$ @dim damn, you are right. Let me rewrite this... \$\endgroup\$ – Mr Scapegrace Jan 13 '17 at 13:03
  • \$\begingroup\$ Are you sure this can't be shortened with Linq? \$\endgroup\$ – VMAtm Jan 15 '17 at 13:04
2
\$\begingroup\$

T-SQL, 276

This answer assumes a query's result set is valid output.

WITH c AS(SELECT*FROM(VALUES('b'),('c'),('d'),('f'),('g'),('h'),('j'),('k'),('l'),('m'),('n'),('p'),('q'),('r'),('s'),('t'),('v'),('w'),('x'),('z'))c(_)),v AS(SELECT*FROM(VALUES('a'),('e'),('i'),('o'),('u'),('y'))v(_))SELECT c._+v._+b._+a._+d._ FROM c,v,c b,v a,c d ORDER BY 1

It works in SQL Server 2008 R2. I'm not sure currently where you'd be able to run it online given the number of rows returned, though.

One CTE definition holds the consonants from a derived table. The other holds the vowels from another derived table. Take the Cartesian product of three consonant CTEs and two vowel CTEs, and you can get an alphabetized list of the appropriately concatenated values.

\$\endgroup\$
  • \$\begingroup\$ Nice, just what I was thinking. If you restrict it to SQL 2016 or higher, you can save ~70 bytes by using STRING_SPLIT instead of VALUES. \$\endgroup\$ – BradC Aug 8 '19 at 13:39
1
\$\begingroup\$

Haskell, 93 bytes

c="bcdfghjklmnpqrstvwxz"
t=[[a,b]|a<-"aeiuoy",b<-c]
main=putStr$unlines[b:y++z|b<-c,y<-t,z<-t]

I have tried to slim the hardcoded lists of consonants and vowels down, but haven't been able to find something that saves bytes. This will output all possible five letter words in the order as stated in the post to stdout, being generated via list comprehensions.

\$\endgroup\$
1
\$\begingroup\$

Java, 347 bytes -.-

public class N{public static void main(String[]a){String c="bcdfghjklmnpqrstvwxz";String v="aeiouy";int[]i=new int[5],e=new int[5];e[0]=e[2]=e[4]=20;e[1]=e[3]=6;m:while(true){System.out.println(c.charAt(i[0])+v.charAt(i[1])+c.charAt(i[2])+v.charAt(i[3])+c.charAt(i[4]));int p=4;while(true){++i[p];if(i[p]<e[p])break;i[p]=0;if(p==0)break m;--p;}}}}
\$\endgroup\$
1
\$\begingroup\$

Informix-SQL, 254 bytes

an SQL version (IBM Informix dialect)

select x from table(list{'a','e','i','o','u','y'})(x)
into temp v;
select x from table(list{'b','c','d','f','g','h','j','k',
'l','m','n','p','q','r','s','t','v','w','x','z'})(x)
into temp c;
select k.x||j.x||i.x||v.x||c.x
from c,v,c i,v j,c k
order by 1
\$\endgroup\$
1
\$\begingroup\$

Brachylog v2, 11 bytes

Ḍ;Ẉj,Ḍ∋ᵐcẉ⊥

Try it online!

Pretty much the same as Fatalize's Brachylog v1 answer.

\$\endgroup\$
1
\$\begingroup\$

Zsh, 58 bytes

v=(a e i o u y)
c=(${${:-{b..z}}:|v})
echo $^c$^v$^c$^v$^c

Try it online!

${ :|foo} is set difference, and ${^foo} enables brace expansion over the contents of the array foo.


If newline-delimited is required, 64 bytes:

v=(a e i o u y)
c=(${${:-{b..z}}:|v})
<<<${(F):-$^c$^v$^c$^v$^c}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I saw the "shell" answer below & thought zsh array diffs & cross-products would work, you beat me to it \$\endgroup\$ – roblogic Sep 2 '19 at 6:26
1
\$\begingroup\$

Shell, 72 64

Try it online! (Thanks @Grimy)

echo {g..z}{a..f}{g..z}{a..f}{g..z}|tr b-z eiouybcdfghj-np-tvwxz


If it must be delimited by newlines (beware of the spaces) 69 bytes Try it online!:

echo {g..z}{a..f}{g..z}{a..f}{g..z}|tr \ b-z \\neiouybcdfghj-np-tvwxz


Original shell, 72 bytes (bash, ksh, zsh, a simple sh may not expand {..}) Try it online!

echo {a..t}{1..6}{a..t}{1..6}{a..t}|tr a-t1-6 bcdfghjklmnpqrstvwxzaeiouy

Less than half a second. Considering that a C equivalent runs in 0.228sec and a perl equivalent takes 2.1 seconds, this is Not slow (times reported as TiO measured them).

\$\endgroup\$
  • \$\begingroup\$ 64: echo {g..z}{a..f}{g..z}{a..f}{g..z}|tr b-z eiouybcdfghj-np-tvwxz \$\endgroup\$ – Grimmy Sep 2 '19 at 14:22
  • \$\begingroup\$ Thanks .. @Grimy \$\endgroup\$ – Isaac Sep 2 '19 at 16:14
0
\$\begingroup\$

C (gcc), 123 bytes

char w[6];f(n,m,c,p){for(n=457653;n<1<<24;p&&puts(w))for(m=n++,c=p=1;m;m/=26,c++)p*=c%2==!strchr("aeiouy",w[5-c]=m%26+97);}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 52 37 34 33 bytes

echo>,{5$'aeiouy'(-.~;[)u:97+i.26

Try it online!

-1 thanks to Adam's suggestion of u:97+i.26 to replace 26{.97|.a.

  • u:97+i.26 extract letters a..z from ascii alphabet a.
  • 'aeiouy'(-.~;[) subtract the vowels from a..z, and link ; that (the consonants) to the vowels, creating a boxed list consonants ; vowels.
  • 5$ extent that boxed list cyclically to 5 elements, yielding consonants ; vowels ; consonants ; vowels ; consonants.
  • { create the cross product
  • , flatten
  • > open the boxes to create regular strings
  • echo print the results
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 25 16 bytes

╩æ╩¼`┼]m▒ε*mÅ─yn

Try it online!

Definitely not optimal, but this challenge got me to actually implement some features of MathGolf that have been in TODO since forever, mainly string subtraction and vowels/consonants. Once it's up on TIO I'll update this answer. MathGolf has been updated, and I'll add this as a disclaimer that some features used in this answer are newer than the challenge itself.

To fetch consonants and vowels from the dictionary, I filtered out words in the dictionary files which were duplicates, and found that I had 3 duplicates within the top 256 words of the dictionary. I decided that that 2 of these slots should be replaced by the string of consonants and the string of vowels. I was unsure of where "y" should be placed, but decided to place it among the vowels.

The one thing that is not in place is cartesian product for strings. Since I have not defined the output format yet, this feature did not make it into the latest version of MathGolf.

Explanation

ʑ                 fetch dictionary word 145 ("bcdfghjklmnpqrstvwxz")
  ʬ               fetch dictionary word 172 ("aeiouy")
    `              duplicate the top two items
     ┼             duplicate second item from top and push to top
      ]            end array / wrap stack in array
                   stack is now ["<cons>", "<vow>", "<cons>", "<vow>", "<cons>"]
       m▒          map each element to a list of chars
         ε*        reduce list by multiplication (cartesian product)
           mÅ      explicit map using next 2 operators
             ─     flatten array
              y    join array without separator to string or number
               n   join array with newlines to a string
\$\endgroup\$
0
\$\begingroup\$

Jelly, 10 bytes

ØỴ,Øỵṁ5ŒpY

Try it online!

\$\endgroup\$
0
\$\begingroup\$

ink, 224 bytes

LIST V=(a),e,i,o,u,y
LIST C=(b),c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,z
~temp D=C
~temp E=V
~temp F=C
~temp G=V
~temp H=C
-(L){D}{E}{F}{G}{H}
~H++
{H:->L}
~H=C
~G++
{G:->L}
~G=V
~F++
{F:->L}
~F=C
~E++
{E:->L}
~E=V
~D++
{D:->L}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.