A201 Introduction to Programming 1

A201 Homework 10

Due Date: Tuesday, November 14, 2023.

Goal: to use lists in Python.

Make sure to complete Lab 10 before doing the homework.

Create a Python script file called hw10.py. Add your name at the top as a comment, along with the class name and date.

Ex. 1. a. Days of the Week

Create a list containing strings for the days of the week, capitalized and in quotes. Store it in a global variable called week at the top of the program.

a. Next Day

Write a function called nextDay that receives one string representing one of the days and returns the next day. At the top of the function, declare the variable week as global.

First, the function should find the index of the day in the list week. If the parameter of the function is called day, the index can be found with the expression
week.index(day)
Store this value in another variable.

After that, use this index to return the next element in the list week: if the index is between 0 and 5, return the element at the position index + 1. Otherwise, if the index is 6, return the first element in the list (at position 0).

Note that this function does not require a loop.

b. Previous Day

Write another function called previousDay that also takes a parameter representing a day as a string, and returns the previous day. Just like in the first one, start by getting the index of the day from the week list. Then return the element of the list at position index-1. Since negative indexes go backward from the end of the list, you don't have to check the value of the index in this one.

c. 3-letter Names

Add a function called shortNames that takes one parameter that is a list of strings, and returns another list where all the strings are shortened to the first 3 letters. For this, use the slice operator seen in the lecture the last week, and the mapping operator discussed this week.

For example, if day contains the value "Monday", then day[1:5] will return the string "onda". Then use the mapping operator to apply a slice with the appropriate parameters to extract the first 3 letters from the string, where the string goes into the list that is the parameter, and collect the results into another list.

d. Test

Define a main function where you ask the user to enter a day, store it in a variable, then call the two functions nextDay and previousDay with the variable, and output the results.

Add a function call to the function shortNames with the variable week as the parameter, and output the result.

Add the usual syntax at the end with a call to the function main():

if __name__ == '__main__':
    main()

Test the program a few times to cover all possible cases.

Homework Submission

Upload the files lab10.py and hw10.py in Canvas, Assignments, Homework 10.