Skip to content

Instantly share code, notes, and snippets.

@MaxHalford
Last active February 28, 2020 10:36
Show Gist options
  • Save MaxHalford/12eb3dd9f3d6eddb5eb007a722392dac to your computer and use it in GitHub Desktop.
Save MaxHalford/12eb3dd9f3d6eddb5eb007a722392dac to your computer and use it in GitHub Desktop.
Find count(s) from percentage(s)
import numpy as np
goal_fracs = np.array([.1, .9])
goal_fracs = np.array([.04, .04, .11, .14, .67])
counts = np.zeros_like(goal_fracs)
n = 1
tol = .001
a = 0
while True:
fracs = counts / n
if np.allclose(fracs, goal_fracs, atol=tol):
break
if sum(fracs) < 1:
i = np.argmin(fracs - goal_fracs)
counts[i] += 1
else:
n += 1
counts, n
goal_frac = .2
k = 0
n = 1
tol = .01
while True:
frac = k / n
if math.isclose(frac, goal_frac, abs_tol=tol):
break
if frac < goal_frac:
k += 1
else:
n += 1
k, n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment