A return value that nobody receives
What does the program print?
def triple(number): number = number * 3 return numberscore = 4triple(score)print(score)60 minutes · 60 points · functions, lists, class, dict, graph, DFS, and BFS
Section A
10 min · 12 pts
Section B
15 min · 18 pts
Section C
35 min · 30 pts
6 questions · 12 points · about 10 minutes. Q1-Q5 are single choice; Q6 is multiple choice.
What does the program print?
def triple(number): number = number * 3 return numberscore = 4triple(score)print(score)What does the program print?
def add_exit(exits): exits.append("east")route = ["north"]add_exit(route)print(route)Which two lines are printed?
scores = {"Ada": 8, "Bo": 5}for value in scores: print(value)What does the final print show?
class Counter: def __init__(self, start): self.value = start def add(self, amount): self.value += amountfirst = Counter(2)second = Counter(5)first.add(3)print(first.value, second.value)Use each neighbour list from left to right. Which is the BFS order? Node X is disconnected.
graph = { "A": ["B", "C"], "B": ["D"], "C": ["E"], "D": ["F"], "E": ["D", "F"], "F": [], "X": [],}Select every correct option. You must choose the complete set for the 2 points.
3 questions · 18 points · about 15 minutes. Precise reasoning matters more than length.
def prepare(path): path.append("C") path = path.copy() path.append("D") return pathroute = ["A", "B"]new_route = prepare(route)print(route)print(new_route)Input visits: ["lab", "hall", "lab", "vault", "lab"]. Required result: {"lab": 3, "hall": 1, "vault": 1}.
Goal: find a route from A to F using the fewest edges. Answer in 4-6 sentences.
graph = { "A": ["B", "C"], "B": ["D"], "C": ["E"], "D": ["F"], "E": ["D", "F"], "F": [], "X": [],}4 questions · 30 points · about 35 minutes. Complete each function in its answer editor.
def positions_at_least(values, limit): # Return a NEW list containing the indexes whose values are >= limit. # Do not change values. passprint(positions_at_least([4, 9, 2, 9], 8))# expected: [1, 3]print(positions_at_least([5, 5], 6))# expected: []def frequency_table(words): # Return a dict that maps each word to the number of times it appears. # An empty input list must return {}. passprint(frequency_table(["red", "blue", "red", "red"]))# expected: {'red': 3, 'blue': 1}print(frequency_table([]))# expected: {}graph = { "A": ["B", "C"], "B": ["D"], "C": ["E"], "D": ["F"], "E": ["D", "F"], "F": [], "X": [],}def dfs_order(graph, start): # Follow each neighbour list from left to right. # Return every reachable node once, in DFS visit order. passprint(dfs_order(graph, "A"))# expected: ['A', 'B', 'D', 'F', 'C', 'E']print(dfs_order(graph, "X"))# expected: ['X']from collections import dequegraph = { "A": ["B", "C"], "B": ["D"], "C": ["E"], "D": ["F"], "E": ["D", "F"], "F": [], "X": [],}def shortest_distance(graph, start, goal): # Return the fewest number of edges from start to goal. # Return 0 when start == goal. # Return -1 when goal cannot be reached. passprint(shortest_distance(graph, "A", "F")) # 3print(shortest_distance(graph, "A", "A")) # 0print(shortest_distance(graph, "F", "A")) # -1