BYU logo Computer Science

To start this assignment, download this zip file.

Homework 5c — Counting

1. Voting

Write a program in voting.py that takes

  • a prompt (a string)

The program should read from input a series of votes and then print out a dictionary that counts which person or thing got the most votes.

For example, you could use this program to vote for your favorite foods:

python voting.py food
food: lasagna
food: pizza
food: chicken parmesan
food: lasagna
food: pizza
food: pizza
food:
{'lasagna': 2, 'pizza': 3, 'chicken parmesan': 1}

Or you could use this program to vote for your favorite CS 110 topic:

python voting.py 'CS 110 topic'
CS 110 topic: Dictionaries
CS 110 topic: Bit
CS 110 topic: Bit
CS 110 topic: Dictionaries
CS 110 topic: Bit
CS 110 topic:
{'Dictionaries': 2, 'Bit': 3}

2. Count letters

Write a program in count_letters.py that takes

  • a string of characters to count
  • a file

The program counts how often each character appears in the file.

For example, the file twinkle.txt contains:

Twinkle, twinkle, little star,
how I wonder, what you are!
Up above the world so high,
like a diamond in the sky.
Twinkle, twinkle, little star,
how I wonder what you are!

If you run your program as shown below, you should get the following output:

python count_letters.py twinkle twinkle.txt
{'t': 14, 'w': 11, 'i': 12, 'n': 8, 'k': 6, 'l': 10, 'e': 14}

We recommend using the readfile() function to read the entire file as one long string. You can find an example of this in the guide on counting.

3. Counting values

Write a program in value_count.py that takes

  • a CSV file name
  • a column (integer)

The program counts how many times each value appears in that column of the CSV file.

For example, the start of the file nba_players.csv contains:

14,Ike,Anigbogu,12,IND,Indiana,East,Central,Indiana Pacers,Pacers
25,Ron,Baker,20,NYK,New York,East,Atlantic,New York Knicks,Knicks
47,Jabari,Bird,2,BOS,Boston,East,Atlantic,Boston Celtics,Celtics
67,MarShon,Brooks,15,MEM,Memphis,West,Southwest,Memphis Grizzlies,Grizzlies
71,Lorenzo,Brown,28,TOR,Toronto,East,Atlantic,Toronto Raptors,Raptors

These columns are:

  • id
  • first name
  • last name
  • team id
  • abbreviation
  • city
  • conference
  • division
  • team name
  • team nickname

If you run this program and specify column 6 (starting from zero) this counts all of the players in the East and West conferences:

python value_count.py nba_players.csv 6
{'East': 496, 'West': 504}

If you run this program and specify column 9, then it counts all of the players per team:

python value_count.py nba_players.csv 9
{'Pacers': 24, 'Knicks': 16, 'Celtics': 37, 'Grizzlies': 29, 'Raptors': 22, 'Thunder': 25, 'Pistons': 41, 'Clippers': 36, 'Warriors': 45, '76ers': 38, 'Jazz': 34, 'Kings': 34, 'Cavaliers': 35, 'Rockets': 45, 'Trail Blazers': 33, 'Hawks': 41, 'Magic': 39, 'Bucks': 33, 'Suns': 46, 'Timberwolves': 43, 'Mavericks': 41, 'Nuggets': 46, 'Heat': 39, 'Wizards': 44, 'Nets': 26, 'Hornets': 28, 'Spurs': 18, 'Lakers': 29, 'Bulls': 33}

Note, this problem is similar to the Pokemon problem from the lab, except you need to be able to count values from an arbitrary column, since the column you are using is one of the command line arguments.

Tests

Be sure you can pass the tests before you turn in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.

Grading

ActivityPoints
Voting5
Count letters7
Counting values8