There are many days when I see accounts that solicit votes on various SNS. When looking at the voting ratio in% notation, as soon as I was wondering how many people are at least that ratio in order to measure credibility
I wrote a program to calculate the minimum required number of votes from turnout.
--Enter the voting rate of one of the two-choice votes as an integer value --Calculate the minimum number of votes for each from the greatest common divisor of the vote rate
The flow.
I gave it as a record of the results of the learning process. It's childish, but it's a mess.
ration_voters.py
# -*- coding: utf-8 -*-
import math
a = int(input("1~Integer of 100"))
b = int(100 - a)
a_s, b_s = str(a), str(b)
print("Voting ratio" + a_s + ' : ' + b_s)
a_div = []
b_div = []
def divisor(side):
#Calculate divisor and insert into list
n = int(side)
div = []
for i in range(2, n+1):
num = float(n)
ni = num / i
f = math.modf(ni)
if f[0] == 0:
div.insert(0, i)
return div
a_div = divisor(a)
b_div = divisor(b)
a_set = set(a_div)
b_set = set(b_div)
t = a_set & b_set
#Calculate and output the minimum required number of votes from the divisor
if t == set():
print("At least 100 people vote.")
else:
max_divisor = max(t)
a_max_div, b_max_div = a / max_divisor, b / max_divisor
saitei = str(a_max_div + b_max_div)
print("At least," + saitei + "It is a vote by a person.")
str_a_max_div, str_b_max_div = str(a_max_div), str(b_max_div)
print("Voting ratio" + str_a_max_div + ' : ' + str_b_max_div)
I wrote it while researching various things from the basic grammar of python. I thought about the calculation part of the divisor by myself, but I realized that there was a library after I finished writing. This is also part of my study.
I learned how to insert Japanese comments, handle math.modf, get intersections from multiple lists, and create arrays.
As the content --Make it possible to handle voter turnout as a float type. ――Be able to handle up to 4 voting options.
I want to implement the above.
I want to be able to make things smarter, such as naming conventions and processing procedures.
I'm still immature in the first post, but thank you for watching until the end. That's the record.
Recommended Posts