DEV Community


Day 15 of 100DaysofCode: A Dummy Project To Find Numbers Of Fraud Voters

iamdurga profile image Durga Pokharel ・3 min read

Today is my 15 day of #100DaysofCode. Today I learned more properties of CSS among them some are avoid colorblindness issues by using sufficient contrast, avoid colorblindness issues by carefully choosing colors that convey information, give links meaning by using descriptive link text, use tabindex to add keyboard to an element etc on Freecodecamp.

Also I learned more about python access on web data from coursera. Did some code regarding to python. I did one project to find the numbers of total voters, unique voters, fraud voters, voters who voted more than once.

Code To Find Numbers Of Fraud Voters

My challenge is given below.

## Challenge 1
You worked hard to get a government job but it is part time only and if you did well, then there is a high chance of getting a permanent placement. And you are assigned to one task. As trainee, you have nothing hard task but a boring one. 
You are assigned to a vote counting board and you have to count valid votes. 

After looking at the past data, you knew that there has been irregularities on vote counting. Some corrupt politician feeds some money to do revote on themself. And the previous authorities didn't knew about that but lucky you, did and this is going to one hell of a promotion. So you knew that, there are 5 parties, A, B, C, D and E and a person's name, age and citizenship id. And there are only few chances that same person has similar name, age and citizenship id. So you will have data like below:
Enter fullscreen mode Exit fullscreen mode
Ram Lama,29,2030-03-01-055,A
Judda Thapa,19,2040-03-01-055,B
Haris Lama,29,2030-03-03-055,B
Aakriti Tiwari,39,2023-03-01-055,C
Ramesh Shah,22,2031-03-01-034,D
Ram Lama,29,2030-03-01-055,A
Enter fullscreen mode Exit fullscreen mode

Problem 1: Find out how many valid votes there has been?

I started with reading file.This way, file is read as one file pointer and it is closed automatically.

with open("vote 1.txt", "r") as fp:
    lines = fp.readlines()

Enter fullscreen mode Exit fullscreen mode

And I used len() to find total voters. For unique voters I used len(set()) this gives non repeated value. After that it is easy to find fraud voters by subtracting total voters with unique voters.

print(f"Total  {len(lines)} votes.")
print(f"Total {len(set(lines))} unique votes.")
print(f"There are {len(lines)-len(set(lines))} fraud votes.")
Enter fullscreen mode Exit fullscreen mode

Problem 2 Who voted more than once?

To find the voters who voted more than once I build one set. There is loop. If count of items more than once then kept in repeated-items. I built a empty dictionary to keep the voters name as key and how many times they voted as value. And my program is given below in my code.

# Who voted more than once
repeated_items = set()
for item in lines:
    if lines.count(item)>1:

occurance = {}

for item in lines:
    if occurance.get(item):
nd = {k: v for k, v in list(reversed(sorted(occurance.items(), key=lambda item: item[1])))}

Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Editor guide