Created
March 4, 2019 06:41
-
-
Save MongkonEiadon/9dbd2b2e4cf5f975d6b41d4447b6e78a to your computer and use it in GitHub Desktop.
Test_ByMongkon_Eiadon
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; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Data; | |
namespace Bingo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello Bingo!"); | |
Console.WriteLine("--- input number to validate bingo ---"); | |
Console.WriteLine("--- type r to reset ---"); | |
var collection = new List<int>(); | |
do { | |
var input = Console.ReadLine(); | |
if (input == "r") { | |
collection.Clear(); | |
Console.WriteLine("Bingo clear"); | |
} | |
if(int.TryParse(input, out var number)) { | |
collection.Add(number); | |
Console.WriteLine($"[{string.Join(", ", collection)}]"); | |
if (new Bingo().IsBingo(collection.ToArray())) | |
{ | |
Console.WriteLine("==========="); | |
Console.WriteLine(" B I N G O "); | |
Console.WriteLine("==========="); | |
collection.Clear(); | |
} | |
} | |
} while (true); | |
} | |
} | |
} | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Bingo { | |
public class Bingo { | |
private IEnumerable<int[]> _bingoCollection = new[] { | |
// horizontal | |
new []{1, 2, 3, 4, 5}, | |
new []{6, 7, 8, 9, 10}, | |
new []{11, 12, 13, 14, 15}, | |
new []{16, 17, 18, 19, 20}, | |
new []{21, 22, 23, 24, 25}, | |
//vertical | |
new []{1, 6, 11, 16, 21}, | |
new []{2, 7, 12, 17, 22}, | |
new []{3, 8, 13, 18, 23}, | |
new []{4, 9, 14, 19, 24}, | |
new []{5, 10, 15, 20, 25}, | |
// corners | |
new []{1, 5, 21, 25}, | |
// cross | |
new []{1, 7, 13, 19, 25}, | |
new []{21, 17, 13, 9, 5}, | |
}; | |
public bool IsBingo(int[] collections) { | |
return _bingoCollection.Any(x => !x.Except(collections).Any()); | |
} | |
} | |
} | |
using NUnit.Framework; | |
namespace Bingo { | |
[TestFixture] | |
public class BingoTest { | |
[TestCase(new[]{ 3, 4, 8, 13, 18, 19, 23 })] | |
[TestCase(new []{ 2, 1, 12, 15, 6, 18, 16, 4, 3, 21, 11 })] | |
[TestCase(new[] { 5, 25, 21, 13, 1 })] | |
public void IsBingo_WithValidCase_TrueMustReturn(int[] validCase) { | |
// arrange | |
var result = new Bingo().IsBingo(validCase); | |
// assert | |
Assert.IsTrue(result); | |
} | |
[TestCase(new[]{ 1, 13, 19, 25, 23, 2})] | |
[TestCase(new[] { 1, 13})] | |
[TestCase(new[] { 1, 13, 19, 25, 23, 2 })] | |
[TestCase(new[] { 1, 13, 19, 25, 23, 2 })] | |
public void IsBingo_WithInvalidCase_FalseMustReturned(int[] invalidCase) { | |
// arrange | |
var result = new Bingo().IsBingo(invalidCase); | |
// assert | |
Assert.IsFalse(result); | |
} | |
} | |
} | |
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using NUnit.Framework; | |
namespace Formula { | |
class Program { | |
static void Main(string[] args) { | |
Console.WriteLine("Hello Formula!"); | |
Console.WriteLine("--- input formula to calculate ---"); | |
do { | |
var input = Console.ReadLine(); | |
try { | |
var values = new ExpressionEvaluation().Eval(input); | |
Console.WriteLine($"--- {input} = {values} ---"); | |
} | |
catch | |
{ | |
Console.WriteLine($"--- expression {input} incorrect ---"); | |
} | |
} while (true); | |
} | |
} | |
public class ExpressionEvaluation { | |
public double Eval(String expr) | |
{ | |
var operations = new Stack<string>(); | |
var values = new Stack<double>(); | |
foreach (var s in Regex.Split($"({expr})", @"(\(|\)|(?<!e|E)-|(?<!e|E)\+|\*|/|\s+)")) { | |
if (string.IsNullOrWhiteSpace(s)) { } | |
else if (s.Equals("(")) { } | |
else if (s.Equals("+")) operations.Push(s); | |
else if (s.Equals("-")) operations.Push(s); | |
else if (s.Equals("*")) operations.Push(s); | |
else if (s.Equals("/")) operations.Push(s); | |
else if (s.Equals(")")) { | |
var count = operations.Count; | |
while (count > 0) | |
{ | |
var op = operations.Pop(); | |
var val = values.Pop(); | |
if (op.Equals("+")) val = values.Pop() + val; | |
else if (op.Equals("-")) val = values.Pop() - val; | |
else if (op.Equals("*")) val = values.Pop() * val; | |
else if (op.Equals("/")) val = values.Pop() / val; | |
values.Push(val); | |
count--; | |
} | |
} | |
else values.Push(double.Parse(s)); | |
//// | |
//if (values.Count == 2 && (operations.FirstOrDefault() == "+" || operations.FirstOrDefault() == "-")) { | |
// var val = values.Pop(); | |
// var op = operations.Pop(); | |
// if (op.Equals("+")) val = values.Pop() + val; | |
// else if (op.Equals("-")) val = values.Pop() - val; | |
// values.Push(val); | |
//} | |
} | |
return values.Pop(); | |
} | |
} | |
[TestFixture] | |
public class ExpressionEvaluationTest { | |
[TestCase("(22 * 2) + 50", 94)] | |
[TestCase("((2 * 3 + 12) / 2)" , 15)] | |
[TestCase("(10 - 5 + 3 / 2 * 2)", 8)] | |
public void Eval_ResultShouldBe_Correct(string expression, double result) { | |
// assert | |
var values = new ExpressionEvaluation().Eval(expression); | |
// assert | |
Assert.AreEqual(result, values); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment