Created
December 14, 2010 23:55
-
-
Save rippinrobr/741374 to your computer and use it in GitHub Desktop.
my solution
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
(* | |
Project Chadwick #2 - Calc Top 5 All Time OBP for San Fransisco Giants | |
OBP = (H + BB + HBP) / (AB + BB + HBP + SF) | |
0 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 | |
LastName,FirstName,lahmanID,yearID,stint,teamID,lgID,g,g_batting,ab,r, h,doubles,triples,hr,rbi,sb,cs,bb,so,ibb,hbp,sh,sf,gidp,g_old | |
*) | |
open System.IO | |
open System.Collections.Generic | |
// Batter Record | |
type Batter = { Last : string; First : string; Season : int; AB : float; OBP : float } | |
let all_obps = new List<Batter>(5) | |
all_obps.AddRange([| { Last = ""; First = ""; Season = 0; AB = 0.0; OBP = 0.0 }; | |
{ Last = ""; First = ""; Season = 0; AB = 0.0; OBP = 0.0 }; | |
{ Last = ""; First = ""; Season = 0; AB = 0.0; OBP = 0.0 }; | |
{ Last = ""; First = ""; Season = 0; AB = 0.0; OBP = 0.0 }; | |
{ Last = ""; First = ""; Season = 0; AB = 0.0; OBP = 0.0 } |]) | |
// Calculates the OBP | |
let obp h bb hbp ab sf = | |
let denom : float = ab * 1.0 + bb + hbp + sf | |
if denom > 0.0 then | |
(h + bb + hbp) / denom | |
else | |
0.0 | |
// Add the entry to the top 5 if its in the range | |
let rec check_top_5 entry index = | |
if entry.OBP > all_obps.[index].OBP then | |
all_obps.Insert(index, entry) | |
else | |
let new_item_index = index + 1 | |
if new_item_index < 5 then | |
check_top_5 entry new_item_index | |
let create_batters stats = | |
let cols = stats.ToString().Split(',') |> Seq.toList | |
// I'm doing this for clarity if I was worried about lines of code I would just use the | |
// the conversion when I call the obp function | |
let ab = float (cols.Item 9) | |
let h = float (cols.Item 11) | |
let bb = float (cols.Item 18) | |
let hbp = float (cols.Item 21) | |
let sf = float (cols.Item 23) | |
let season = int (cols.Item 3) | |
if ab >= 200.0 then | |
let b = { Last = cols.Item 0; First = cols.Item 1; Season = season; | |
AB = ab; OBP = (obp h bb hbp ab sf) } | |
check_top_5 b 0 | |
let print_batter batter = | |
printfn "%0.3f in %d by %s %s" batter.OBP batter.Season batter.First batter.Last | |
// File.ReadAllLines returns a string[] but to use the calc_obp we need a list<string> | |
// the |> Seq.toList will convert the string[] into a list<string> | |
let stats = File.ReadAllLines(@".\data\sf_giants_batting.csv") |> Seq.toList | |
List.map create_batters stats.Tail | |
// Display the results | |
printfn "Top 5 Seasons On Base Percentages since the Giants moved to San Fransisco" | |
printfn "=========================================================================" | |
List.map print_batter (Seq.take 5 all_obps |> Seq.toList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment