{{ message }}

Instantly share code, notes, and snippets.

# StewSchrieff/1_explanation.md

Created Dec 20, 2019

# Riddler Express

## https://fivethirtyeight.com/features/can-you-find-a-matching-pair-of-socks/

### Methodology

I've implemented a greedy algorithm that simulates every voter in the office being able to vote for any combination of options A, B, or C. By adding voters until the percentages are reached, we can find the least number of voters possible.

 import math if __name__ == '__main__': a_target = 73 b_target = 58 c_target = 32 a, b, c = 0, 0, 0 a_percent, b_percent, c_percent = 0, 0, 0 total = 0 while a_percent != a_target or b_percent != b_target or c_percent != c_target: # Perform one vote, to the option that is furthest from the target a_diff = a_target - a_percent b_diff = b_target - b_percent c_diff = c_target - c_percent if a_diff > 0: # vote for a a += 1 if b_diff > 0: # vote for b b += 1 if c_diff > 0: # vote for c c += 1 total += 1 # Recalculate all percents a_percent = math.floor(a / total * 100) b_percent = math.floor(b / total * 100) c_percent = math.floor(c / total * 100) print(f'A: {a} votes -> {a_percent}%') print(f'B: {b} votes -> {b_percent}%') print(f'C: {c} votes -> {c_percent}%') print(total) print(f'{a_percent}, {b_percent}, {c_percent}') # time.sleep(5) print(f'The number of people in office: {total}') print(f'A: {a} votes - {a_percent}%') print(f'B: {b} votes - {b_percent}%') print(f'C: {c} votes - {c_percent}%')

# Extra Credit

The following code counts down from 10000 until you find a number of coworkers that doesn't let you satisfy the percentages. We calculate the optimal number of votes by taking ceiling after calculating the target percentage, and multiplying by the number of coworkers. This value is the ideal number of votes (note it cannot be a non-whole number) cast for that holiday party option. Then, we divide by the total coworkers and take the floor to simulate writing down the percentage up to the decimal point. If this value is not equal to the target for each of the holiday options, then that number of coworkers does not contain a possible solution