Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.22 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.org
  2.  
  3. open System
  4. open System.Text.RegularExpressions
  5. open FSharp.Data
  6.  
  7. type FlairComboDataRaw = CsvProvider<"https://pastebin.com/raw/gmH0JSw4", "|">
  8.  
  9. let ReplaceEllipsis (s: string) = s.Replace("…", "...")
  10.  
  11. type Flair = {
  12.     Flair: string option
  13.     Team: string
  14. }
  15.  
  16. type FlairCombo = {
  17.     Flair1: Flair
  18.     Flair2: Flair
  19. }
  20.  
  21. let FlairRegex = new Regex(" :([^:]+): ")
  22. let TeamRegex = new Regex(" ([^:]+) ")
  23. let ParseFlairCombo (row: FlairComboDataRaw.Row): seq<FlairCombo> =
  24.     let m1 = TeamRegex.Match(row.``Team 1``)
  25.     let m2 = TeamRegex.Match(row.``Team 2``)
  26.     let f1 = FlairRegex.Match(row.``Team 1``)
  27.     let f2 = FlairRegex.Match(row.``Team 2``)
  28.     match (m1.Success, m2.Success, Int32.TryParse(row.Count)) with
  29.     | (true, true, (true, count)) ->
  30.         Seq.replicate count {
  31.             Flair1 = {
  32.                 Flair = if f1.Success then Some f1.Groups.[1].Value else None
  33.                 Team = ReplaceEllipsis m1.Groups.[1].Value
  34.             }
  35.             Flair2 = {
  36.                 Flair = if f2.Success then Some f2.Groups.[1].Value else None
  37.                 Team = ReplaceEllipsis m2.Groups.[1].Value
  38.             }
  39.         }
  40.     | _ ->
  41.         Seq.empty
  42.  
  43. type Team =
  44. | Wisconsin
  45. | WisconsinRiverFalls
  46. | WisconsinStout
  47. | WisconsinEauClaire
  48. | WisconsinLaCrosse
  49. | WisconsinStevensPoint
  50. | WisconsinPlatteville
  51. | WisconsinOshkosh
  52. | WisconsinWhitewater
  53. | WisconsinSuperior
  54. | WisconsinParkside
  55. | Milwaukee
  56. | Other
  57.  
  58. type TeamCombo = {
  59.     Team1: Team
  60.     Team2: Team
  61. }
  62.  
  63. let Normalize (flair: Flair): Team =
  64.     match flair.Flair with
  65.     | Some ":wisconsin:" -> Wisconsin
  66.     | Some ":wisconsinriverfalls:" -> WisconsinRiverFalls
  67.     | Some ":wisconsinstout:" -> WisconsinStout
  68.     | Some ":wisconsineauclaire:" -> WisconsinEauClaire
  69.     | Some ":wisconsinlacrosse:" -> WisconsinLaCrosse
  70.     | Some ":wisconsinstevenspoint:" -> WisconsinStevensPoint
  71.     | Some ":wisconsinplatteville:" -> WisconsinPlatteville
  72.     | Some ":wisconsinoshkosh:" -> WisconsinOshkosh
  73.     | Some ":wisconsinwhitewater:" -> WisconsinWhitewater
  74.     | Some ":wisconsinsuperior:" -> WisconsinSuperior
  75.     | Some ":wisconsinparkside:" -> WisconsinParkside
  76.     | Some ":milwaukee:" -> Milwaukee
  77.     | _ ->
  78.         let team = flair.Team
  79.         if team.StartsWith("Wisconsin-R") then WisconsinRiverFalls
  80.         else if team.StartsWith("Wisconsin-Sto") then WisconsinStout
  81.         else if team.StartsWith("Wisconsin-E") then WisconsinEauClaire
  82.         else if team.StartsWith("Wisconsin-L") then WisconsinLaCrosse
  83.         else if team.StartsWith("Wisconsin-Ste") then WisconsinStevensPoint
  84.         else if team.StartsWith("Wisconsin-Pl") then WisconsinPlatteville
  85.         else if team.StartsWith("Wisconsin-O") then WisconsinOshkosh
  86.         else if team.StartsWith("Wisconsin-W") then WisconsinWhitewater
  87.         else if team.StartsWith("Wisconsin-Su") then WisconsinSuperior
  88.         else if team.StartsWith("Wisconsin-Pa") then WisconsinParkside
  89.         else if team = "Milwaukee" then Milwaukee
  90.         else if "Wisconsin".StartsWith(team.Replace("...", "")) then Wisconsin
  91.         else Other
  92.  
  93. let NormalizeCombo (f: FlairCombo): TeamCombo = {
  94.     Team1 = Normalize f.Flair1
  95.     Team2 = Normalize f.Flair2
  96. }
  97.  
  98. let OnlyInvolvesWisconsin (t: TeamCombo) =
  99.     match (t.Team1, t.Team2) with
  100.     | Other, _ -> false
  101.     | _, Other -> false
  102.     | _ -> true
  103.  
  104. let InvolvesWisconsinAndOther (t: TeamCombo) =
  105.     match (t.Team1, t.Team2) with
  106.     | Other, Other -> false
  107.     | _, Other -> true
  108.     | Other, _ -> true
  109.     | _ -> false
  110.  
  111. [<EntryPoint>]
  112. let main _ =
  113.     let single = FlairComboDataRaw.Load("https://pastebin.com/raw/A1uwGrf9")
  114.     let multiple = FlairComboDataRaw.Load("https://pastebin.com/raw/gmH0JSw4")
  115.     let groups =
  116.         [single.Rows; multiple.Rows]
  117.         |> Seq.collect id
  118.         |> Seq.collect ParseFlairCombo
  119.         |> Seq.map NormalizeCombo
  120.         |> Seq.where OnlyInvolvesWisconsin
  121.         |> Seq.groupBy id
  122.         |> Seq.sortByDescending (fun (_, g) -> Seq.length g)
  123.  
  124.     printfn "|Flair 1|Flair 2|Count|"
  125.     printfn ":--|:--|:--|"
  126.     for (f, g) in groups do
  127.         printfn "|%A|%A|%d|" f.Team1 f.Team2 (Seq.length g)
  128.     printfn ""
  129.  
  130.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement