22
2
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.
2I think this could do with a few more awkward cases for testing.
26.7 53.3 20.0
(4 8 3 of 15),48.4 13.7 21.6 6.5 9.8
(74 21 33 10 15 of 153) etc. – Gareth – 2012-04-24T09:44:39.910@Gareth: Good thought. Updated with your test cases. – mellamokb – 2012-04-24T12:06:19.043
shouldn't sum of all votes be 100%? it's not in last four testcases – Ali1S232 – 2012-04-25T18:56:40.273
@Gajet: No it does not always equal 100%. Every time there is a rounding down, you lose up to
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. – mellamokb – 2012-04-25T19:14:10.437I am struggling with the non-trivial answer in VBA (trying since so far, there have been none), but I'm working on it! – Gaffi – 2012-04-26T03:31:58.350
I rescind. [tag:VBA] doesn't like to
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. :-) – Gaffi – 2012-04-26T17:39:15.303