Perl Weekly Challenge 274: Goat Latin

These are some answers to the Week 274, Task 1, of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on June 23, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Goat Latin

You are given a sentence, $sentance.

Write a script to convert the given sentence to Goat Latin, a made up language similar to Pig Latin.

Rules for Goat Latin:

1) If a word begins with a vowel ("a", "e", "i", "o", "u"), append "ma" to the end of the word. 2) If a word begins with consonant i.e. not a vowel, remove first letter and append it to the end then add "ma". 3) Add letter "a" to the end of first word in the sentence, "aa" to the second word, etc etc.

Example 1

Input: $sentence = "I love Perl"
Output: "Imaa ovelmaaa erlPmaaaa"

Example 2

Input: $sentence = "Perl and Raku are friends"
Output: "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa"

Example 3

Input: $sentence = "The Weekly Challenge"
Output: "heTmaa eeklyWmaaa hallengeCmaaaa"

Goat Latin in Raku

The first thing is to define consonants: I decided to define it as the set of all upper-case and lower-case letters minus (in the sense of set difference) upper-case and lower-case vowels. The rest is just following the task description: remove the first letter from the beginning if t is a consonant, add "ma" and then add an "a" to the end of first word in the sentence, an "aa" to the second word, etc.*

sub goat-latin ($in) {
    my $consonants = (('a'..'z').Set ∪ ('A'..'Z').Set)
        (-) <a e i o u A E I O U>.Set;
    my @out;
    my $wc = 0;
    for $in.words -> $word {
        my $result = $word;
        $wc++;
        if (substr $result, 0, 1)  ∈ $consonants {
            $result = (substr $word, 1) ~ (substr $result, 0, 1);
        }
        $result ~= "ma";
        $result ~= 'a' x  $wc;
        push @out, $result;
    }
    return join " ", @out;
}
my @tests = "I love Perl", "Perl and Raku are friends",
    "The Weekly Challenge";
for @tests -> $test {
    say "English: $test";
    say "Goat Latin: ", goat-latin $test;
    say "-----";
}

This program displays the following output:

$ raku ./goat-latin.raku
English: I love Perl
Goat Latin: Imaa ovelmaaa erlPmaaaa
-----
English: Perl and Raku are friends
Goat Latin: erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa
-----
English: The Weekly Challenge
Goat Latin: heTmaa eeklyWmaaa hallengeCmaaaa

Goat Latin in Perl %vowel

This is a port to Perl of the above Raku program. The most significant difference is that we use a hash (%vowels) to store the vowels.

use strict;
use warnings;
use feature 'say';

sub goat_latin {
    my $in = shift;
    my %vowels = map {$_ => 1} qw<a e i o u A E I O U>;
    my @out;
    my $wc = 0;
    for my $word (split /\s+/, $in) {
        $wc++;
        unless (defined $vowels{substr $word, 0, 1}) {
            $word = (substr $word, 1) . (substr $word, 0, 1);
        }
        $word .= "ma";
        $word .= 'a' x  $wc;
        push @out, $word;
    }
    return join " ", @out;
}
my @tests = ("I love Perl", "Perl and Raku are friends", "The Weekly Challenge");
for my $test (@tests) {
    say "English: $test";
    say "Goat Latin: ", goat_latin $test;
    say "-----";
}

This program displays the following output:

$ perl ./goat-latin.pl
English: I love Perl
Goat Latin: Imaa ovelmaaa erlPmaaaa
-----
English: Perl and Raku are friends
Goat Latin: erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa
-----
English: The Weekly Challenge
Goat Latin: heTmaa eeklyWmaaa hallengeCmaaaa
-----

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on June 30, 2024. And, please, also spread the word about the Perl Weekly Challenge if you can.

Leave a comment

About laurent_r

user-pic I am the author of the "Think Perl 6" book (O'Reilly, 2017) and I blog about the Perl 5 and Raku programming languages.