Created
June 15, 2013 11:41
-
-
Save ielcoro/5787852 to your computer and use it in GitHub Desktop.
CodeBreaker Kata without ifs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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