Dana Vrajitoru
C151 Multi-User Operating Systems

C151 Homework 11

Due date: Monday, April 12, 2021.

Ex. 1. A Python Script

Create a file called hw11.py. The entire content of your homework should be in that file.

a. The Function maxList

Copy the following code into your Python file, as seen and explained in the PowerPoint slides:

a = [3, 1, 6, 2, 4, 9, 0]
max = a[0]
for element in a:
   if element > max:
       max = element
print max

This is a piece of code that declares a list containing 7 values, finds the maximum of these values, and then prints it to the terminal. Convert the code except for the first instruction into a function called maxList that takes one parameter called a. The code needs to be indented accordingly.

Add a '__main__' section to this script just like you did in Lab 11. Place the first instruction above (declaring a and assigning it a list) in this section and change the name of the variable a. Change the print statement into a return of the value max. Add a function call to this function with the new name of the list as parameter and print the result.

b. The Function reverseList

Add another function called reverseList that reverses the elements of a list. This function will also take a parameter a that can be assumed to be a list. Get the size of the list from the expression len(a) and then store this length minus 1 in a variable called n. Then add a loop to this function with a control variable i that goes from 0 to n/2. For this you can either use a while loop or a for loop using a range, similar to the one in Lab 11. In the loop, swap the element a[i] with a[n-i] using some temporary variable as intermediate.

Going back to the main, add a call to this function using the same list as parameter, and then print the list after that. Note that you don't need a loop for it. Just using the list variable in the print statement will print out all the elements in a bracket syntax.

Make the script executable and test it from the terminal. Add an example of a test as a comment at the bottom of the script file.

Homework Submission

Upload to Canvas: the file hw11.py.