Strings are amongst the
most popular types in Python. We can create them simply by enclosing characters
in quotes. Python treats single quotes the same as double quotes.
Problem : To solve the vote counting problem
As a vegetable retailer we have to find most required radish variety of the vegetable by the customers.
The data file contains 300 line with two variable which are character datatype , separated by hyphen .
The name of the file is "radishsurvey.txt".
we are creating an empty "dictionary" counts to store the vote counts and an empty "list" voted to track the duplicate voters. Comments in the below code explain the purpose of every step.
The optional argument to "split" is the number of times to split the string, not the number of parts to split it into. So splitting it one time creates two strings, splitting it two times creates three strings, etc. A little "got you" moment for the unwary Python programmer!
The data file contains 300 line with two variable which are character datatype , separated by hyphen .
The name of the file is "radishsurvey.txt".
# Main objective of this study is to find out the following:
- What's the most popular radish variety?
- What are the least popular?
- Did anyone vote twice?
# Things covered in the program:
- Reading the data
- Operations
- Looping
- Use of list and Dictionary
# Approach Towards the problem
Save the file radishsurvey.txt to your computer. How do we write a program to find out which person voted for each radish preference.we are creating an empty "dictionary" counts to store the vote counts and an empty "list" voted to track the duplicate voters. Comments in the below code explain the purpose of every step.
The optional argument to "split" is the number of times to split the string, not the number of parts to split it into. So splitting it one time creates two strings, splitting it two times creates three strings, etc. A little "got you" moment for the unwary Python programmer!
Counting votes for each radish variety is a bit time consuming, you have to know all the names in advance and you have to loop through the file multiple times. How about if you could automatically find all the varieties that were voted for, and count them all in one pass?
You'll need a data structure where you can associate a radish variety with the number of votes counted for it. A dictionary would be perfect!
Then to check if any one has voted twice
You will need to start making a list of the names of everyone who has voted so far. Each time you see a new name, check if it is already in the list of names. Starting with an empty list of names, you can use
voterlist.append(newentry)
to append a new entry to the end.
Then need to apply the same data munging techniques to clean up people's names,
Then to find the winner at the end .
Then our program prints the number of votes cast for each radish variety, but it doesn't declare a winner, So we can use a for loop which iterates over all of the keys in a dictionary.
Her is the Output for this problem :-
Phoebe Barwell has already votedProcopio Zito has already voted
{'April cross': 72,
'Bunny tail': 72,
'Champion': 76,
'Cherry belle': 58,
'Daikon': 63,
'French breakfast': 72,
'Plum purple': 56,
'Red king': 56,
'Sicily giant': 57,
'Snow belle': 63,
'White icicle': 64}
('Champion', 76)
Conclusion :
Champion is the most popular variety and Red King and Plum Purple are the least popular.
Reference:http://opentechschool.github.io/python-data-intro/core/strings.html
No comments:
Post a Comment