Download Loop invariants - Introduction to Computing Using Python - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!
While-Loops and Flow
print 'Before while'
count = 0
i = 0
while i < 3:
print 'Start loop '+i
count = count + i
i = i + 1
print 'End loop '
print 'After while'
Output:
Before while
Start loop 0
End loop
Start loop 1
End loop
Start loop 2
End loop
After while
Some Important Terminology
• assertion : true-false statement placed in a program to
assert that it is true at that point
§ Can either be a comment , or an assert command
• precondition : assertion placed before a statement
§ Same idea as function precondition , but more general
• postcondition : assertion placed after a statement
• loop invariant : assertion supposed to be true before
and after each iteration of the loop
§ Distinct from attribute invariant
• iteration of a loop : one execution of its repetend
Assertions versus Asserts
• Assertions prevent bugs
§ Help you keep track of
what you are doing
• Also track down bugs
§ Make it easier to check
belief–code mismatches
• The assert statement is
also an assertion
§ an assertion you are
asking Python to enforce
§ Cannot always convert a
comment to an assert
# x is the sum of 1..n
x? n 3
x? n 0
x? n 1
Comment form The root of the assertion. of all bugs!
Preconditions & Postconditions
• Precondition: assertion
placed before a segment
• Postcondition: assertion
placed after a segment
# x = sum of 1..n-
x = x + n
n = n + 1
# x = sum of 1..n-
precondition postcondition
x contains the sum of these (6)
n
n
x contains the sum of these (10)
Relationship Between Two
If precondition is true, then
postcondition will be true
Solving a Problem
# x = sum of 1..n
n = n + 1
# x = sum of 1..n
precondition postcondition
What statement do you
put here to make the
postcondition true?
A: x = x + 1
B: x = x + n
C: x = x + n+
D: None of the above
E: I don’t know
Remember the new value of n
Invariants: Assertions That Do Not Change
x = 0; i = 2 while i <= 5: x = x + i*i i = i +
# x = sum of squares of 2..
Invariant:
x = sum of squares of 2..i-
in terms of the range of integers
that have been processed so far
i = 2 i <= 5 i = i + true false x = x + i*i The loop processes the range 2.. # invariant
• Loop Invariant : an assertion that is true before and
after each iteration (execution of repetend)
Designing Integer while-loops
# Process integers in a..b
# inv: integers in a..k-1 have been processed
k = a
while k <= b:
process integer k
k = k + 1
# post: integers in a..b have been processed
Command to do something
Equivalent postcondition
true init (^) cond k= k +1; false Process k invariant invariant
Designing Integer while-loops
- Recognize that a range of integers b..c has to be processed
- Write the command and equivalent postcondition
- Write the basic part of the for-loop
- Write loop invariant
- Figure out any initialization
- Implement the repetend (process k)
Process b..c
Initialize variables (if necessary) to make invariant true
Invariant: range b..k-1 has been processed
while k <= c: # Process k k = k + 1
Postcondition: range b..c has been processed
Finding an Invariant
set x to # adjacent equal pairs in s[0..len(s)-1]
invariant: ???
k = 0 while k < len(s):
Process k;
k = k +
x = # adjacent equal pairs in s[0..len(s)-1]
Command to do something Equivalent postcondition
A: 0..k
B: 1..k
C: 0..k–
D: 1..k–
E: I don’t know
A: x = no. adj. equal pairs in s[1..k]
B: x = no. adj. equal pairs in s[0..k]
C: x = no. adj. equal pairs in s[1..k–1]
D: x = no. adj. equal pairs in s[0..k–1]
E: I don’t know
k: next integer to process. Which have been processed? What is the invariant? for s = 'ebeee', x = 2
Reason carefully about initialization
s is a string; len(s) >= 1
Set c to largest element in s
c = ?? k = ??
inv:
while k < len(s):
Process k
k = k+
c = largest char in s[0..len(s)–1]
1. What is the invariant?
2. How do we initialize c and k?
c is largest element in s[0..k–1] Command to do something Equivalent postcondition
An empty set of characters or integers has no maximum. Therefore,
be sure that 0..k–1 is not empty. You must start with k = 1.
A: k = 0; c = s[0]
B: k = 1; c = s[0]
C: k = 1; c = s[1]
D: k = 0; c = s[1]
E: None of the above