Dana Vrajitoru
C151 Multi-User Operating Systems

C151 Homework 12

Due date: Monday, April 19, 2021.

Ex. 1 Python functions

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

a. Create a list

Write a Python function called makeList with one parameter n that generates a list of size n. In this function, generate the numbers from 1 to n and store them in the list. Use a while loop or a for loop for this. Make the function return the resulting list. You can start by storing an empty list in a variable (for example, a = []). Then use the function append inside the loop to add more elements to the end of the list (for example, a.append(i), where i is the control variable for the loop).

b. Shuffle the list

Write a function called shuffleList that receives an list as parameter and shuffles it. As a reminder, if a is an list, then len(a) will give you its size. The function should use a for loop going from 0 to size-1 (use the range for it), and for each position, let's say i, generate a random integer between i and size-1. Then if the random position is different from i, it should swap a[i] with the element at that random position. You'll have to import the appropriate module for this (see Lab 12). The function should return the shuffled list.

c. Calling the functions

Add a __main__ section to the script (like in the lab). Ask the user for the size of the list and store it in some variable. Then call the function declared at point a), store the result in a variable, and print the result. Then call the function shuffleList to randomize the list, and print the result again after that (with some explanation).

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

Homework Submission

Upload to Canvas: the file hw12.py.