Skip to content

Instantly share code, notes, and snippets.

@ijrussell
Created April 27, 2022 20:17
Show Gist options
  • Save ijrussell/48ef9839130feaf359cee611d6f6c7cb to your computer and use it in GitHub Desktop.
Save ijrussell/48ef9839130feaf359cee611d6f6c7cb to your computer and use it in GitHub Desktop.
type Result = Win | Lose | Draw
type TableRow = { Id:string; Wins:int; Draws:int; Losses:int; Points:int }
let results = [
"A,B,win"
"C,D,lose"
"B,D,draw"
"C,A,win"
"B,C,lose"
"D,A,win"
]
let processData (data:string list) =
data
|> List.map (fun row ->
match row.Split(",") with
| [|home; away; result|] ->
match result with
| "win" -> [(home, Win); (away, Lose)]
| "lose" -> [(home, Lose); (away, Win)]
| "draw" -> [(home, Draw); (away, Draw)]
| x -> failwith $"Unknown result: {x}"
| _ -> failwith "Invalid input"
)
|> List.collect id
|> List.groupBy fst
|> List.map (fun (team,results) ->
results
|> List.fold (fun acc res ->
match snd res with
| Win -> { acc with Wins = acc.Wins + 1; Points = acc.Points + 3 }
| Draw -> { acc with Draws = acc.Draws + 1; Points = acc.Points + 1 }
| Lose -> { acc with Losses = acc.Losses + 1 }
) { Id = team; Wins = 0; Draws = 0; Losses = 0; Points = 0 }
)
|> List.sortByDescending (fun x -> x.Points)
let res =
results
|> processData
|> printfn "%A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment