Permalink
Browse files

Four4Sample - add ExpressionSearch class and test

  • Loading branch information...
bobbymcr committed Mar 11, 2018
1 parent dcdabb0 commit 9177cdd64495dc07620c4d05c339ebb25a7f6c81
@@ -0,0 +1,36 @@
// <copyright file="ExpressionSearchTest.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>
namespace Four4.Test
{
using System.Collections.Generic;
using FluentAssertions;
using Xunit;
public sealed class ExpressionSearchTest
{
[Fact]
public void OneOperandAndOneBinaryOperator()
{
List<string> expressions = new List<string>();
ExpressionSearch search = new ExpressionSearch();
search.AddOperand("4");
search.AddBinary("+");
search.Run(e => expressions.Add(e.ToString()));
expressions.Sort();
expressions.Should().ContainInOrder(
"4",
"4 4 +",
"4 4 + 4 +",
"4 4 + 4 + 4 +",
"4 4 + 4 4 + +",
"4 4 4 + +",
"4 4 4 + + 4 +",
"4 4 4 + 4 + +",
"4 4 4 4 + + +");
}
}
}
@@ -19,8 +19,12 @@ private Expression(int count, string text, NumberStack operands)
this.operands = operands;
}
public bool IsValid => this.operands.IsValid;
public int NumeralCount => this.count;
public Number Result => this.operands.Result;
public static Number Eval(string input)
{
Expression expr = default(Expression);
@@ -30,7 +34,7 @@ public static Number Eval(string input)
expr = expr.Append(token);
}
return expr.operands.Result();
return expr.Result;
}
public override string ToString()
@@ -126,14 +130,37 @@ private NumberStack(Number n1, Number n2 = default(Number), Number n3 = default(
}
}
public Number Result()
public bool IsValid
{
switch (this.size)
get
{
case 1:
return this.n1;
default:
return default(Number);
switch (this.size)
{
case 1:
return this.n1.IsValid;
case 2:
return this.n2.IsValid;
case 3:
return this.n3.IsValid;
case 4:
return this.n4.IsValid;
default:
return true;
}
}
}
public Number Result
{
get
{
switch (this.size)
{
case 1:
return this.n1;
default:
return default(Number);
}
}
}
@@ -159,7 +186,7 @@ public NumberStack Apply1(Func<Number, Number> op)
switch (this.size)
{
case 0:
return default(NumberStack);
return new NumberStack(default(Number));
case 1:
return new NumberStack(op(this.n1));
case 2:
@@ -177,7 +204,7 @@ public NumberStack Apply2(Func<Number, Number, Number> op)
{
case 0:
case 1:
return default(NumberStack);
return new NumberStack(default(Number));
case 2:
return new NumberStack(op(this.n1, this.n2));
case 3:
@@ -0,0 +1,62 @@
// <copyright file="ExpressionSearch.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>
namespace Four4
{
using System;
using System.Collections.Generic;
public sealed class ExpressionSearch
{
private readonly List<string> ops;
private readonly List<string> binaryOps;
public ExpressionSearch()
{
this.ops = new List<string>();
this.binaryOps = new List<string>();
}
public void AddOperand(string op)
{
this.ops.Add(op);
}
public void AddBinary(string op)
{
this.binaryOps.Add(op);
}
public void Run(Action<Expression> each)
{
this.Run(default(Expression), each);
}
private void Run(Expression expr, Action<Expression> each)
{
if (!expr.IsValid)
{
return;
}
if (expr.Result.IsValid)
{
each(expr);
}
if (expr.NumeralCount < 4)
{
foreach (string op in this.ops)
{
this.Run(expr.Append(op), each);
}
}
foreach (string bop in this.binaryOps)
{
this.Run(expr.Append(bop), each);
}
}
}
}

0 comments on commit 9177cdd

Please sign in to comment.