vowel families
19.11.2023 1 min readWrite a function that selects all words that have all the same vowels (in any order and/or number) as the first word, including the first word.
sameVowelGroup(["toe", "ocelot", "maniac"]) ➞ ["toe", "ocelot"]
sameVowelGroup(["many", "carriage", "emit", "apricot", "animal"]) ➞ ["many"]
sameVowelGroup(["hoops", "chuff", "bot", "bottom"]) ➞ ["hoops", "bot", "bottom"]
This was a nice fun one to start the day.
Crystal:
def get_v_group(w : String)
vowels = w.chars.uniq.select { |e| "aeiou".includes?(e) }
vowels.sort.uniq.join
end
def same_vowel_group(a : Array(String)) : Array(String)
first_w_vowels = get_v_group(a[0])
a.select { |w| get_v_group(w) == first_w_vowels }
end
Nim:
func getVowelGroup(w: string): string =
let allVowels = w.toSeq.deduplicate.filterIt("aeiou".contains(it))
allVowels.deduplicate.sorted.join
func sameVowelGroup(a: seq[string]): seq[string] =
let firstVowelGroup = getVowelGroup(a[0])
return a.filterIt(getVowelGroup(it) == firstVowelGroup)
Raku:
sub get-v-group($w) {
$w.comb.grep({"aeiou".contains($_)}).unique.sort.join;
}
sub same-vowel-group(@a) {
my $first-v-group = get-v-group(@a.head);
@a.grep({get-v-group($_) eq $first-v-group});
}
Javascript:
function getVowelGroup(s: string): string {
const vowels = s.split("").filter((el) => "aeiou".includes(el));
const uniqueVowels = [...new Set(vowels.sort())];
return uniqueVowels.join("");
}
function sameVowelGroup(a: string[]): string[] {
const firstVowelGroup = getVowelGroup(a[0]);
return a.filter((el) => getVowelGroup(el) === firstVowelGroup);
}
Built with Astro and Tailwind 🚀