The weekly challenge 274 - Task 1: Goat Latin
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-274/#TASK1 3 # 4 # Task 1: Goat Latin 5 # ================== 6 # 7 # You are given a sentence, $sentance. 8 # 9 # Write a script to convert the given sentence to Goat Latin, a made up language similar to Pig Latin. 10 # 11 # Rules for Goat Latin: 12 # 13 # 1) If a word begins with a vowel ("a", "e", "i", "o", "u"), append 14 # "ma" to the end of the word. 15 # 2) If a word begins with consonant i.e. not a vowel, remove first 16 # letter and append it to the end then add "ma". 17 # 3) Add letter "a" to the end of first word in the sentence, "aa" to 18 # the second word, etc etc. 19 # 20 ## Example 1 21 ## 22 ## Input: $sentence = "I love Perl" 23 ## Output: "Imaa ovelmaaa erlPmaaaa" 24 # 25 ## Example 2 26 ## 27 ## Input: $sentence = "Perl and Raku are friends" 28 ## Output: "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa" 29 # 30 ## Example 3 31 ## 32 ## Input: $sentence = "The Weekly Challenge" 33 ## Output: "heTmaa eeklyWmaaa hallengeCmaaaa" 34 # 35 ############################################################ 36 ## 37 ## discussion 38 ## 39 ############################################################ 40 # 41 # Split the sentence into single words, then check if the first 42 # character is a consonant. After handling both the cases of a 43 # consonant and a vowel, append the proper number of "a" characters. 44 # Note: This solution doesn't handle punctuation. Adding that would 45 # require a temporary variable that would contain any punctuation at 46 # the end of a word in order to re-add it after calculating the 47 # goat latin word. 48 49 use strict; 50 use warnings; 51 52 goat_latin("I love Perl"); 53 goat_latin("Perl and Raku are friends"); 54 goat_latin("The Weekly Challenge"); 55 56 sub goat_latin { 57 my $sentence = shift; 58 print "Input: '$sentence'\n"; 59 my $output = ""; 60 my $count = 0; 61 foreach my $word (split /\s+/, $sentence) { 62 $count++; 63 if($word =~ m/^[aeiouAEIOU]/) { 64 $word .= "ma"; 65 } else { 66 $word =~ s/^(.)(.*)/$2${1}ma/; 67 } 68 $word .= "a"x$count; 69 $output .= "$word "; 70 } 71 print "Output: $output\n"; 72 }