Unit 2

Lists and Methods

Code Reading/Tracing

[11.1] What do the following lines of code evaluate to?


a. [1,2,3]+[4,5,6]
b. [1,2,3]*4
c. "one" in ["one", "two", "three"]
d. 3 in ["one", "two", "three"]
e. len(["one", "two", "hellow", "world"])

[11.2] What do the following lines evaluate to?


lst = [50, 40, 12, 20, 60, 30, 72]
a. lst[0]
b. lst[1]
c. lst[1:3]
d. lst[len(lst)-1]
e. lst[0:len(lst):3]

[11.3] What do the following lines evaluate to?


lst = ["one", "two", "hellow", "world"]
a. lst[1]
b. lst[1][2]

[11.4] What is the value of list after the following code runs?


lst = [2, 4, 6, 8]
lst.remove(4)
lst.pop(2)

print(lst)

[11.5] What will the following code print once it is done executing?



lst = ["h", "e", "l", "l", "o"]

lst[0] = "m"
print("change 1: ", lst)

lst[2] = "k"
print("change 2: ", lst)

lst.append("a")
lst.append("b")
lst.append("c")
print("change 3: ", lst)

lst.pop()
print("change 4: ", lst)

[11.6] What will the following code print?


lst = [20, 3, 50, 19, 34, 1, 42]
for i in range(len(lst)):
    print("i: ", i, " value: ", lst[i])

[11.7] What will the following evaluate to?



lst = [[1,2,3], [4,5], [6,7,8,9], [10]]
a. lst[0]
b. lst[3]
c. lst[0][1]
d. lst[2][2]

[11.8] What will the following print?



lst = [[1,1], [2,3,4]]

lst[0][0] = 55
print("change 1: ", lst)

lst[1].append(99)
print("change 2: ", lst)

[11.9] What will the following code print?



g = [[1,2,3], [4,5], [6,7], [8]]

for i in range(len(g)):
    print("i: ", i, " outer list element", g[i])
    for j in range(len(g[i])):
        print("     j: ", j, " inner list element", g[i][j])

[11.10] What is the output of the following code?



lst = [[1,1], [2,3,4]]

x = 0
i = 1
for j in range(len(lst[0])):
    x = x + lst[i][j]

print(x)

[11.11] What does the following Python function do with the 2D list grid?

def process_grid(grid):
    result = 0
    for i in range(len(grid)):
        for j in range(len(grid[0])):
            if (i + j) % 2 == 0:
                result += grid[i][j]
    return result

Options:

A. Calculates the sum of elements in even rows and columns.

B. Calculates the sum of elements at positions where the sum of row and column indices is even.

C. Counts the number of elements that are even.

D. Finds the maximum element where the sum of row and column indices is even.

[11.12] Which of the following statements accurately describes a destructive operation on a 1D list?

A. A new list is created, and the original list remains unchanged.

B. The operation modifies the original list by adding or removing elements.

C. The list's elements are rearranged and saved in a new list.

D. A copy of the list is made for backup while the original list is used for processing.

[11.13] True or False: A non-destructive function always returns a new list and does not change the original list.

[11.14] Describe what it means for a function to be non-destructive in the context of 1D list manipulation.

[11.15] Match the following operations with either "Destructive" or "Non-Destructive" and a short description why:

  1. list.append(4)
  2. sorted(list)
  3. list.pop()
  4. list + [4, 5]

[11.16] What will each of the following code snippets evaluate to?

  1. [ 5 ] * 3
  2. ["a", "b", "c"][1]
  3. min([5, 1, 8, 2])
  4. max([5, 1, 8, 2])
  5. sum([5, 1, 8, 2])

[11.17] What will the value of lst be at the end of the following operations? Why? How is it different from the exercise below?

 
lst = [ "a", "b", "c" ]
lst2 = lst
print("lst: " , lst, " - lst2: ", lst2)

lst[1] = "foo"
print("lst: " , lst, " - lst2: ", lst2)

lst.append("b") 
print("lst: " , lst, " - lst2: ", lst2)
    

[11.18] What will the value of lst be at the end of the following operations? Why? How is it different from the above exercise?

 
lst = [ "a", "b", "c" ]
lst2 = lst
print("lst: " , lst, " - lst2: ", lst2)

lst = lst[0:1] + ["foo"] + lst[2:]
print("lst: " , lst, " - lst2: ", lst2)

lst = lst + ["b"]
print("lst: " , lst, " - lst2: ", lst2)
    

[11.19] At the end of this set of operations, which lists will be aliased? What values will each variable hold?

 
1. a = [ 1, 2, "x", "y" ]
2. b = a
3. c = [ 1, 2, "x", "y" ]
4. d = c
5. a.pop(2)
6. b = b + [ "woah" ]
7. c[0] = 42
8. d.insert(3, "yowza")
    

