Created
September 24, 2013 17:47
-
-
Save khellang/6688580 to your computer and use it in GitHub Desktop.
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
using System.Text.RegularExpressions; | |
private const string DelimiterRegex = @"\[(.*?)\]"; | |
public int Add(string numbers) | |
{ | |
if (string.IsNullOrEmpty(numbers)) return 0; | |
var delimiters = new List<string> { "," }; | |
if (numbers.StartsWith("//")) | |
{ | |
var match = Regex.Match(numbers, DelimiterRegex); | |
delimiters = match.Success | |
? match.Groups[1].Captures.Cast<Capture>().Select(x => x.Value).ToList() | |
: new List<string> { numbers.Substring(2, 1) }; | |
numbers = numbers.Substring(numbers.IndexOf("\n") + 1); | |
} | |
var nums = numbers.Split(delimiters.Union(new[] { "\n" }).ToArray(), StringSplitOptions.None); | |
if (nums.Any(string.IsNullOrEmpty)) throw new InvalidOperationException("White space is not allowed."); | |
var integers = nums.Select(x => Convert.ToInt32(x)).Where(x => x <= 1000); | |
var negatives = integers.Where(x => x < 0); | |
if (negatives.Any()) throw new InvalidOperationException(string.Format("Negatives not allowed: {0}", string.Join(", ", negatives))); | |
return integers.Aggregate((current, next) => current + next); | |
} |
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
using System.Linq.Expressions; | |
public void AssertFail(Expression<Action> expression) | |
{ | |
try { | |
expression.Compile().Invoke(); | |
} catch (Exception ex) { | |
Console.WriteLine("Action '{0}' failed: {1}", expression.ToString().Replace("\n", "\\n"), ex.Message); | |
return; | |
} | |
Console.WriteLine("Action did not fail: " + expression.ToString()); | |
} | |
public void AssertEquals(int value, int expected) | |
{ | |
if (value != expected) | |
{ | |
Console.WriteLine("Expected {0}, Got {1}", expected, value); | |
} | |
Console.WriteLine(value); | |
} |
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
#load test-framework.csx; | |
#load string-calculator.csx; | |
// 1 | |
AssertEquals(Add(""), 0); | |
AssertEquals(Add("1,2"), 3); | |
Console.ReadLine(); | |
// 2 | |
AssertEquals(Add("1,2,3"), 6); | |
AssertEquals(Add("1,2,3,4,5,6"), 21); | |
Console.ReadLine(); | |
// 3 | |
AssertEquals(Add("1\n2,3"), 6);| | |
AssertFail(() => Add("1,\n2")); | |
Console.ReadLine(); | |
// 4 | |
AssertEquals(Add("//;\n1;2"), 3); | |
Console.ReadLine(); | |
// 5 | |
AssertFail(() => Add("1,-2,6,-1")); | |
Console.ReadLine(); | |
// 6 | |
AssertEquals(Add("2,1001"), 2); | |
Console.ReadLine(); | |
// 7 | |
AssertEquals(Add("//[***]\n1***2***3"), 6); | |
Console.ReadLine(); | |
// 8 | |
AssertEquals(Add("//[*][%]\n1*2%3"), 6); | |
Console.ReadLine(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment