Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
28
Task/Knuths-algorithm-S/Swift/knuths-algorithm-s.swift
Normal file
28
Task/Knuths-algorithm-S/Swift/knuths-algorithm-s.swift
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import Darwin
|
||||
|
||||
func s_of_n_creator<T>(n: Int) -> T -> [T] {
|
||||
var sample = [T]()
|
||||
var i = 0
|
||||
return {(item: T) in
|
||||
i++
|
||||
if (i <= n) {
|
||||
sample.append(item)
|
||||
} else if (Int(arc4random_uniform(UInt32(i))) < n) {
|
||||
sample[Int(arc4random_uniform(UInt32(n)))] = item
|
||||
}
|
||||
return sample
|
||||
}
|
||||
}
|
||||
|
||||
var bin = [Int](count:10, repeatedValue:0)
|
||||
for trial in 0..<100000 {
|
||||
let s_of_n: Int -> [Int] = s_of_n_creator(3)
|
||||
var sample: [Int] = []
|
||||
for i in 0..<10 {
|
||||
sample = s_of_n(i)
|
||||
}
|
||||
for s in sample {
|
||||
bin[s]++
|
||||
}
|
||||
}
|
||||
println(bin)
|
||||
Loading…
Add table
Add a link
Reference in a new issue