[11.20] What will the value of x be at the end of the code below? Why?

 
def destructiveDouble(lst):
    for i in range(len(lst)):
        lst[i] = lst[i] * 2 

x = [1, 2, 3]
destructiveDouble(x)
print(x)
    

[11.21] What will the value of x and y be at the end of the code below? Why?

 
def nonDestructiveDouble(lst):
    result = [ ]
    for i in range(len(lst)):
        result.append(lst[i] * 2) 
    return result

x = [1, 2, 3]
y = nonDestructiveDouble(x)
print(x, y) 
    

[11.22] What will the code below print? Why?

 
def fun(lst1, lst2):
    r = [ ]
    for i in range(len(lst1)):
        lst1[i] = lst1[i] + 10
        r.append(lst2[i] * 2)
    print("lst1: ", lst1)
    print("lst2: ", lst2)
    return r

x = [1, 2, 3]
y = [3, 4, 5]
z = fun(x, y)

print("x: ", x)
print("y: ", y)
print("z: ", z)

    

Strings vs Lists

[11.23] What will the following do?


s = "hello"
lst = ["a", "b", "c"]
print("s[0]:", s[0])
print("lst[0]:", lst[0])

print("len(s):", len(s))
print("len(lst):", len(lst))

[11.24] What will the following do?

s = "0.2 0.8 0.05 0.13"
lst = s.split()
print(lst)

[11.25] What will the following do?

lst = ["Pittsburgh", "NYC", "Baltimore"]
s = ", ".join(lst)

print(s)

[11.26] What is the output of the following code?



def mystery(s, lst):
    s = s + "world"
    lst = lst + [99]
    

s1 = "hello"
lst1 = [1,2,3]

mystery(s1, lst1)
print(s1, lst1)

Code Writing

[11.27] Write a line of code to do each of the following:


a. Create a list with the numbers 20, 3, 50, 19 in it
b. Create a list with the strings “one”, “two”, “three”
c. Create a list with values of different types

[11.28] Write a function called findSum that takes as a parameter a list of numbers (integers and floats) and returns their sum.

[11.29] Write a function called findMax that takes as a parameter a list of integers and returns the maximum number.

[11.30] Write a function called removeAll that takes a list as a parameter and a value, and removes all occurrences of the given value from the given list.

[11.31] Write a function that takes a list of numbers and returns True if the list is sorted in ascending order or False otherwise.

[11.32] Write a function that takes a 2D list and returns a list of the sums of each row.

[11.33] Write a function that finds and prints the minimum and maximum values in a 2D list.

[11.34] Write a function to check if a 2D list is a square matrix (the number of rows and columns are equal).

[11.35] Write a function called countVal that takes a list and a value as a paramter and returns how many times the value is present in the list or -1 if the value is not in the list.

[11.36] Write a function called addAll that takes a 2 dimensional list of numbers and finds the sum of all the numbers in it.

[11.37] Write a function that shifts a list by k elements, where k < n. For instance, given an array [1,2,3,4,5,6,7] and k = 3, the array should be modified to [5,6,7,1,2,3,4].

Example:

Input: nums = [1,2,3,4,5,6,7], k = 3

Output: [5,6,7,1,2,3,4]

[11.38] Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. You must do this destructively.

Example:

Input: [0,1,0,3,12]

Output: [1,3,12,0,0]

[11.39] Given a list of positive integers, find the second largest number in the list.

Example:

Input: [10, 5, 20, 8]

Output: 10

[11.40] Given a list of integers, rearrange the list so that all odd numbers come before all even numbers, but the relative order of odd and even numbers should be preserved. you may use non-destructive operations.

Example:

Input: [1, 2, 3, 4, 5, 6, 7]

Output: [1, 3, 5, 7, 2, 4, 6]

[11.41] Write a function that takes a 1D list and returns a new list that is the reverse of the original list. Do this both non-destructively and destructively.

Example:

Input: [1, 2, 3, 4]

Output: [4, 3, 2, 1]

[11.42] Write a function that takes a sorted 1D list of integers and returns a new list with all duplicates removed, preserving the original order of elements. The original list should remain unchanged (non-destructive).

Example:

Input: [1, 2, 2, 3, 4, 4, 5]

Output: [1, 2, 3, 4, 5]

[11.43] Write a function that replaces all elements in a 1D list greater than a given value n with n itself, modifying the list in place (destructive).

Example:

Input: nums = [2, 3, 5, 8, 1], n = 4

Output: nums should become [2, 3, 4, 4, 1]