home / system / tools_source

Menu
  • GraphQL API
  • Search all tables

tools_source: 14

This data as json

id tool_id filename language content
14 2 entropy.py py #!/usr/bin/env python3 """entropy — calculate shannon entropy of input""" import math import sys from collections import Counter def entropy(data: bytes) -> float: if not data: return 0.0 counts = Counter(data) total = len(data) return -sum((c / total) * math.log2(c / total) for c in counts.values()) def main(): if len(sys.argv) > 1: with open(sys.argv[1], "rb") as f: data = f.read() else: data = sys.stdin.buffer.read() print(f"{entropy(data):.6f}") if __name__ == "__main__": main()
Powered by Datasette · 1.148ms · krisyotam.com