YAPC::EU 2018 Glasgow Update!
This commit is contained in:
parent
22f33d4004
commit
4e2d22a71d
1170 changed files with 15042 additions and 3047 deletions
14
Task/Entropy/Swift/entropy.swift
Normal file
14
Task/Entropy/Swift/entropy.swift
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Foundation
|
||||
|
||||
func entropy(of x: String) -> Double {
|
||||
return x
|
||||
.reduce(into: [String: Int](), {cur, char in
|
||||
cur[String(char), default: 0] += 1
|
||||
})
|
||||
.values
|
||||
.map({i in Double(i) / Double(x.count) } as (Int) -> Double)
|
||||
.map({p in -p * log2(p) } as (Double) -> Double)
|
||||
.reduce(0.0, +)
|
||||
}
|
||||
|
||||
print(entropy(of: "1223334444"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue