C# PigLatin exercise : tests failing for Rule 3 and Rule 4
I am writing a Translate function :
Rule 3:If a word starts with zero or more consonants followed by "qu", first move those consonants (if any) and the "qu" part to the end of the word, and then add an "ay" sound to the end of the word.
For example:
"quick" -> "ickqu" -> "ickquay" (starts with "qu", no preceding consonants)
"square" -> "aresqu" -> "aresquay" (starts with one consonant followed by "qu")
Test case failed :
Expected: "eenquay"
Actual: "ueenqay"
Rule 4:If a word starts with one or more consonants followed by "y", first move the consonants preceding the "y"to the end of the word, and then add an "ay" sound to the end of the word.
Some examples:
"my" -> "ym" -> "ymay" (starts with single consonant followed by "y")
"rhythm" -> "ythmrh" -> "ythmrhay" (starts with multiple consonants followed by "y")
Test case failed :
Expected: "ymay"
Actual: "myay"
This is my code
What I am doing wrong?

8 Replies
What's rule 2?
Try moving Rule 2 after Rule 4 then return word logic.
@Pavan
Rule 2: If a word begins with one or more consonants, first move those consonants to the end of the word and then add an "ay" sound to the end of the word.
For example:
"pig" -> "igp" -> "igpay" (starts with single consonant)
"chair" -> "airch" -> "airchay" (starts with multiple consonants)
Thanks for your suggestion! I did as you recommended, and it really helped — now 22 out of 23 test cases are passing.
One test case is still failing with the message below:
Code Run
Assert.Equal("ickquay astfay unray", PigLatin.Translate("quick fast run"));
Test Failure
Assert.Equal() Failure: Strings differ
↓ (pos 3)
Expected: "ickquay astfay unray"
Actual: "ick fast runquay"
↑ (pos 3)
Also, I was wondering do you have any idea why the other test cases were failing before I moved Rule 2 after Rule 4?
Because rule 2 and rule 4nhave overlaps. What does the question says about that?
understood, could you clarify which part of the question you're referring to?
Have you set breakpoints and stepped through the code?
It is just a matter of the flow of control. You will see now that the word is "quick" qu again. My guess is the flow again is not ordered properly for the expression logic. I believe this exercise is to teach you Control Flow. The suggestion of stepping through the code was a great one.
I get it now, thanks for the clarification!
Also, I refactored the code to use a foreach loop and go through each word, apply the Pig Latin rules one at a time, and build up the full translated sentence. After making this change, all 23 test cases are passing!
Yes, I did, and I now understand what the issue was.