Skip to content

Commit 75f186b

Browse files
committed
Pig latin challenge and solution. 🐷
1 parent b5fcf0b commit 75f186b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

challenges/pig-latin-solution.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!usr/bin/env ruby
2+
3+
# Defines a method to show the title of the game.
4+
def show_game_title(title)
5+
formatted_title = '| ' + title + ' |'
6+
dashes = '-' * formatted_title.length
7+
puts dashes
8+
puts formatted_title
9+
puts dashes
10+
end
11+
12+
def pig_latin(word)
13+
# Sets a few values to help work with word.
14+
vowels = ['a', 'e', 'i', 'o', 'u']
15+
suffix = 'ay'
16+
position = 0
17+
# Turns word into an array.
18+
word = word.split(//)
19+
20+
# If word has a vowel then return its position in the array.
21+
word.any? do |character|
22+
if vowels.include?(character)
23+
position = word.find_index(character)
24+
end
25+
end
26+
27+
# Turns word back into a string.
28+
word = word.join
29+
30+
# If the position is above 0, the word starts with one or more consonants.
31+
# It concatenates the characters from the vowel until the end of the word with the consonants and the suffix at the end.
32+
if position > 0
33+
word = word[position..-1] + word[0...position] + suffix
34+
return word
35+
end
36+
# If the word starts with a vowel it will be the last value returned.
37+
word = word + suffix
38+
end
39+
40+
show_game_title('Pig Latin')
41+
puts pig_latin('Ruby').capitalize
42+

challenges/pig-latin.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Challenge: Pig Latin
2+
# Write a script which converts a single word into pig latin.
3+
# Pig latin is typically a children's game where words are converted converted into alternative versions according to a set of simple rules.
4+
# Rules:
5+
# For words beggining with one consonant, the consonant moves to the end, followed by "ay". e.g "pig" => "igpay"
6+
# For words that begin with vowel sounds just add "ay" to the end. e.g "elevator" => "elevatoray"
7+
# For words that have a consonant cluster (two or more together), the whole clutes will move to the end, followed by "ay". e.g "glove" => "oveglay" and "where" => "erewhay"
8+
# Define a method called pig_latin that accepts a word as an argument, and returns that word that has been converted into pig latin.
9+
# Hints:
10+
# It might be helpful to have an array with the different vowels. ['a', 'e', 'i', 'o', 'u']
11+
# The word might be easier to work with if converted into an array.
12+
# If you can identify where the first value occurs, you'll know how many characters to move to the end.
13+
# Find the first vowel index position and use it to remove other characters up to that position and move them to the end of the string.
14+
# Don't forget to add the "ay" at the end.
15+
16+
# Bonus Challenge: Write methods that work together to convert a whole sentence into pig latin.
17+
# Use good capitalization.
18+
# Preserve punctuation.
19+
# Create a translator program by using a loop to get user input for sentences and show them back in pig latin.

0 commit comments

Comments
 (0)