Created
June 29, 2020 19:23
-
-
Save joelstransky/5f8165331c029bc53bc9838d0b577f63 to your computer and use it in GitHub Desktop.
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.Text.RegularExpressions; | |
namespace BreakEvenCalculator | |
{ | |
// Parses strings into American Odds and converts to other formats. | |
public class AmericanOdds | |
{ | |
private const string rx_string = @"^([\+-])(\d\d\d+)$"; | |
private readonly int odds; | |
private readonly bool favorite; | |
private readonly float oddsValue; | |
public static bool IsValid(string am_odds) | |
{ | |
return Regex.IsMatch(am_odds, rx_string); | |
} | |
public AmericanOdds(string am_odds) | |
{ | |
var valid = false; | |
var rx = new Regex(rx_string, RegexOptions.Compiled); | |
MatchCollection matches = rx.Matches(am_odds); | |
if (matches.Count < 1) | |
{ | |
valid = false; | |
} | |
else | |
{ | |
GroupCollection groups = matches[0].Groups; | |
if (groups[1].Value == "+") | |
{ | |
favorite = false; | |
valid = true; | |
} | |
else if (groups[1].Value == "-") | |
{ | |
favorite = true; | |
valid = true; | |
} | |
else | |
{ | |
valid = false; | |
} | |
if (int.TryParse(groups[2].Value, out odds)) | |
{ | |
oddsValue = Int32.Parse(groups[2].Value) * (favorite ? -1 : 1); | |
} | |
else | |
{ | |
valid = false; | |
} | |
} | |
if (!valid) | |
{ | |
throw new ArgumentException("Invalid odds format", nameof(am_odds)); | |
} | |
} | |
public double GetBreakEvenPercentage() | |
{ | |
if (oddsValue > 0) | |
{ | |
return 100 / (100 + oddsValue); | |
} | |
else | |
{ | |
return -oddsValue / (100 + oddsValue * -1); | |
} | |
// Converts parsed American Odds to a break-even percentage. | |
// Examples: | |
// -150 => 0.6 | |
// +300 => 0.25 | |
// -100 => 0.5 | |
// TODO: Please implement this function. | |
return 0.0; | |
} | |
} | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Enter American Odds"); | |
var odds = Console.ReadLine(); | |
try | |
{ | |
var am_odds = new AmericanOdds(odds); | |
Console.WriteLine("Break-Even Percentage: {0:P}", am_odds.GetBreakEvenPercentage()); | |
} | |
catch (ArgumentException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment