Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
17
Task/Knuth-shuffle/F-Sharp/knuth-shuffle-1.fs
Normal file
17
Task/Knuth-shuffle/F-Sharp/knuth-shuffle-1.fs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
open System
|
||||
|
||||
let FisherYatesShuffle (initialList : array<'a>) = // '
|
||||
let availableFlags = Array.init initialList.Length (fun i -> (i, true))
|
||||
// Which items are available and their indices
|
||||
let rnd = new Random()
|
||||
let nextItem nLeft =
|
||||
let nItem = rnd.Next(0, nLeft) // Index out of available items
|
||||
let index = // Index in original deck
|
||||
availableFlags // Go through available array
|
||||
|> Seq.filter (fun (ndx,f) -> f) // and pick out only the available tuples
|
||||
|> Seq.nth nItem // Get the one at our chosen index
|
||||
|> fst // and retrieve it's index into the original array
|
||||
availableFlags.[index] <- (index, false) // Mark that index as unavailable
|
||||
initialList.[index] // and return the original item
|
||||
seq {(initialList.Length) .. -1 .. 1} // Going from the length of the list down to 1
|
||||
|> Seq.map (fun i -> nextItem i) // yield the next item
|
||||
10
Task/Knuth-shuffle/F-Sharp/knuth-shuffle-2.fs
Normal file
10
Task/Knuth-shuffle/F-Sharp/knuth-shuffle-2.fs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
let KnuthShuffle (lst : array<'a>) = // '
|
||||
let Swap i j = // Standard swap
|
||||
let item = lst.[i]
|
||||
lst.[i] <- lst.[j]
|
||||
lst.[j] <- item
|
||||
let rnd = new Random()
|
||||
let ln = lst.Length
|
||||
[0..(ln - 2)] // For all indices except the last
|
||||
|> Seq.iter (fun i -> Swap i (rnd.Next(i, ln))) // swap th item at the index with a random one following it (or itself)
|
||||
lst // Return the list shuffled in place
|
||||
2
Task/Knuth-shuffle/F-Sharp/knuth-shuffle-3.fs
Normal file
2
Task/Knuth-shuffle/F-Sharp/knuth-shuffle-3.fs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> KnuthShuffle [| "Darrell"; "Marvin"; "Doug"; "Greg"; "Sam"; "Ken" |];;
|
||||
val it : string array = [|"Marvin"; "Doug"; "Sam"; "Darrell"; "Ken"; "Greg"|]
|
||||
Loading…
Add table
Add a link
Reference in a new issue