samedi 8 juillet 2017

F# - Generate Random Subset of List With Sums Of Singles And Pairs

I would like to generate an output list, which is a randomly generated subset of the original input list of size count and separately sum the singles and pairs in the output list, I show three sample output lists and their associated singles and pairs sums in the code below:

let input = [1; 1; 2; 2; 3; 3; 4; 4; 5; 5; 6; 6; 7; 7; 8; 9; 10; 11]
let count = 11
let random = System.Random()
let output = 
    List.map (fun x -> input.[random.Next(input.Length)]) 
    input
    |> Seq.take(count)
    |> Seq.toList

printfn "%A" output

// Output: [1; 1; 2; 2; 3; 3; 4; 4; 5; 5; 6]

// Sample 1: [1; 1; 2; 2; 3; 3; 4; 5; 8; 9; 11]
// Singles: 5 Pairs: 3
// Sample 2: [1; 2; 2; 3; 4; 5; 5; 6; 6; 7; 7]
// Singles: 3 Pairs: 4
// Sample 3: [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]
// Singles: 11 Pairs: 0




Aucun commentaire:

Enregistrer un commentaire