Context
I would like to receive feedback on my code for the question which can be seen here.
Problem
This is another C# implementation for the a function for given parameters int a, int b - both represent characters 'A' and 'B' respectively, where the function should return a string containing both characters 'A' and 'B' occurring a times and b times respectively but neither 'A' nor 'B' repeating consecutively for more than 2 times. Both values for a and b are given in a way that a string can be build using those numbers - so eg: Foo(0,3) or Foo(1,7) shall not be invoked.
eg:
Foo(3,3) returns "BBAABA" or "AABBAB"
Foo(4,1) returns "AABAA"
Foo(3,5) returns "BAABBABB" or "BBAABBAB"
Code
static string GenerateString (int a, int b)
{
int remainingTwiceChar = a;
int remainingOnceChar = b;
string twiceChar = "A";
string onceChar = "B";
if(b>a)
{
twiceChar = "B";
onceChar = "A";
remainingTwiceChar = b;
remainingOnceChar = a;
}
int mod = (a + b) % 3;
int div = (a + b) / 3;
StringBuilder sb = new StringBuilder(a + b);
for(int x = 0; x < div ; x++)
{
if (remainingTwiceChar >= 2 && remainingOnceChar >= 1)
{
sb.Append(twiceChar);
sb.Append(twiceChar);
sb.Append(onceChar);
remainingTwiceChar -= 2;
remainingOnceChar--;
}
else
{
sb.Append(twiceChar);
sb.Append(onceChar);
sb.Append(onceChar);
}
}
for (int x = 0; x < mod; x++)
{
if (remainingOnceChar > 0)
{
sb.Append(onceChar);
remainingOnceChar--;
}
if (remainingTwiceChar > 0)
{
sb.Append(twiceChar);
remainingTwiceChar--;
}
}
return sb.ToString();
}
Even though the implementation in the original question has a bug, my code does not have it. I attended this question a code testing platform. I guess, the code works (even it does not have the flaw that the code in the question has), however the testing platform gave only 33.33% marks to my question. Can anybody suggest me what I am doing wrong.