Champions League Round of 16 Draw

Copy Source
Copy Link
Tools:
  1: //Declare some types
  2: type Country = 
  3:     |Germany    |Italy      |England    |Spain
  4:     |France     |Portugal   |Turkey     |Scotland
  5:     |Ukraine
  6: 
  7: type Group = |A|B|C|D|E|F|G|H
  8: 
  9: type Place = |First |Second
 10: 
 11: type Team = {
 12:     name : string;
 13:     country : Country;
 14:     group : Group;
 15:     place: Place;
 16: }
 17: 
 18: //Declare all teams
 19: let PSG = {name ="Paris Saint-Germain"; country=France; group=A;place=First}
 20: let Porto = {name ="FC Porto"; country=Portugal; group=A; place=Second}
 21: 
 22: let Schalke = {name ="FC Schalke 04"; country=Germany; group=B;place=First}
 23: let Arsenal = {name ="Arcenal FC"; country=England; group=B; place=Second}
 24: 
 25: let Malaga = {name ="Malaga CF"; country=Spain; group=C;place=First}
 26: let Milan = {name ="AC Miland"; country=Italy; group=C; place=Second}
 27: 
 28: let Dortmund = {name ="Borussia Dortmund"; country=Germany; group=D;place=First}
 29: let Real = {name ="Real Madrid CF"; country=Spain; group=D; place=Second}
 30: 
 31: let Juventus  = {name ="Juventus"; country=Italy; group=E;place=First}
 32: let Shakhtar = {name ="FC Shakhtar Donetsk"; country=Ukraine; group=E; place=Second}
 33: 
 34: let Bayern  = {name ="FC Bayern München"; country=Germany; group=F;place=First}
 35: let Valencia = {name ="Valemcia CF"; country=Spain; group=F; place=Second}
 36: 
 37: let Barcelona  = {name ="FC Barcelona"; country=Spain; group=G;place=First}
 38: let Celtic = {name ="Celtic FC"; country=Scotland; group=G; place=Second}
 39: 
 40: let ManchesterU  = {name ="Manchester United FC"; country=England; group=H;place=First}
 41: let Galatasaray = {name ="Galatasaray "; country=Turkey; group=H; place=Second}
 42: 
 43: //And put them in a list
 44: let allTeams = [PSG; Porto; Schalke; Arsenal; Malaga; Milan; Dortmund; Real; Juventus;
 45:     Shakhtar; Bayern; Valencia; Barcelona;Celtic;ManchesterU; Galatasaray;]
 46: //Partition the list in group winners and runner ups
 47: let groupWinners, runnerUps = 
 48:     let isGroupWinner team  = (team.place = First)
 49:     List.partition isGroupWinner allTeams;
 50: //I stole this function from stack overflow to generate all permutaions of a list
 51: let rec perms = 
 52:     let distrib e L =
 53:         let rec aux pre post = 
 54:             seq {
 55:                 match post with
 56:                 | [] -> yield (L @ [e])
 57:                 | h::t -> yield (List.rev pre @ [e] @ post)
 58:                           yield! aux (h::pre) t 
 59:             }
 60:         aux [] L
 61:     function 
 62:     | [] -> Seq.singleton []
 63:     | h::t -> Seq.collect (distrib h) (perms t)
 64: 
 65: //Create all draws by using all permutations of the runner ups and pairing
 66: //them with an unchanged list of gruop winners
 67: let getDraws runnerUps  groupWinners = 
 68:     [for perm in (perms runnerUps) do
 69:         yield List.zip perm groupWinners
 70:     ]
 71: 
 72: let draws = getDraws runnerUps  groupWinners
 73: 
 74: //fnction to check if a match is valid
 75: let isValidMatch (footballMatch: Team*Team) = 
 76:     match footballMatch with
 77:     |(teamA,teamB) when teamA.group = teamB.group -> false
 78:     |(teamA,teamB) when teamA.country = teamB.country  ->false
 79:     |_ -> true
 80: //checks if all matches in a draw are valid
 81: let isValidDraw (draw: (Team*Team) list)  = 
 82:     draw
 83:     |>List.map isValidMatch 
 84:     |>List.fold (&&) true
 85: //filter the draws so we only keep the valid ones
 86: let validDraws = List.filter isValidDraw draws
 87: //Calculates the probability of a match by looking how many of the draws
 88: //contain the match and dividing by the total amount of matches
 89: let matchProbability (footballMatch:Team*Team) =  
 90:     let occurences = 
 91:         validDraws 
 92:         |> List.filter (List.exists (fun (x:Team*Team) -> x= footballMatch)) 
 93:         |> List.length
 94: 
 95:     (float occurences)*100.0/(float <| List.length validDraws)
 96: //Gets a list of tupples where each tuple consist of the match and it respective probability           
 97: let getProbabilities runnerUps groupWinners = 
 98:     [for runnerUp in runnerUps do
 99:         for adversary in groupWinners do
100:         let footbalMatch = (runnerUp,adversary)
101:         yield (footbalMatch, matchProbability footbalMatch)
102:     ]
103: 
104: let probabilities = getProbabilities runnerUps groupWinners
105: 
106: //a function to print a probability
107: let printProbability probabilities = 
108:     match probabilities with
109:     |((teamA,teamB),probability) -> printfn "%s  \t  %s   \t  %F%% " teamA.name teamB.name probability
110: //print all probabilities
111: List.iter printProbability probabilities
union case Country.Germany: Country
union case Country.Italy: Country
union case Country.England: Country
union case Country.Spain: Country
union case Country.France: Country
union case Country.Portugal: Country
union case Country.Turkey: Country
union case Country.Scotland: Country
union case Country.Ukraine: Country
type Group =
  | A
  | B
  | C
  | D
  | E
  | F
  | G
  | H

Full name: Snippet.Group

  type: Group
  implements: System.IEquatable<Group>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Group>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
union case Group.A: Group
union case Group.B: Group
union case Group.C: Group
union case Group.D: Group
union case Group.E: Group
union case Group.F: Group
union case Group.G: Group
union case Group.H: Group
type Place =
  | First
  | Second

Full name: Snippet.Place

  type: Place
  implements: System.IEquatable<Place>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Place>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
union case Place.First: Place
union case Place.Second: Place
type Team =
  {name: string;
   country: Country;
   group: Group;
   place: Place;}

Full name: Snippet.Team

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
Team.name: string
Multiple items
val string : 'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
Team.country: Country
type Country =
  | Germany
  | Italy
  | England
  | Spain
  | France
  | Portugal
  | Turkey
  | Scotland
  | Ukraine

Full name: Snippet.Country

  type: Country
  implements: System.IEquatable<Country>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Country>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
Team.group: Group
Team.place: Place
val PSG : Team

Full name: Snippet.PSG

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Porto : Team

Full name: Snippet.Porto

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Schalke : Team

Full name: Snippet.Schalke

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Arsenal : Team

Full name: Snippet.Arsenal

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Malaga : Team

Full name: Snippet.Malaga

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Milan : Team

Full name: Snippet.Milan

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Dortmund : Team

Full name: Snippet.Dortmund

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Real : Team

Full name: Snippet.Real

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Juventus : Team

Full name: Snippet.Juventus

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Shakhtar : Team

Full name: Snippet.Shakhtar

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Bayern : Team

Full name: Snippet.Bayern

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Valencia : Team

Full name: Snippet.Valencia

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Barcelona : Team

Full name: Snippet.Barcelona

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Celtic : Team

Full name: Snippet.Celtic

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val ManchesterU : Team

Full name: Snippet.ManchesterU

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Galatasaray : Team

Full name: Snippet.Galatasaray

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val allTeams : Team list

Full name: Snippet.allTeams

  type: Team list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<Team>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<Team>
  implements: System.Collections.IEnumerable
val groupWinners : Team list

Full name: Snippet.groupWinners

  type: Team list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<Team>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<Team>
  implements: System.Collections.IEnumerable
val runnerUps : Team list

Full name: Snippet.runnerUps

  type: Team list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<Team>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<Team>
  implements: System.Collections.IEnumerable
val isGroupWinner : (Team -> bool)
val team : Team

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of 'T * 'T list
  with
    interface System.Collections.IEnumerable
    interface System.Collections.Generic.IEnumerable<'T>
    member Head : 'T
    member IsEmpty : bool
    member Item : index:int -> 'T with get
    member Length : int
    member Tail : 'T list
    static member Cons : head:'T * tail:'T list -> 'T list
    static member Empty : 'T list
  end

Full name: Microsoft.FSharp.Collections.List<_>

  type: List<'T>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
