Skip to content

Instantly share code, notes, and snippets.

@efleming969
Created June 6, 2018 19:57
Show Gist options
  • Save efleming969/67cce71ccb5e98cfb3008fc7cce1dfdf to your computer and use it in GitHub Desktop.
Save efleming969/67cce71ccb5e98cfb3008fc7cce1dfdf to your computer and use it in GitHub Desktop.
Bowling Parser in C#
[TestClass]
public class BowlingServiceParserTest
{
[TestMethod]
public void number_from_one_to_nine_translate_to_the_number()
{
int[] result = BowlingServiceParser.Parse("123456789");
CollectionAssert.AreEqual(new int[] {1,2,3,4,5,6,7,8,9}, result);
}
[TestMethod]
public void gutter_symbols_translate_to_zero()
{
int[] result = BowlingServiceParser.Parse("--");
CollectionAssert.AreEqual(new int[] { 0, 0 }, result);
}
[TestMethod]
public void char_x_translate_to_the_number_ten()
{
int[] result = BowlingServiceParser.Parse("X");
CollectionAssert.AreEqual(new int[] {10}, result);
}
[TestMethod]
public void char_slash_translate_to_ten_minus_previous_number()
{
int[] result = BowlingServiceParser.Parse("1/");
CollectionAssert.AreEqual(new int[] {1, 9}, result);
}
}
public class BowlingServiceParser
{
public static int[] Parse(string symbols)
{
var numbers = new List<int>();
foreach(var symbol in symbols)
{
if (symbol == '-')
{
numbers.Add(0);
}
else if (symbol == 'X')
{
numbers.Add(10);
}
else if (symbol == '/')
{
numbers.Add(10 - numbers.Last());
}
else {
numbers.Add(Convert.ToInt32(Char.GetNumericValue(symbol)));
}
}
return numbers.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment