[PYTHON] Calculation of the minimum required number of votes from turnout

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)

content of study

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.

Improvement points

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

Calculation of the minimum required number of votes from turnout
Calculation of the number of Klamer correlations
Visualize the number of complaints from life insurance companies
10. Counting the number of lines
Get the number of digits
Calculate the number of changes
Examine the margin of error in the number of deaths from pneumonia
How to calculate the amount of calculation learned from ABC134-D
Get the number of views of Qiita
Existence from the viewpoint of Python
Get the number of Youtube subscribers
[Python] Yuriko Koike Calculate the number of votes you need exactly [matplotlib]
[Python] Calculate the number of digits required when filling in 0s [Note]
Learning notes from the beginning of Python 1
Omit BOM from the beginning of the string
Learning notes from the beginning of Python 2
Count the number of characters with echo
Let Code Day10 Starting from Zero "1431. Kids With the Greatest Number of Candies"
Get the contents of git diff from python
Output the number of CPU cores in Python
About the accuracy of Archimedean circle calculation method
Calculate the total number of combinations with python
Divide the string into the specified number of characters
Find the number of days in a month
Change the decimal point of logging from, to.
Finding the beginning of Abenomics from NT magnification 2
Minimize the number of polishings by combinatorial optimization
Extract only complete from the result of Trinity
Determine the number of classes using the Starges formula
Finding the beginning of Abenomics from NT magnification 1
From the introduction of pyethapp to the execution of contract
The transition of baseball as seen from the data
The story of moving from Pipenv to Poetry
A python script that gets the number of jobs for a specified condition from indeed.com
[Python] Representing the number of complaints from life insurance companies in a bar graph
Bayesian inference concept (3) ... Calculation of change points in the number of emails received by PyMC3