Professional Documents
Culture Documents
Winter 2022/2023
Anja Rey
insertion_sort(A[0..n], n):
1 def insertio n_sort ( sequence ) :
Running Time:
28/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Intuition: Derive statements from axioms or previously shown statements using logical implications.
28/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Intuition: Derive statements from axioms or previously shown statements using logical implications.
28/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Intuition: Derive statements from axioms or previously shown statements using logical implications.
Correctness of an Algorithm
An algorithm is correct ≈∆
28/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Intuition: Derive statements from axioms or previously shown statements using logical implications.
Correctness of an Algorithm
An algorithm is correct ≈∆
Axiom
XENA
28/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
correct es Einputofthisform
2 if d < t then
4 return False
run
if
claim
Lied d is la b
blzt.lt d la bl dit
prog continues to line 4
29/50
rot True
Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Loop Invariant
factorial(n):
2 for i Ω 1 to n do
3 x Ωx ·i
4 return x
30/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Loop Invariant
factorial(n):
2 for i Ω 1 to n do
30/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Loop Invariant
factorial(n):
2 for i Ω 1 to n do
Loop invariant
30/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Loop Invariant
factorial(n):
2 for i Ω 1 to n do
Loop invariant
30/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Loop Invariant
factorial(n):
2 for i Ω 1 to n do AG
x Ωx ·i Tool: a statement that holds at the beginning (and
LEANN
Loop invariant
30/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Loop Invariant
factorial(n):
2
µ
for j Ω 2 to n do
I un
of runs of the loop?
30/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Loop Invariant
factorial(n):
for j Ω 2 to n do
jena
termination: implication for end of loop: for j = b + 1, loop condition not satisfied.
30/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Loop Invariant
factorial(n):
2 for j Ω 2 to n do
termination: implication for end of loop: for j = b + 1, loop condition not satisfied.
30/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
dein
j
2ejencoud.h
enH
inaiiFxi
Proof Init 2
Meint us
Aljte look at loop
AG jagen
mit
lassen Alp
then 1.3
Iii
in x
x
j Aljtikolds
dofjusbergofthend
D loopino
use this AG
after
31/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Claim Insertion sort solves
the song probt
input segnen u output
sorted
aserdy
i
genau an
uns
adai.az
auanzjs
Fft see
and
Proof init i
ja
123in
meint
II Tutu
im i
assenAlita j0
1 A key lead
14 candies i Ky
1.5 ATADEAE
AEHM also
MAE
sky Cory is edged
AEHM D D SEIN
We know now Safi after loop O ALT IM
Tor AE Key AEthj
z Key AG
31/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Python: Code Quality I
32/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Python: Code Quality I
Goals
• readability
• maintainability
32/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Python: Code Quality I
Goals
• readability
• maintainability
How do we achieve that?
• consistency (e. g. spacing conventions)
• expressivity (e. g. variable names)
• functions:
• extendability
• unique purpose
• brevity
• testability
• avoiding redundancy
32/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Python: Code Quality I
Goals
• readability
• maintainability
How do we achieve that?
• consistency (e. g. spacing conventions)
• expressivity (e. g. variable names)
• functions:
• extendability
• unique purpose
• brevity
• testability
• avoiding redundancy
; Refactoring
https://xkcd.com/844 (CC BY-NC 2.5)
32/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Testing
33/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Testing
33/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Testing
33/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Testing
33/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Testing
33/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Testing
33/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Testing
33/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Testing
33/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Coding Style Guidelines Overview
3
for i in range ( 1 , 100 ) :
x = 2 * x+i
An 2
3
for i in range (1 , 100) :
x = 2* x + i
Functions: properties:
• short (Æ 6 lines),
• one purpose (otherwise extract a substep)
• testable (well-defined input–output relation, return value)
• as general as possible (extendable, reusable, easy to adapt in the future)
34/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Coding Style Guidelines Overview
3
for i in range ( 1 , 100 ) :
x = 2 * x+i
An 2
3
for i in range (1 , 100) :
x = 2* x + i
Functions: properties:
• short (Æ 6 lines),
• one purpose (otherwise extract a substep)
• testable (well-defined input–output relation, return value)
• as general as possible (extendable, reusable, easy to adapt in the future)
34/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Coding Style Guidelines Overview
3
for i in range ( 1 , 100 ) :
x = 2 * x+i
An 2
3
for i in range (1 , 100) :
x = 2* x + i
Functions: properties:
• short (Æ 6 lines),
• one purpose (otherwise extract a substep)
• testable (well-defined input–output relation, return value)
• as general as possible (extendable, reusable, easy to adapt in the future)
34/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
quiz
35/50 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey