diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-06-18 10:40:51 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-06-18 10:40:51 +0200 |
| commit | 44ef63bedf72546f1cb8c828a604430fe9e5d983 (patch) | |
| tree | 01d7c7bff3ca8fb19866b08abc0b2fdbe6e78af3 | |
| parent | 39cdf980f32c87a37e69fa0e74416711584a1847 (diff) | |
| download | perlweeklychallenge-club-44ef63bedf72546f1cb8c828a604430fe9e5d983.tar.gz perlweeklychallenge-club-44ef63bedf72546f1cb8c828a604430fe9e5d983.tar.bz2 perlweeklychallenge-club-44ef63bedf72546f1cb8c828a604430fe9e5d983.zip | |
Add ch-1 in Ruby
| -rw-r--r-- | challenge-274/spadacciniweb/ruby/ch-1.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-274/spadacciniweb/ruby/ch-1.rb b/challenge-274/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..e944cec48f --- /dev/null +++ b/challenge-274/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,48 @@ +# Task 1: Goat Latin +# Submitted by: Mohammad Sajid Anwar +# +# 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" + +def goat_latin(sentence) + new_sentences = [] + sentence.split.each_with_index do | w, idx | + unless w.match?(/^[aeiou]/i) + w=w[1, w.length-1]+w[0] + end + new_sentences.push( w+"ma"+"a"*(idx+1) ) + end + + printf "sentence '%s' -> '%s'\n", + sentence, + new_sentences.join(" ") +end + +sentence = "I love Perl" +goat_latin(sentence) + +sentence = "Perl and Raku are friends" +goat_latin(sentence) + +sentence = "The Weekly Challenge" +goat_latin(sentence) |
