Skip to content

Commit ed0dc39

Browse files
committed
finished pig_latin exercise
1 parent fe04c59 commit ed0dc39

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

04_pig_latin/pig_latin.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
def translate(input)
2+
vowel = "aeiouAEIOU"
3+
output = ""
4+
punct_check = /[[:punct:]]/.match(input)
5+
words = []
6+
7+
if punct_check
8+
letters = input.split("")
9+
wbuild = ""
10+
letters.each do |c|
11+
if /\w/.match(c)
12+
wbuild += c
13+
else
14+
words.push(wbuild) if wbuild != ""
15+
words.push(c) if c != ""
16+
wbuild = ""
17+
end
18+
end
19+
else
20+
words = input.split(" ")
21+
end
22+
23+
words.each do |term|
24+
if /\w/.match(term) == nil
25+
output += term
26+
next
27+
end
28+
29+
cap = false
30+
if /[[:upper:]]/.match(term[0])
31+
cap = true
32+
end
33+
term = term.downcase
34+
term_letters = term.split("")
35+
36+
while !vowel.include?(term_letters[0])
37+
if term_letters[0] + term_letters[1] == "qu"
38+
term_letters.push(term_letters.shift)
39+
end
40+
term_letters.push(term_letters.shift)
41+
end
42+
43+
term_letters[0].upcase! if cap == true
44+
if punct_check
45+
output += term_letters.join("") + "ay"
46+
else
47+
output += term_letters.join("") + "ay" + " "
48+
end
49+
end
50+
51+
return output.strip
52+
end

04_pig_latin/pig_latin_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,13 @@
6969
# * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course)
7070
# * retain the punctuation from the original phrase
7171

72+
it "capitalized words are still capitalized" do
73+
s = translate("hello World")
74+
s.should == "ellohay Orldway"
75+
end
76+
77+
it "retain punctuation from original phrase" do
78+
s = translate("Hello, World!")
79+
s.should == "Ellohay, Orldway!"
80+
end
7281
end

0 commit comments

Comments
 (0)