-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindExpression.cs
147 lines (118 loc) · 4.89 KB
/
FindExpression.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
namespace LearningDotNet
{
public static class FindExpression
{
public static string GetFinalExpression(IReadOnlyCollection<int> inputs, int result)
{
var combinedValue = 0;
Random rnd = new Random();
while (true)
{
bool ForTheFirstCount(int i)
{
return i == 1;
}
var count = 1;
var expression = "";
//Useful for getting random numbers and remove picked up numbers
List<int> inputNumbers = inputs.ToList();
while (count < inputs.Count)
{
Tuple<string, int> expressionWithCombinedValue;
expression = ForTheFirstCount(count)
? GetExpressionForFirstCount(inputNumbers, out expressionWithCombinedValue, rnd)
: GetExpressionForRestOfTheCount(inputNumbers, combinedValue, out expressionWithCombinedValue,
expression, rnd);
combinedValue = expressionWithCombinedValue.Item2;
if (combinedValue == result)
{
return expression;
}
count++;
}
}
}
private static string GetExpressionForRestOfTheCount(List<int> inputNumbers, int combinedValue,
out Tuple<string, int> expressionWithCombinedValue, string expression, Random rnd)
{
Tuple<int, int> randomNumberAndIndex = GetRandomNumberAndRemoveItFromCollection(inputNumbers);
expressionWithCombinedValue =
CallRandomMethodForExpressionAndValue(rnd.Next(0, 4), combinedValue,
randomNumberAndIndex.Item1);
expression += " " + expressionWithCombinedValue.Item1 + " " + randomNumberAndIndex.Item1;
return expression;
}
private static string GetExpressionForFirstCount(List<int> inputNumbers,
out Tuple<string, int> expressionWithCombinedValue, Random rnd)
{
Tuple<int, int> randomNumberAndIndex1 = GetRandomNumberAndRemoveItFromCollection(inputNumbers);
Tuple<int, int> randomNumberAndIndex2 = GetRandomNumberAndRemoveItFromCollection(inputNumbers);
expressionWithCombinedValue = CallRandomMethodForExpressionAndValue(rnd.Next(0, 4),
randomNumberAndIndex1.Item1,
randomNumberAndIndex2.Item1);
var expression = randomNumberAndIndex1.Item1 + " " + expressionWithCombinedValue.Item1 + " " +
randomNumberAndIndex2.Item1;
return expression;
}
private static Tuple<int, int> GetRandomNumberAndRemoveItFromCollection(List<int> inputNumbers)
{
Tuple<int, int> randomNumberAndIndex1 = GetRandomNumberAndIndex(inputNumbers);
inputNumbers.RemoveAt(randomNumberAndIndex1.Item2);
return randomNumberAndIndex1;
}
private static Tuple<int, int> GetRandomNumberAndIndex(IReadOnlyList<int> inputNumbers)
{
Random rnd = new Random();
var index = rnd.Next(0, inputNumbers.Count);
return new Tuple<int, int>(inputNumbers[index], index);
}
private static Tuple<string, int> CallRandomMethodForExpressionAndValue(int randomNumber, int input1,
int input2)
{
string expression;
int value;
switch (randomNumber)
{
case 0:
expression = "+";
value = Sum(input1, input2);
break;
case 1:
expression = "*";
value = Multiply(input1, input2);
break;
case 2:
expression = "-";
value = Subtract(input1, input2);
break;
case 3:
expression = "/";
value = Divide(input1, input2);
break;
default:
throw new ArgumentException("Invalid Case to be considered");
}
return new Tuple<string, int>(expression, value);
}
private static int Sum(int input1, int input2)
{
return input1 + input2;
}
private static int Multiply(int input1, int input2)
{
return input1 * input2;
}
private static int Subtract(int input1, int input2)
{
return input1 - input2;
}
private static int Divide(int input1, int input2)
{
if (input2 <= 0) throw new ArgumentOutOfRangeException(nameof(input2));
return input1 / input2;
}
}
}