C
C#5w ago
Pavan

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
using System;
using System.Text.RegularExpressions;

public static class PigLatin
{
public static string Translate(string word)
{
word = word.ToLower();
//Rule 1
if(Regex.IsMatch(word, @"^(xr|yt|[aeiou])"))
{
return word + "ay";
}

//Rule 2
var match = Regex.Match(word, @"^([^aeiou]+)(.*)");
if(match.Success)
{
return match.Groups[2].Value + match.Groups[1].Value + "ay";
}

//Rule 3
var match2 = Regex.Match(word, @"^([^aeiou]*qu)(.*)");
if (match2.Success)
{
return match2.Groups[2].Value + match2.Groups[1].Value + "ay";
}

//Rule 4
var match3 = Regex.Match(word, @"^([^aeiou]+)(y.*)");
if (match3.Success)
{
return match3.Groups[2].Value + match3.Groups[1].Value + "ay";
}
return word + "ay";

}
}
using System;
using System.Text.RegularExpressions;

public static class PigLatin
{
public static string Translate(string word)
{
word = word.ToLower();
//Rule 1
if(Regex.IsMatch(word, @"^(xr|yt|[aeiou])"))
{
return word + "ay";
}

//Rule 2
var match = Regex.Match(word, @"^([^aeiou]+)(.*)");
if(match.Success)
{
return match.Groups[2].Value + match.Groups[1].Value + "ay";
}

//Rule 3
var match2 = Regex.Match(word, @"^([^aeiou]*qu)(.*)");
if (match2.Success)
{
return match2.Groups[2].Value + match2.Groups[1].Value + "ay";
}

//Rule 4
var match3 = Regex.Match(word, @"^([^aeiou]+)(y.*)");
if (match3.Success)
{
return match3.Groups[2].Value + match3.Groups[1].Value + "ay";
}
return word + "ay";

}
}
What I am doing wrong?
No description
8 Replies
Yawnder
Yawnder5w ago
What's rule 2?
glhays
glhays4w ago
Try moving Rule 2 after Rule 4 then return word logic. @Pavan
Pavan
PavanOP4w ago
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?
Yawnder
Yawnder4w ago
Because rule 2 and rule 4nhave overlaps. What does the question says about that?
Pavan
PavanOP4w ago
understood, could you clarify which part of the question you're referring to?
greyfox
greyfox4w ago
Have you set breakpoints and stepped through the code?
glhays
glhays4w ago
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.
Pavan
PavanOP4w ago
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.

Did you find this page helpful?