Skip to content

Instantly share code, notes, and snippets.

@ielcoro
Created June 15, 2013 11:41
Show Gist options
  • Select an option

  • Save ielcoro/5787852 to your computer and use it in GitHub Desktop.

Select an option

Save ielcoro/5787852 to your computer and use it in GitHub Desktop.
CodeBreaker Kata without ifs
class Rules
{
private string passcode;
public Rules(string passcode)
{
this.passcode = passcode;
}
internal string Match(string code)
{
string correctMatches = GetMatchString(code, "*", (c, i) => passcode[i] == c);
string incorrectMatches = GetMatchString(code, "X", (c, i) => passcode.IndexOf(c) != -1 && passcode[i] != c);
return correctMatches + incorrectMatches;
}
private string GetMatchString(string code, string resultChar, Func<char, int, bool> criteria)
{
int matches = code.Where(criteria).Count();
return String.Join("", Enumerable.Range(0, matches).Select(x => resultChar));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment