Albion College

CS 171

Introduction to Computer Science I & Lab

Fall 2021

Lab 5: Lists

Goals and Objectives

The main goal this lab is for you to get a more practice with Pythons's lists and loops.

Task

You will write several functions that perform some simple computations using lists.

Background

Lists are the most basic data structure and are used in many programs to store a collection of related items.

Introductions

Introduce yourself to your lab partner(s). Do you have a favorite movie? What movie do you think everyone should watch?

Download the lab

Download the source file Lab05.py. It contains the stubs of Python functions. Open it in the online Python environment.

Getting Started

In this lab, you will practice writing functions that process strings. You should start off with s1 driving and s2 navigating. Complete the functions in Lab05.py. You should change driver/navigator roles after each method is completed.

Seek help as soon as you are experiencing difficulty with this assignment.
Do not wait until the deadline to seek help!

# Lab 5 # YOUR NAMES HERE import random # return a list of n values between first and last inclusive def randomList(n, first, last): a = [] for i in range(n): a.append(random.randrange(first, last+1)) return a def warmup(): fruits = ["banana", "cherry"] print(fruits) # append "durian" at the end of the fruit list using append() # see https://docs.python.org/3/tutorial/datastructures.html # YOUR CODE HERE print(fruits) # ["banana", "cherry", "durian"] # insert "apple" at the beginning of the fruit list using insert() # see https://docs.python.org/3/tutorial/datastructures.html # YOUR CODE HERE print(fruits) # ["apple", "banana", "cherry", "durian"] # append "elderberry" at the end of the fruit list using append() # see https://docs.python.org/3/tutorial/datastructures.html # YOUR CODE HERE print(fruits) # ["apple", "banana", "cherry", "durian", "elderberry"] # remove item with index 2 from the fruit list using the del statement # see https://docs.python.org/3/tutorial/datastructures.html#the-del-statement # YOUR CODE HERE print(fruits) # ["apple", "banana", "durian", "elderberry"] # reverse the order of the list using reverse() # see https://docs.python.org/3/tutorial/datastructures.html#the-del-statement # YOUR CODE HERE print(fruits) # ["elderberry", "durian", "banana", "apple"] # generate a list of random integers between 1 and 100 inclusive data = randomList(10, 1, 100) print(data) # sort data (increasing) using the sort() function # see https://docs.python.org/3/tutorial/datastructures.html # YOUR CODE HERE print(data) # low to high # sort data (decreasing) using the sort() function with reverse=True # see https://docs.python.org/3/tutorial/datastructures.html # YOUR CODE HERE print(data) # high to low return None # return a new list having length n with all elements equal to 0 def zero(n): return [0]*n # return a new list having length n with all elements equal to v def myFill(n, v): return None # return a new list having values # 0, 1, 2, 3, ..., n-2, n-1 def fillNaturals(n): return None # return a new list having values # 0, 1, 4, 9, 16, ... def fillSquares(n): return None # Fibonacci sequence # return a new list having values # 0, 1, 1, 2, 3, 5, 8, 13, ... # note F(i) = F(i-1) + F(i-2) when i >= 2 def fillFibonacci(n): return None # return a new list having values that are the positive factors of n def factors(n): return None # return a new list having values that are the # square the items in the input list def square(A): return None # return the number of occurrence of item in A # don't use the count function def myCount(A, item): return None # return the index of the first occurrence of item in A # don't use index function # return -1 if items is not found def indexOf(A, item): return None # return the index of the first occurrence of item in A # return -1 if items is not found # don't use index function # hint: start at the last element and work down to the first def lastIndexOf(A, item): return None # insert item in A, assuming the elements of A are sorted # v will be placed at index i when A[i-1] <= v <= A[i] # hint: find position where to insert # then use insert function def myInsert(A, item): return None # replace all items of A having value v1 with value v2 def replaceItems(A, v1, v2): return None def main(): print("warmup") warmup() print("zero") print(zero(10)) print("fill") print(myFill(5, 2021)) # [2021, 2021, 2021, 2021, 2021] print("fillNaturals") print(fillNaturals(10)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print("fillSquares") print(fillSquares(10)) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print("fillFibonacci") print(fillFibonacci(10)) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] print("factors") print(factors(10)) # [1, 2, 5, 10] print(factors(24)) # [1, 2, 3, 4, 6, 8, 12, 24] print("square") print(square([1, 3, 5])) # [1, 9, 25] print("myCount") print(myCount([], 0)) # 0 print(myCount([0], 0)) # 1 print(myCount([1], 0)) # 0 print(myCount([1,2,0], 0)) # 1 print(myCount([1,2,3], 0)) # 0 print(myCount([1,2,3,0,3,0,2,0,1,0], 0)) # 4 print("indexOf") print(indexOf([], 0)) # -1 print(indexOf([0], 0)) # 0 print(indexOf([1], 0)) # -1 print(indexOf([1,2,0], 0)) # 1 print(indexOf([1,2,3], 0)) # -1 print(indexOf([1,2,3,0,3,0,2,0,1,0], 0)) # 3 print("lastIndexOf") print(lastIndexOf([], 0)) # -1 print(lastIndexOf([0], 0)) # 0 print(lastIndexOf([1], 0)) # -1 print(lastIndexOf([1,2,0], 0)) # 2 print(lastIndexOf([1,2,3], 0)) # -1 print(lastIndexOf([1,2,3,0,3,0,2,0,1,0], 0)) # 9 print("myInsert") data = [] print(data) # [] myInsert(data, 1) print(data) # [1] myInsert(data, 10) print(data) # [1, 10] myInsert(data, 5) print(data) # [1, 5, 10] myInsert(data, 0) print(data) # [0, 1, 5, 10] myInsert(data, 11) print(data) # [0, 1, 5, 10, 11] print("replaceItems") print(replaceItems([], 1, 2)) # [] print(replaceItems([0, 3, 9], 1, 2)) # [0, 3, 9] print(replaceItems([1, 0, 1, 0, 1, 1], 1, 2)) # [2, 0, 2, 0, 2, 2] main()

Have Fun!

Final Steps

When done, email you lab partner and instructor your lab (.py file).