# How to create this function? [closed]

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]]]
)

• What does your function do in words? Sep 20, 2022 at 14:25
• What is it that you don't understand @MarcoB ? - Oddly when I pose a question in only words, they ask me to produce code. Sep 21, 2022 at 10:14
• 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. Sep 21, 2022 at 13:46

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.

• Looks nice. Will thest this. - Update: Done, it works. Sep 20, 2022 at 14:37
• 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? Sep 21, 2022 at 10:12
• 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}]
– Syed
Sep 21, 2022 at 10:27
• @niloderoock Even shorter, e.g.; SubsetQ[vowels, #] & /@ Tuples[alphabet, 2]
– Syed
Sep 21, 2022 at 14:29