Introduction
Given a set of percentages of choices in a poll, calculate the minimum number of voters there must be in the poll to generate those statistics.
Example: What is your favorite pet?
- Dog:
44.4%
- Cat:
44.4%
- Mouse:
11.1%
Output: 9
(minimum possible # of voters)
Specs
Here are the requirements for your program/function:
- You are given an array of percentage values as input (on stdin, as function argument, etc.)
- Each percentage value is a number rounded to one decimal place (e.g.,
44.4 44.4 11.1
). - Calculate the minimum possible number of voters in the poll whose results would yield those exact percentages when rounded to one decimal place (on stdout, or function return value).
- Bonus: -15 characters if you can solve in a "non-trivial" way (i.e., doesn't involve iterating through every possible # of voters until you find the first one that works)
Example
>./pollreverse 44.4 44.4 11.1
9
>./pollreverse 26.7 53.3 20.0
15
>./pollreverse 48.4 13.7 21.6 6.5 9.8
153
>./pollreverse 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 99.6
2000
>./pollreverse 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 98.7
667
>./pollreverse 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 98.7
2000
>./pollreverse 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 97.8
401
Scoring
This is code-golf, so shortest possible characters wins. Any bonuses are further subtracted from the total character count.
0.5%
from the total, and every time there is a rounding up, you add up to0.5%
to the total. The last four test cases were purposely constructed to optimally exploit this phenomenon. In the first test case that results in2000
, each of the first 9 entries represents1
vote (and are all rounded up0.5%
), whereas the last one represents1991
votes (and is rounded down ~0.5%
). If you calculate those percentages manually and round to 1 decimal place, you will see they are all correct. \$\endgroup\$mod
with non-integers, and the code to correct is getting too long. I can create a working, non-trivial function for SIMPLE percentages, at least. :-) \$\endgroup\$