-1
$\begingroup$

Let vowels:= {"A","E","I","O","U"}

Let alphabet be some CharacterRange, i.e.:

CharacterRange["A","F"]

or even

CharacterRange["A","Z"]

Now, is it possible to define a function fun[n_], such that:

f[1]=

Map[StringJoin[#] &, Select[
   Map[# &, Tuples[alphabet, 1]],
   (MemberQ[vowels, #[[1]]])
    &]] // Length

f[2]=

Map[StringJoin[#] &, Select[
   Map[# &, Tuples[alphabet, 2]],
   (MemberQ[vowels, #[[1]]] &&
      MemberQ[vowels, #[[2]]]
     )
    &]] // Length

f[3]=

Map[StringJoin[#] &, Select[
   Map[# &, Tuples[alphabet, 3]],
   (MemberQ[vowels, #[[1]]] &&
      MemberQ[vowels, #[[2]]] &&
      MemberQ[vowels, #[[3]]]
     )
    &]] // Length

and so on...

I see that

Tuples[alphabet,3]

can be written as

Tuples[alphabet,n]

but how about the logical construction, (that is where I get lost) ?

   (MemberQ[vowels, #[[1]]] &&
      MemberQ[vowels, #[[2]]] &&
      MemberQ[vowels, #[[3]]]
.
.
.
      MemberQ[vowels, #[[n]]]
     )
$\endgroup$
3
  • 2
    $\begingroup$ What does your function do in words? $\endgroup$
    – MarcoB
    Sep 20, 2022 at 14:25
  • $\begingroup$ What is it that you don't understand @MarcoB ? - Oddly when I pose a question in only words, they ask me to produce code. $\endgroup$ Sep 21, 2022 at 10:14
  • $\begingroup$ Including code is great. I would always prefer that code be accompanied by an explanation of what it does or is supposed to do. It is sometimes possible to improve code by re-implementing the ultimate goal in a radically different way, rather than trying to understand what existing code does and fixing it. In this case, you accepted an answer that ultimately boiled down to Intersection[vowels, alphabet], but it took Syed a few iterations to get there. I suspect that, if you had described your goal, we would have gotten there quicker. $\endgroup$
    – MarcoB
    Sep 21, 2022 at 13:46

1 Answer 1

1
$\begingroup$

I hope the following helps. Please let me know if it doesn't.

vowels = {"A", "E", "I", "O", "U"};
alphabet = CharacterRange["A", "Z"];


func[vowels_List, alphabet_List, n_] := 
 Module[{t = Tuples[alphabet, n]}, 
  StringJoin @@@ Pick[t, AllTrue[#, MemberQ[vowels, #] &] & /@ t] // 
   Length
  ]

Usage

func[vowels, alphabet, #] & /@ Range[3]

{5, 25, 125}


EDIT

f2[n_] := (Pick[vowels, MemberQ[alphabet, #] & /@ vowels] // Length)^n

or even simpler:

CountsBy[alphabet, MemberQ[vowels, #] &]

Once the vowels in alphabet are counted, it would be more efficient to generate the tuples instead of the other way round. I don't know of the larger picture, so I will let you decide.


Come to think of it

Intersection[vowels, alphabet]

and then make tuples.

$\endgroup$
4
  • $\begingroup$ Looks nice. Will thest this. - Update: Done, it works. $\endgroup$ Sep 20, 2022 at 14:37
  • $\begingroup$ Can you please explain this part of the solution? Map[AllTrue[#, MemberQ[vowels, #] &] &, Tuples[alphabet, 1]] Especially the use of two times # & ? Is there a more verbose form of this? $\endgroup$ Sep 21, 2022 at 10:12
  • 1
    $\begingroup$ The inner & is for checking membership in the vowels list. The outer& is for AllTrue establishing that all entries in the sublists are vowels. A less verbose form e.g., for 2-tuples would be: And @@@ Map[MemberQ[vowels, #] &, Tuples[alphabet, 2], {2}] $\endgroup$
    – Syed
    Sep 21, 2022 at 10:27
  • $\begingroup$ @niloderoock Even shorter, e.g.; SubsetQ[vowels, #] & /@ Tuples[alphabet, 2] $\endgroup$
    – Syed
    Sep 21, 2022 at 14:29

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