A document from MCS 260 Fall 2021, instructor Emily Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Homework 4

  • Course instructor: Emily Dumas

Instructions:

  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)

Deadline

This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, September 21, 2021.

Topic

This homework assignment is about the topics of worksheet 4, i.e. strings, integers, and list comprehensions.lists, while loops and for loops.

Collaboration

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

The course materials you may refer to for this homework are:

Point distribution

This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:

Points Item
2 Autograder
4 Problem 2
4 Problem 3
10 Total

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

( 1. There's no problem 1 )

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

2. Filtered county names

The state of Illinois is divided into 102 counties, and each county has a name. UIC is in Cook county.

This problem is about finding all counties in Illinois whose names have an even number of characters, and which contain the letter k somewhere in the name. Note that we are only interested in the lower case letter k, and the county names will be capitalized, so we don't consider the county named Kane to meet the criteria.

Below you'll find code that represents a partial solution. It contains a list of all counties, a statement meant to filter down to the counties of interest, and a for loop that will print the results.

Modify the line that contains the comment # <---- FIX THIS LINE so that it uses a list comprehension to find all the counties matching the conditions described above. Do not change anything else in the script. Save your modified script in hwk4prob2.py and submit it to Gradescope.

IMPORTANT: Unlike most MCS 260 problems, this one limits the ways you are allowed to solve the problem. Specifically:

  • Your solution must be based on the provided code
  • You must use a single list comprehension to filter the list of all Illinois counties according to the given criteria
  • You may only change the indicated line of the script
In [ ]:
all_illinois_counties = ['Adams', 'Alexander', 'Bond', 'Boone', 'Brown',
                         'Bureau', 'Calhoun', 'Carroll', 'Cass', 'Champaign',
                         'Christian', 'Clark', 'Clay', 'Clinton', 'Coles',
                         'Cook', 'Crawford', 'Cumberland', 'De', 'DeKalb',
                         'Douglas', 'DuPage', 'Edgar', 'Edwards', 'Effingham',
                         'Fayette', 'Ford', 'Franklin', 'Fulton', 'Gallatin',
                         'Greene', 'Grundy', 'Hamilton', 'Hancock', 'Hardin',
                         'Henderson', 'Henry', 'Iroquois', 'Jackson', 'Jasper',
                         'Jefferson', 'Jersey', 'Jo', 'Johnson', 'Kane',
                         'Kankakee', 'Kendall', 'Knox', 'LaSalle', 'Lake',
                         'Lawrence', 'Lee', 'Livingston', 'Logan', 'Macon',
                         'Macoupin', 'Madison', 'Marion', 'Marshall', 'Mason',
                         'Massac', 'McDonough', 'McHenry', 'McLean', 'Menard',
                         'Mercer', 'Monroe', 'Montgomery', 'Morgan', 'Moultrie',
                         'Ogle', 'Peoria', 'Perry', 'Piatt', 'Pike', 'Pope',
                         'Pulaski', 'Putnam', 'Randolph', 'Richland', 'Rock',
                         'Saline', 'Sangamon', 'Schuyler', 'Scott', 'Shelby',
                         'St. Clair', 'Stark', 'Stephenson', 'Tazewell', 'Union',
                         'Vermilion', 'Wabash', 'Warren', 'Washington', 'Wayne',
                         'White', 'Whiteside', 'Will', 'Williamson', 'Winnebago',
                         'Woodford']

even_len_k_counties = [ ... ]  # <---- FIX THIS LINE
for name in even_len_k_counties:
    print(name)

3. Alphabetically next county

Write a program hwk4prob3.py that lets the user enter a string, which is expected to be the name of a county in the state of Illinois. The program should then print the name of the county in Illinois that comes after the one the user entered (in alphabetical order).

Here is a transcript of what using the program should look like:

Enter a county name: Bond
The alphabetically next county is: Boone

and here is another example:

Enter a county name: Putnam
The alphabetically next county is: Randolph

Your program doesn't need to do any error checking. If the user enters a string that is not a county name, or if they enter Woodford (the alphabetically last county name), then it is acceptable for your program to exit with some kind of error. You don't need to do anything to attempt to detect or handle such an error.

Hint: You can (and should) copy the list of county names from the previous problem into your code. That list is in alphanetical order.

Revision history

  • 2021-09-17 Clarify that we want a case-sensitive match in problem 2
  • 2021-09-16 Revise problem 3 since it was briefly posted along with a solution
  • 2021-09-16 Initial release