Created
February 6, 2018 19:49
-
-
Save jianminchen/2dc7bef86be55d7d7d9af10af081a217 to your computer and use it in GitHub Desktop.
Float number and operators to get maximum number, brute force solution. I had a good time to play the code, and it works; but I am not sure how it is designed from line 43 to line 67
This file contains 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; | |
using System.Threading.Tasks; | |
namespace floatNumberAndOperators | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var maximum = EvaluateMaximize(new double[] { 1, 12, -3 }); | |
} | |
/// <summary> | |
/// Float numbers and operators: +, -, *, / | |
/// What is the maximum number? | |
/// code review Feb. 6, 2018 | |
/// [1, 12, -3], 33, first two numbers 1 - 12 = -11 | |
/// This function is a brute force solution. | |
/// </summary> | |
/// <param name="terms"></param> | |
/// <returns></returns> | |
public static double EvaluateMaximize(double[] terms) | |
{ | |
int N = terms.Length; | |
if (N > 16) | |
{ | |
throw new ArgumentException("Too complex for this solution"); | |
} | |
var intermediate = new double[1 << (2 * N)]; | |
int iInterm = 0; | |
double maximized = 0; | |
intermediate[iInterm++] = terms[0]; // 1 | |
for (int index = 1; index < N; index++) | |
{ | |
int iMaxInterm = iInterm - 1; | |
double m = 0; | |
for (int iPrev = iMaxInterm; iPrev >= 0; iPrev--) | |
{ | |
double prev = intermediate[iPrev]; | |
var startIndex = iPrev * 4; | |
var current = terms[index]; | |
intermediate[startIndex + 0] = prev * current; | |
intermediate[startIndex + 1] = prev / current; | |
intermediate[startIndex + 2] = prev - current; | |
intermediate[startIndex + 3] = prev + current; | |
if (index != N - 1) // skip if not the last term | |
continue; | |
m = Math.Max(m, intermediate[startIndex + 3]); | |
m = Math.Max(m, intermediate[startIndex + 2]); | |
m = Math.Max(m, intermediate[startIndex + 1]); | |
m = Math.Max(m, intermediate[startIndex + 0]); | |
} | |
maximized = m; | |
iInterm *= 4; | |
} | |
return maximized; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment