Page MenuHomeDevCentral

D2263.id5685.diff
No OneTemporary

D2263.id5685.diff

diff --git a/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution b/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution
new file mode 100755
--- /dev/null
+++ b/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+import math
+import sys
+
+
+def binomial_coefficient(n, k):
+ f = math.factorial
+ return f(n) / f(k) / f(n - k)
+
+
+def compute(population_size, success_states_count, draws_count, observed_successes):
+ N = population_size
+ K = success_states_count
+ n = draws_count
+ k = observed_successes
+
+ bc = binomial_coefficient
+
+ return bc(K, k) * bc(N - K, n - k) / bc(N, n)
+
+
+def usage():
+ print(f"Usage: {sys.argv[0]} <population size> <number of success states> <number of draws> <number of observed successes>", file=sys.stderr)
+
+
+def help():
+ usage()
+ print()
+ print("Example:")
+ print()
+ print("In a Magic the Gathering deck of 60 crads, you've put 23 lands.")
+ print("To compute the probability to get 3 lands in your opening hand (7 cards):")
+ print(f" {sys.argv[0]} 60 23 7 3")
+
+
+def has_help_arg():
+ help_args = ["-h", "--help", "/?", "/help"]
+ return any([help_arg in sys.argv[1:] for help_arg in help_args])
+
+
+if __name__ == "__main__":
+ if has_help_arg():
+ help()
+ sys.exit(0)
+
+ if len(sys.argv) != 5:
+ usage()
+ sys.exit(1)
+
+ try:
+ numbers = [float(arg) for arg in sys.argv[1:]]
+ probability = compute(*numbers)
+ print(probability)
+ except ValueError as e:
+ print(e)
+ usage()
+ sys.exit(2)

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 29, 00:28 (14 h, 27 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2270300
Default Alt Text
D2263.id5685.diff (1 KB)

Event Timeline