val partition : ('T -> bool) -> 'T list -> 'T list * 'T list

Full name: Microsoft.FSharp.Collections.List.partition
val perms : ('a list -> seq<'a list>)

Full name: Snippet.perms
val distrib : ('b -> 'b list -> seq<'b list>)
val e : 'b
Multiple items
val L : 'b list

  type: 'b list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'b>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'b>
  implements: System.Collections.IEnumerable


--------------------
val L : 'b list

  type: 'b list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'b>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'b>
  implements: System.Collections.IEnumerable
val aux : ('b list -> 'b list -> seq<'b list>)
val pre : 'b list

  type: 'b list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'b>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'b>
  implements: System.Collections.IEnumerable
val post : 'b list

  type: 'b list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'b>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'b>
  implements: System.Collections.IEnumerable
Multiple items
val seq : seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>

  type: seq<'T>
  inherits: System.Collections.IEnumerable
val L : 'b list

  type: 'b list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'b>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'b>
  implements: System.Collections.IEnumerable
val h : 'b
val t : 'b list

  type: 'b list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'b>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'b>
  implements: System.Collections.IEnumerable
val rev : 'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
module Seq

from Microsoft.FSharp.Collections
val singleton : 'T -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.singleton
val h : 'a
val t : 'a list

  type: 'a list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'a>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'a>
  implements: System.Collections.IEnumerable
val collect : ('T -> #seq<'U>) -> seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.collect
val getDraws : 'a list -> 'b list -> ('a * 'b) list list

Full name: Snippet.getDraws
val runnerUps : 'a list

  type: 'a list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'a>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'a>
  implements: System.Collections.IEnumerable
val groupWinners : 'b list

  type: 'b list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'b>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'b>
  implements: System.Collections.IEnumerable
val perm : 'a list

  type: 'a list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'a>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'a>
  implements: System.Collections.IEnumerable
val zip : 'T1 list -> 'T2 list -> ('T1 * 'T2) list

Full name: Microsoft.FSharp.Collections.List.zip
val draws : (Team * Team) list list

Full name: Snippet.draws

  type: (Team * Team) list list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<(Team * Team) list>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<(Team * Team) list>
  implements: System.Collections.IEnumerable
val isValidMatch : Team * Team -> bool

Full name: Snippet.isValidMatch
val footballMatch : Team * Team
val teamA : Team

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val teamB : Team

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val isValidDraw : (Team * Team) list -> bool

Full name: Snippet.isValidDraw
val draw : (Team * Team) list

  type: (Team * Team) list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<Team * Team>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<Team * Team>
  implements: System.Collections.IEnumerable
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>

  type: 'T list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
val map : ('T -> 'U) -> 'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val fold : ('State -> 'T -> 'State) -> 'State -> 'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val validDraws : (Team * Team) list list

Full name: Snippet.validDraws

  type: (Team * Team) list list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<(Team * Team) list>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<(Team * Team) list>
  implements: System.Collections.IEnumerable
val filter : ('T -> bool) -> 'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
val matchProbability : Team * Team -> float

Full name: Snippet.matchProbability
val occurences : int

  type: int
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
val exists : ('T -> bool) -> 'T list -> bool

Full name: Microsoft.FSharp.Collections.List.exists
val x : Team * Team
val length : 'T list -> int

Full name: Microsoft.FSharp.Collections.List.length
Multiple items
val float : 'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>

  type: float<'Measure>
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IFormattable
  implements: System.IComparable<float<'Measure>>
  implements: System.IEquatable<float<'Measure>>
  inherits: System.ValueType


--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val getProbabilities : seq<Team> -> seq<Team> -> ((Team * Team) * float) list

Full name: Snippet.getProbabilities
val runnerUps : seq<Team>

  type: seq<Team>
  inherits: System.Collections.IEnumerable
val groupWinners : seq<Team>

  type: seq<Team>
  inherits: System.Collections.IEnumerable
val runnerUp : Team

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val adversary : Team

  type: Team
  implements: System.IEquatable<Team>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Team>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val footbalMatch : Team * Team
val probabilities : ((Team * Team) * float) list

Full name: Snippet.probabilities

  type: ((Team * Team) * float) list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<(Team * Team) * float>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<(Team * Team) * float>
  implements: System.Collections.IEnumerable
val printProbability : (Team * Team) * float -> unit

Full name: Snippet.printProbability
val probabilities : (Team * Team) * float
val probability : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val printfn : Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val iter : ('T -> unit) -> 'T list -> unit

Full name: Microsoft.FSharp.Collections.List.iter

More information

Link: http://fssnip.net/g2
Posted: 1 years ago
Author:
Tags: