Lecture 24: Loop Invariants
[Online Reading]
CS 1110 Introduction to Computing Using Python
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
Lecture 24: Loop Invariants [Online Reading] CS 1110 Introduction - - PowerPoint PPT Presentation
http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 24: Loop Invariants [Online Reading] CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Announcements Lab
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
2
3
4
condition true false body
precondition postcondition
5
6
precondition: n_forks are needed @ table postcondition: n_forks are needed @ table
7
postcondition: n_forks are needed @ table precondition: n_forks are needed @ table
8
postcondition: n_forks are needed @ table
precondition: n_forks are needed @ table
9
postcondition: n_forks are needed @ table precondition: n_forks are needed @ table
10
k = 2 k <= 5 k = k +1
True False
total = total + k*k
11
12
13
2..1 (empty)
k = 2 k <= 5 k = k +1
True False
total = total + k*k
none
k = 2 k <= 5 k = k +1
True False
total = total + k*k
2..2 2
14
1
k = 2 k <= 5 k = k +1
True False
total = total + k*k
2..3 2, 3
15
2
k = 2 k <= 5 k = k +1
True False
total = total + k*k
2..4 2, 3, 4
16
3
k = 2 k <= 5 k = k +1
True False
total = total + k*k
2..5 2, 3, 4, 5
17
4
k = 2 k <= 5 k = k +1
True False
total = total + k*k
18
19
20
21
22
A: 0..k B: 1..k C: 0..k–1 D: 1..k–1 E: I don’t know
23
A: 0..k B: 1..k C: 0..k–1 D: 1..k–1 E: I don’t know
24
A: n_pair = num adj. equal pairs in s[1..k] B: n_pair = num adj. equal pairs in s[0..k] C: n_pair = num adj. equal pairs in s[1..k–1] D: n_pair = num adj. equal pairs in s[0..k–1] E: I don’t know
25
A: n_pair = num adj. equal pairs in s[1..k] B: n_pair = num adj. equal pairs in s[0..k] C: n_pair = num adj. equal pairs in s[1..k–1] D: n_pair = num adj. equal pairs in s[0..k–1] E: I don’t know
26
A: k = 0 B: k = 1 C: k = –1 D: I don’t know
27
A: k = 0 B: k = 1 C: k = –1 D: I don’t know
28
A: s[k] and s[k+1] B: s[k-1] and s[k] C: s[k-1] and s[k+1] D: s[k] and s[n] E: I don’t know
29
A: s[k] and s[k+1] B: s[k-1] and s[k] C: s[k-1] and s[k+1] D: s[k] and s[n] E: I don’t know
30
31
postcondition: n_pair = # adjacent equal pairs in s[0..len(s)-1] precondition: s is a string
k k-1
32
postcondition: n_pair = # adjacent equal pairs in s[0..len(s)-1] precondition: s is a string
k+1 k
33
34
A: k = 0; big = int_list[0] B: k = 1; big = int_list[0] C: k = 1; big = int_list[1] D: k = 0; big = int_list[1] E: None of the above
35
A: k = 0; big = int_list[0] B: k = 1; big = int_list[0] C: k = 1; big = int_list[1] D: k = 0; big = int_list[1] E: None of the above
36