Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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