.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, September 21, 2021.
This homework assignment is about the topics of worksheet 4, i.e. strings, integers, and list comprehensions.lists, while loops and for loops.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
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 |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
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.
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:
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 = [county for county in all_illinois_counties if len(county) % 2 == 0 and "k" in county]
for name in even_len_k_counties:
print(name)
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.
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']
startcounty = input("Enter a county name: ")
# Find where the entered county appears in the list
index = all_illinois_counties.index(startcounty)
# Find the county that appears after the given county in the list
nextcounty = all_illinois_counties[index + 1]
print("The alphabetically next county is:", nextcounty)