






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Key points in this Introduction to Computing Using Python lecture are: Sequence Algorithms, Sorting, Key Algorithmic Family, Famous Sorting Function, Pictorial Notation, Sequence Assertions, Partition Algorithm, Partition Algorithm Implementation, Developing Algorithms On Sequences, Dutch National Flag Algorithm . Objectives of this course are: 1.Fluency in (Python) procedural programming 2. Competency in object-oriented programming 3. Knowledge of searching and sorting algorithms
Typology: Slides
1 / 12
This page cannot be seen from the preview
Don't miss anything!







1 Also, computing poker-hand scores.
Clicker Q2: base case Clicker Q1: recursive case
Clicker Q2: base case Clicker Q1: recursive case
def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]; Return index of pivot point. Assume a swap function _swap(b,ind1, ind2). Pre: k>=h""" **CLICKER Q
while CLICKER Q if b[i+1] >= x:
b[i+1], b[j-1] = b[j-1], b[i+1] j = j - 1 else: # b[i+1] < x CLICKER Q
return i
def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]""" i = h; j = k+1; x = b[h]
while i < j-1: if b[i+1] >= x:
b[i+1], b[j-1] = b[j-1], b[i+1] j = j - 1 else: # b[i+1] < x b[i], b[i+1] = b[i+1], b[i] i = i + 1
return i
h i i+1 j k <= x x? >= x 1 2 1 3 5 0 6 3 8 h i i+1 j k 1 2 1 3 0 5 6 3 8 h i j k 1 2 1 0 3 5 6 3 8 h i j k
Sequence of h..k of red (<0), white (=0), blue (>0) "pixels" Arrange to put <0 first, then =0 , then >0, return "split pts" ? h k pre: b <0 =0 > h k post: b (values in h..k are unknown) inv : b <0? =0 > h t i j k b[h..t-1] <0, b[t..i-1] unknown, b[i..j] =0, b[j+1..k] >
def dnf(b, h, k): """(DNF explanation omitted for space.) Returns: split-points as a tuple (i,j)"""
# inv: b[h..t-1] < 0, b[t..i-1] ?, b[i..j] = 0, b[j+1..k] > 0 while t < i: if b[i-1] < 0:
elif b[i-1] == 0:
else:
return (i, j)
h t i j k -1 -2 3 -1 0 0 0 6 3 h t i j k
h t i j k -1 -2 -1 0 0 0 3 6 3 h i j k h t j k