Skip to content

Instantly share code, notes, and snippets.

@damienstanton
Last active June 9, 2020 17:45
Show Gist options
  • Save damienstanton/d06e0bca635b6ce93c3a7dde81e52272 to your computer and use it in GitHub Desktop.
Save damienstanton/d06e0bca635b6ce93c3a7dde81e52272 to your computer and use it in GitHub Desktop.
Entropy methods for String
import Foundation
extension String {
/// Returns the minimum number of bits required to encode the given string
func bitFloor() -> Int { Int(entropy().rounded(.up)) }
/// Returns the metric entropy of the given string, as a measure of randomness
func randomness() -> Double { entropy() / Double(self.count)}
/// Returns the approximate Shannon entropy of the given string
func entropy() -> Double {
var freq = [Character: Double]()
self.map { freq[$0, default: 0] += 1 }
let prob = Array(freq.values.map { Double($0 / Double(self.count)) })
let ce = prob.map { Double($0) * log2(Double($0)) / log2(2.0) }
return Double(abs(ce.reduce(0, +)))
}
}
assert("2A2D84FD-CAE9-4753-93AC-F1E0BBE14D43".bitFloor() == 4)
assert("DamienStanton".bitFloor() == 4)
assert("abc".bitFloor() == 2)
assert("Bb".bitFloor() == 1)
assert("x".bitFloor() == 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment