Reverse Engineer Polling Statistics
Python, 154
def p(x):
n=[1]*len(x);d=2;r=lambda z:round(1000.*z/d)/10
while 1:
if(map(r,n),sum(n))==(x,d):return d
d+=1
for i in range(len(x)):n[i]+=r(n[i])<x[i]
It works for the last example now.
Example runs:
>>> p([44.4, 44.4, 11.1])
9
>>> p([26.7, 53.3, 20.0])
15
>>> p([48.4, 13.7, 21.6, 6.5, 9.8])
153
>>> p([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 99.6])
2000
>>> p([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 98.7])
667
>>> p([0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 98.7])
2000
>>> p([0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 97.8])
401
Python, 154
def r(l):
v=0
while 1:
v+=1;o=[round(y*v/100)for y in l];s=sum(o)
if s:
if all(a==b for a,b in zip(l,[round(y*1000/s)/10for y in o])):return s
J, 57 characters
t=:".>'1'8!:0|:100*%/~i.1001
{.I.*/"1(t{~i.#t)e."1~1!:1[1
Used the trivial method. It takes input from the keyboard. t
creates a lookup table and the second line looks for the input within the table. I can provide an expanded explanation of the code if anyone's interested.
I had looked into using the percentage to create a fraction then get the lowest form of the fraction to figure out the number, but I couldn't figure out a way to make it work with the rounding of the results.