Created
December 9, 2017 13:58
-
-
Save Miesvanderlippe/599b5bbffc5a28e62a26c5a53a8d0928 to your computer and use it in GitHub Desktop.
And exercise for a user in a C# Telegram group.
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; | |
namespace ConsoleApp1 | |
{ | |
internal interface Solution | |
{ | |
/// <summary> | |
/// Simple interface for solutions to math problems with 2 factors | |
/// </summary> | |
int Factor1 { get; set; } | |
int Factor2 { get; set; } | |
int Target { get; set; } | |
double DistanceToTarget { get; } | |
string Visualization { get; } | |
} | |
internal class Multiplication : Solution | |
{ | |
/// <summary> | |
/// Multiplies factor1 and factor 2 | |
/// </summary> | |
public int Factor1 { get; set; } | |
public int Factor2 { get; set; } | |
public int Target { get; set; } | |
public double DistanceToTarget => System.Math.Abs(Target - (Factor1 * Factor2)); | |
public string Visualization => $"{Factor1} * {Factor2} = {Factor1 * Factor2}"; | |
} | |
internal class Division : Solution | |
{ | |
/// <summary> | |
/// Divides factor1 over factor 2 | |
/// </summary> | |
public int Factor1 { get; set; } | |
public int Factor2 { get; set; } | |
public int Target { get; set; } | |
public double DistanceToTarget => System.Math.Abs(Target - (Factor1 / Factor2)); | |
public string Visualization => $"{Factor1} / {Factor2} = {Factor1 / Factor2}"; | |
} | |
internal class Addition : Solution | |
{ | |
/// <summary> | |
/// Adds factor1 to factor 2 | |
/// </summary> | |
public int Factor1 { get; set; } | |
public int Factor2 { get; set; } | |
public int Target { get; set; } | |
public double DistanceToTarget => System.Math.Abs(Target - (Factor1 + Factor2)); | |
public string Visualization => $"{Factor1} + {Factor2} = {Factor1 + Factor2}"; | |
} | |
internal class Subtraction : Solution | |
{ | |
/// <summary> | |
/// Subtracts factor2 from factor1 | |
/// </summary> | |
public int Factor1 { get; set; } | |
public int Factor2 { get; set; } | |
public int Target { get; set; } | |
public double DistanceToTarget => System.Math.Abs(Target - (Factor1 - Factor2)); | |
public string Visualization => $"{Factor1} - {Factor2} = {Factor1 - Factor2}"; | |
} | |
class Program | |
{ | |
/// <summary> | |
/// Simple program that tries to find solutions that come as close as posible to a target number. | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) | |
{ | |
// The factors we'll use | |
var factors = new[] { 1, 3, 3, 7, 10, 5 }; | |
// The number we're trying to be close to | |
const int target = 999; | |
// Our solutions | |
var solutions = new List<Solution>(); | |
// The factors we've already used so we won't end up with duplicate solutions | |
var usedFactors = new List<int>(); | |
foreach (var factor1 in factors) | |
{ | |
// Avoiding duplicate solutions | |
foreach (var factor2 in factors.Where(x => !usedFactors.Contains(x))) | |
{ | |
// Add each type of solution | |
solutions.AddRange(new Solution[] | |
{ | |
// A constructor would be nice, but whatever. | |
new Multiplication { Factor1 = factor1, Factor2 = factor2, Target = target }, | |
new Division { Factor1 = factor1, Factor2 = factor2, Target = target }, | |
new Addition { Factor1 = factor1, Factor2 = factor2, Target = target }, | |
new Subtraction { Factor1 = factor1, Factor2 = factor2, Target = target } | |
}); | |
} | |
usedFactors.Add(factor1); | |
} | |
foreach (var solution in solutions.OrderBy(x => x.DistanceToTarget).Take(10)) | |
{ | |
Console.WriteLine(solution.Visualization); | |
} | |
Console.WriteLine("Press any key to exit"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment