Sunday 9 August 2015

Python - Assginment 1 (Voting Problem)


In my last couple of blogs about Python(details), I elaborated on different data types used in Python and also we designed a mode median and standard deviation calculator.
In this blog we I will take you through an assignment which will cover a great deal of basic part of Python.

Let us define our problem in a very simple way.

(Source : Open Tech School)

We have a survey result of what radish variety customer love the most. You can download the survey result text file here. We want to know couple of things.

a. What is the most popular Radish variety ?
b. Did anyone vote twice ?

Let us design our approach,

What are the things we need to do to answer those two questions.

1. First and most important thing, We have to read the data from the file.

2. Data contains two parts, one the name of the voter and second his vote (i.e. the raddish name)
    we have to split the data in two parts. to get the name and vote.

3. As you can see the data is not clean. It has duplicate entries and unintended space. So, we have to      clean the data.

4. After cleaning we will calculate Who has voted twice.

5. then we will calculate the number of votes for each raddish by eliminating the duplicate entries of people who have voted twice.

6. Finally, we will find the winner (i.e. the Raddish which has received maximum votes.).


Now let us structure our approach. We have defined he flow. In my last blog I have talked about functions. Let us design solution for this problem using functions. So we can understand the code flow easily and also it is a good practice.

Now what functions will we Include ?
The answer is simple. Every step that we have included in the design section will contribute as a function.

We will define functions for the following things :

 - Reading a file
 - Finding Duplicate voter
 - Counting votes
- Finding Winner

Please refer to the below snippet that defines these functions.



We have defined three functions in this snippet. To clean the data, count votes and get the duplicate voter. names[] is a list that will consist of all the customers that voted. So we can check the details about duplicate voter using this list. And Count is a dictionary that will take raddish Names as key and their count as the value.

Moreover, We have to write a separate function to clean the data because, we will have to clean the data for names as well as votes. So , this will avoid repetition of code.

Let us look at below code snippet to get hold of main function.






In the above snippet we are defining three more functions. One is for Finding the winner, second for pretty printing and the third is main.
In PrintResults() function we  are using  a Python functionality of Pretty Printing. As the name suggests our output will be well formatted which is shown later.

Main() reads the data from the file and makes the call to these functions. We have introduced for loop in main to read each and every line of given file.

We will just call the main function using main() argument in Python. and we will get the results.
Let us look at the results.

Results are displayed by running all the functions one by one to get more clarity.
Let us look at the first result of Duplicate Voters.





Vote counting results without Pretty printing.





Vote counting results with Pretty Printing.



Finally, let us see who got the maximum votes.



So, using this code we have answered the two questions that were asked.

Let us summarize the answers :


a. What is the most popular Radish variety  - It is champion
b. Did anyone vote twice  - Yes two people voted twice. (Phoebe Barwell and Procopito Zito)

In my next blog, We will learn how to draw  chart and we will draw a chart for the counts of each raddish variety.

1 comment:

  1. Test cases are well-explained for even a novice like me. Cheers!

    ReplyDelete