Created
May 19, 2017 11:44
-
-
Save jovaneyck/a9c19cb71807c10b54d4d8bff141340d to your computer and use it in GitHub Desktop.
F# script to tally up AE hackathon 2017 scores
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
#r @"C:\nuget_local\Unquote.3.1.1\lib\net45\Unquote.dll" | |
open Swensen.Unquote | |
type Vote = {voter : string; first : string; second : string; third : string} | |
let lines = | |
System.IO.File.ReadAllLines(@"C:\Users\ext_jvy\Desktop\hubspot-form-submissions-ae-hackathon-2017-public-voting-2017-04-28 (1).csv") | |
|> List.ofArray | |
|> List.skip 1 | |
let parseVote (rawVote : string) = | |
let split = rawVote.Split([|","|], System.StringSplitOptions.RemoveEmptyEntries) | |
{voter = split.[2]; first = split.[3]; second = split.[4]; third = split.[5]} | |
let onlyLatestVotes votes = | |
votes | |
|> List.groupBy (fun v -> v.voter) | |
|> List.map (fun (voter, votes) -> List.head votes) | |
let duplicateTeamInVote v = | |
v.first = v.second | |
|| v.first = v.third | |
|| v.second = v.third | |
let toPoints vote = | |
[ | |
(vote.first, 3) | |
(vote.second, 2) | |
(vote.third, 1) | |
] | |
let tally lines = | |
lines | |
|> List.map parseVote | |
|> onlyLatestVotes | |
|> List.filter (duplicateTeamInVote >> not) | |
|> List.collect toPoints | |
|> List.groupBy (fun (team, point) -> team) | |
|> List.map (fun (team, points) -> (team, points |> List.map snd)) | |
|> List.map (fun (team, points) -> (team, List.sum points)) | |
|> List.sortByDescending snd | |
let aVote = {voter = "voter"; first = "first"; second = "second"; third = "third"} | |
printfn "Testing" | |
test <@ parseVote "_,_voter,first,second,third" = {voter = "voter"; first = "first"; second = "second"; third = "third"} @> | |
test <@ onlyLatestVotes [aVote] = [aVote] @> | |
test <@ onlyLatestVotes [aVote; {aVote with voter = "another_voter"}] = [aVote; {aVote with voter = "another_voter"}] @> | |
test <@ onlyLatestVotes [aVote; aVote] = [aVote] @> | |
test <@ onlyLatestVotes [aVote; {aVote with first = "another_first"}] = [{aVote with first = "another_first"}] @> | |
test <@ onlyLatestVotes [aVote; {aVote with voter = "another_voter"} ;{aVote with first = "another_first"}] = [{aVote with first = "another_first"}; {aVote with voter = "another_voter"}] @> | |
test <@ duplicateTeamInVote {aVote with first = "f"; second = "f"} @> | |
test <@ duplicateTeamInVote {aVote with first = "f"; third = "f"} @> | |
test <@ duplicateTeamInVote {aVote with second = "f"; third = "f"} @> | |
test <@ not (duplicateTeamInVote aVote) @> | |
printfn "Done!" | |
printfn "LET'S GO!" | |
tally lines | |
|> List.iter (fun (t,p) ->printfn "Team %s scores %d points!" t p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment