Guest User

Untitled

a guest
Jan 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. def translate(string)
  2. array = string.split(" ")
  3. pigged_array = array.map! {|x| pigify(x)}
  4. result = pigged_array.join(" ")
  5. return result
  6. end
  7.  
  8. def pigify(word)
  9. vowels = ["a", "e", "i", "o", "u"]
  10.  
  11. if vowels.include? word[0].downcase
  12. puts word + "ay"
  13.  
  14. # two cases for "qu"
  15. elsif word[0..1] == "qu"
  16. puts word[2..-1] + "quay"
  17. elsif word[1..2] == "qu"
  18. puts word[3..-1] + word[0..2] + "ay"
  19.  
  20. # for words that start with 3 consonants
  21. elsif !(vowels.include? word[0]) && !(vowels.include? word[1]) && !(vowels.include? word[2])
  22. puts word[3..-1] + word[0..2] + "ay"
  23.  
  24. # for words that start with 2 consonants
  25. elsif !(vowels.include? word[0]) && !(vowels.include? word[1]) # for 2
  26. puts word[2..-1] + word[0..1] + "ay"
  27.  
  28. # for words that start with a single consonant
  29. else
  30. puts word[1..-1] + word[0] + "ay"
  31. end
  32. end
  33.  
  34. require "04_pig_latin"
  35.  
  36. describe "#translate" do
  37. it "translates a word beginning with a vowel" do
  38. s = translate("apple")
  39. expect(s).to eq("appleay")
  40. end
  41.  
  42. it "translates a word beginning with a consonant" do
  43. s = translate("banana")
  44. expect(s).to eq("ananabay")
  45. end
  46.  
  47. it "translates a word beginning with two consonants" do
  48. s = translate("cherry")
  49. expect(s).to eq("errychay")
  50. end
  51.  
  52. it "translates two words" do
  53. s = translate("eat pie")
  54. expect(s).to eq("eatay iepay")
  55. end
  56.  
  57. it "translates a word beginning with three consonants" do
  58. expect(translate("three")).to eq("eethray")
  59. end
  60.  
  61. it "counts 'sch' as a single phoneme" do
  62. s = translate("school")
  63. expect(s).to eq("oolschay")
  64. end
  65.  
  66. it "counts 'qu' as a single phoneme" do
  67. s = translate("quiet")
  68. expect(s).to eq("ietquay")
  69. end
  70.  
  71. it "counts 'qu' as a consonant even when it's preceded by a consonant" do
  72. s = translate("square")
  73. expect(s).to eq("aresquay")
  74. end
  75.  
  76. it "translates many words" do
  77. s = translate("the quick brown fox")
  78. expect(s).to eq("ethay ickquay ownbray oxfay")
  79. end
  80.  
  81. #translate
  82.  
  83. appleay
  84. #translates a word beginning with a vowel (FAILED - 1)
  85.  
  86. Failures:
  87.  
  88. 1) #translate translates a word beginning with a vowel
  89. Failure/Error: expect(s).to eq("appleay")
  90.  
  91. expected: "appleay"
  92. got: ""
  93.  
  94. (compared using ==)
  95. # ./spec/04_pig_latin_spec.rb:29:in `block (2 levels) in <top
  96. (required)>'
  97.  
  98. Finished in 0.00168 seconds (files took 0.11091 seconds to load)
  99. 1 example, 1 failure
  100.  
  101. Failed examples:
  102.  
  103. rspec ./spec/04_pig_latin_spec.rb:27 # #translate translates a word beginning with a vowel
Add Comment
Please, Sign In to add comment