/
lec10prob1.py
74 lines (57 loc) · 1.55 KB
/
lec10prob1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# lec10prob1.py
#
# Answers to questions are answered online. Definitions and questions
# included here to help understand problems.
# In this problem, we'll examine how indirection works.
# Consider the following definitions:
a = [1, 2, 3, 4, 0]
b = [3, 0, 2, 4, 1]
c = [3, 2, 4, 1, 5]
# 1. What is the value of the following expressions? If you think
# there will be an error, please type in 'error' (without quotes)
# in the input box.
# I've wrapped them in print() statements to show values
print(a[0])
print(b[1])
print(a[a[1]])
print(b[b[2]])
print(a[b[2]])
print(c[a[b[3]]])
print(a[c[a[b[0]]]])
print(a[c[a[b[3]]]])
# 2. Assume we have defined the following function:
def foo(L):
val = L[0]
while (True):
val = L[val]
# Which of the following statement(s) will result in an infinite loop?
print(foo(a))
print(foo(b))
print(foo(c))
# 3. Consider the following code:
# Note that this code will not run as-s with num is set to '???'
# Commented it out and included lines for each possible value.
# Comment out desired assignment for num to run
# num = ???
num = 0
# num = 1
# num = 3
# num = 5
L = [5, 0, 2, 4, 6, 3, 1]
val = 0
for i in range(0, num):
val = L[L[val]]
print val
# 1) What is the smallest value that num can be such that the number 3 is printed?
# 0
# 1
# 3
# 5
# Impossible
# 2) Now, we redefine L to be:
L = [2, 0, 1, 5, 3, 4]
# What is the smallest value that num can be such that the number 3 is printed?
# 0
# 3
# 5
# Impossible