Permalink
Browse files

Four4Sample - simplify ExpressionSearch by combining unary/binary ops

  • Loading branch information...
bobbymcr committed Mar 16, 2018
1 parent ae058fe commit 21709342fb0f692013d9125fbad455e78aa8df14
@@ -16,7 +16,7 @@ public void CancelSearch()
{
ExpressionSearch search = new ExpressionSearch();
search.AddOperand("4");
search.AddBinary("+");
search.AddOperator("+");
int count = 0;
search.Run(e => ++count < 3);
@@ -39,7 +39,7 @@ public void OneOperandAndOneBinaryOperator()
ExpressionSearch search = new ExpressionSearch();
search.AddOperand("4");
search.AddBinary("+");
search.AddOperator("+");
TestSearch(search, Expected);
}
@@ -52,7 +52,7 @@ public void OneOperandAndOneUnaryOperator()
ExpressionSearch search = new ExpressionSearch();
search.AddOperand("4");
search.AddUnary("R");
search.AddOperator("R");
TestSearch(search, Expected);
}
@@ -65,7 +65,7 @@ public void OneOperandAndOneUnaryOperator2()
ExpressionSearch search = new ExpressionSearch();
search.AddOperand("4");
search.AddUnary("!");
search.AddOperator("!");
TestSearch(search, Expected);
}
@@ -639,8 +639,8 @@ 4 R
ExpressionSearch search = new ExpressionSearch();
search.AddOperand("4");
search.AddOperand(".4");
search.AddUnary("R");
search.AddUnary("+");
search.AddOperator("R");
search.AddOperator("+");
TestSearch(search, Expected);
}
@@ -754,7 +754,7 @@ public void TwoOperandsAndOneBinaryOperator()
ExpressionSearch search = new ExpressionSearch();
search.AddOperand("4");
search.AddOperand(".4");
search.AddBinary("-");
search.AddOperator("-");
TestSearch(search, Expected);
}
@@ -1480,8 +1480,8 @@ public void TwoOperandsAndTwoBinaryOperators()
ExpressionSearch search = new ExpressionSearch();
search.AddOperand("4");
search.AddOperand(".4");
search.AddBinary("+");
search.AddBinary("-");
search.AddOperator("+");
search.AddOperator("-");
TestSearch(search, Expected);
}
@@ -9,30 +9,23 @@ namespace Four4
public sealed class ExpressionSearch
{
private readonly List<string> ops;
private readonly List<string> binaryOps;
private readonly List<string> unaryOps;
private readonly List<string> operands;
private readonly List<string> operators;
public ExpressionSearch()
{
this.ops = new List<string>();
this.binaryOps = new List<string>();
this.unaryOps = new List<string>();
this.operands = new List<string>();
this.operators = new List<string>();
}
public void AddOperand(string op)
{
this.ops.Add(op);
this.operands.Add(op);
}
public void AddBinary(string op)
public void AddOperator(string op)
{
this.binaryOps.Add(op);
}
public void AddUnary(string op)
{
this.unaryOps.Add(op);
this.operators.Add(op);
}
public void Run(Func<Expression, bool> each)
@@ -57,7 +50,7 @@ private bool Run(Expression expr, Func<Expression, bool> each)
if (expr.NumeralCount < 4)
{
foreach (string op in this.ops)
foreach (string op in this.operands)
{
if (!this.Run(expr.Append(op), each))
{
@@ -66,17 +59,9 @@ private bool Run(Expression expr, Func<Expression, bool> each)
}
}
foreach (string bop in this.binaryOps)
{
if (!this.Run(expr.Append(bop), each))
{
return false;
}
}
foreach (string uop in this.unaryOps)
foreach (string op in this.operators)
{
if (!this.Run(expr.Append(uop), each))
if (!this.Run(expr.Append(op), each))
{
return false;
}
@@ -30,12 +30,12 @@ private static void Main(string[] args)
private static ExpressionSearch InitSearch()
{
ExpressionSearch search = new ExpressionSearch();
search.AddBinary("+");
search.AddBinary("-");
search.AddBinary("*");
search.AddBinary("/");
search.AddUnary("!");
search.AddUnary("R");
search.AddOperator("+");
search.AddOperator("-");
search.AddOperator("*");
search.AddOperator("/");
search.AddOperator("!");
search.AddOperator("R");
search.AddOperand("4");
search.AddOperand("44");
search.AddOperand(Number.PointFour);

0 comments on commit 2170934

Please sign in to comment.