Lecture 26. Sequence Algorithms (Continued)

Similar documents
Lecture 25. Designing Sequence Algorithms

CS 1110: Introduction to Computing Using Python Sequence Algorithms

CS1110. Lecture 20: Sequence algorithms. Announcements

Announcements for This Lecture

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

Central Algorithmic Techniques. Iterative Algorithms

Foundations of Computation

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Loop Invariants and Binary Search. Chapter 4.4, 5.1

Recap: Prefix Sums. Given A: set of n integers Find B: prefix sums 1 / 86

CS 336. We use the principle of inclusion and exclusion. Let B. = {five digit decimal numbers ending with a 5}, and

Hoare Logic (I): Axiomatic Semantics and Program Correctness

Proof Calculus for Partial Correctness

Top Down Design. Gunnar Gotshalks 03-1

Solutions. Prelim 2[Solutions]

Algorithm Design and Analysis

MATH 137 : Calculus 1 for Honours Mathematics. Online Assignment #2. Introduction to Sequences

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

CS 591, Lecture 9 Data Analytics: Theory and Applications Boston University

CS173 Lecture B, November 3, 2015

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Formal Reasoning CSE 331. Lecture 2 Formal Reasoning. Announcements. Formalization and Reasoning. Software Design and Implementation

May bring 1 sheet of paper with notes!! Both sides!! Printed/handwritten/whatever.. I don t really care

Decision Procedures. Jochen Hoenicke. Software Engineering Albert-Ludwigs-University Freiburg. Winter Term 2016/17

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

(b) What is the variance of the time until the second customer arrives, starting empty, assuming that we measure time in minutes?

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

CSC 344 Algorithms and Complexity. Proof by Mathematical Induction

CS483 Design and Analysis of Algorithms

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

CS 125 Section #12 (More) Probability and Randomized Algorithms 11/24/14. For random numbers X which only take on nonnegative integer values, E(X) =

CSE 421 Greedy Algorithms / Interval Scheduling

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Discrete Structures for Computer Science

COM S 330 Lecture Notes Week of Feb 9 13

Quick Sort Notes , Spring 2010

The beginnings of physics

Announcements Wednesday, October 25

Statistical methods in NLP Introduction

MATH10212 Linear Algebra B Homework Week 5

Axiomatic Semantics: Verification Conditions. Review of Soundness of Axiomatic Semantics. Questions? Announcements

Hardware/Software Co-Design

Hoare Logic and Model Checking

CSE 331 Software Design & Implementation

Axiomatic Semantics. Lecture 9 CS 565 2/12/08

LING 473: Day 3. START THE RECORDING Assignment 1 Review Probability. University of Washington Linguistics 473: Computational Linguistics Fundamentals

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

C241 Homework Assignment 7

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

Hoare Examples & Proof Theory. COS 441 Slides 11

Analysis of Algorithms CMPSC 565

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

Data Structures and Algorithms Chapter 2

Lecture 4. Quicksort

Linear Classification: Linear Programming

Announcements Monday, September 18

Info 2950, Lecture 25

4/26/2017. More algorithms for streams: Each element of data stream is a tuple Given a list of keys S Determine which tuples of stream are in S

Mathematical Induction. How does discrete math help us. How does discrete math help (CS160)? How does discrete math help (CS161)?

Divide and conquer. Philip II of Macedon

CS1110 Lab 3 (Feb 10-11, 2015)

Proving Programs Correct

Data Structure and Algorithm Homework #1 Due: 2:20pm, Tuesday, March 12, 2013 TA === Homework submission instructions ===

CSE 331 Winter 2018 Homework 1

Turing Machines Part II

Lec 05 Arithmetic Coding

What happens to the value of the expression x + y every time we execute this loop? while x>0 do ( y := y+z ; x := x:= x z )

Fast Sorting and Selection. A Lower Bound for Worst Case

= It is easy to see that the largest prime less than 93 is 89. Therefore, the answer is = 4 dollars.

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

How many hours would you estimate that you spent on this assignment?

Divide and Conquer Strategy

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

5. Program Correctness: Mechanics

Week 5: Quicksort, Lower bound, Greedy

SMT and Z3. Nikolaj Bjørner Microsoft Research ReRISE Winter School, Linz, Austria February 5, 2014

Proof by contrapositive, contradiction

CS156: The Calculus of Computation

Verifying Properties of Parallel Programs: An Axiomatic Approach

MATH10212 Linear Algebra B Homework 6. Be prepared to answer the following oral questions if asked in the supervision class:

1 What is the area model for multiplication?

Hoare Logic: Reasoning About Imperative Programs

MAT 300 RECITATIONS WEEK 7 SOLUTIONS. Exercise #1. Use induction to prove that for every natural number n 4, n! > 2 n. 4! = 24 > 16 = 2 4 = 2 n

Turing Machine Variants

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

Searching Algorithms. CSE21 Winter 2017, Day 3 (B00), Day 2 (A00) January 13, 2017

Kartsuba s Algorithm and Linear Time Selection

CS341 info session is on Thu 3/1 5pm in Gates415. CS246: Mining Massive Datasets Jure Leskovec, Stanford University

Randomized algorithms. Inge Li Gørtz

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

ABOUT THE CLASS AND NOTES ON SET THEORY

Summer HSSP Week 1 Homework. Lane Gunderman, Victor Lopez, James Rowan

CS246: Mining Massive Datasets Jure Leskovec, Stanford University

Dynamic Semantics. Dynamic Semantics. Operational Semantics Axiomatic Semantics Denotational Semantic. Operational Semantics

Lab 2: 2.1. If a>b+c or b>a+c or c > a+b: print 'Cannot be arranged to make a triangle!' else: print 'Can be arranged to make a triangle!

Final Exam. Take-Home Portion. enter class. No answers will be accepted for any reason after 8:30. NO EXCEP-

CS 231: Algorithmic Problem Solving

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Use mathematical induction in Exercises 3 17 to prove summation formulae. Be sure to identify where you use the inductive hypothesis.

Transcription:

Lecture 26 Sequence Algorithms (Continued)

Announcements for This Lecture Lab/Finals Lab 12 is the final lab Can use Consulting hours Due next Wednesday 9:30 Final: Dec 10 th 2:00-4:30pm Study guide is posted Announce reviews next week. Conflict with Final time? Submit to conflict to CMS by next TUESDAY! Assignments A6 is now graded Mean: 91.5 Median: 95 Std Dev: 12.9 Mean: 11 hr Median: 10 hr Std Dev: 5.7 hr SEVERAL AI hearings A7 is due Tuesday Dec. 4 Due at midnight Some extensions possible 11/27/18 Sequences (Continued) 2

Recall: Horizontal Notation 0 k len(b) b <= sorted >= Example of an assertion about an sequence b. It asserts that: 1. b[0..k 1] is sorted (i.e. its values are in ascending order) 2. Everything in b[0..k 1] is everything in b[k..len(b) 1] b 0 h k Given index h of the first element of a segment and index k of the element that follows that segment, the number of values in the segment is k h. b[h.. k 1] has k h elements in it. h h+1 (h+1) h = 1 11/27/18 Sequences (Continued) 3

Partition Algorithm Given a sequence b[h..k] with some value x in b[h]: h k pre: b x? Swap elements of b[h..k] and store in j to truthify post: h i i+1 k post: b <= x x >= x inv: b h i j k <= x x? >= x Agrees with precondition when i = h, j = k+1 Agrees with postcondition when j = i+1 11/27/18 Sequences (Continued) 4

Partition Algorithm Implementation def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]""" i = h; j = k+1; x = b[h] # invariant: b[h..i-1] < x, b[i] = x, b[j..k] >= x while i < j-1: if b[i+1] >= x: # Move to end of block. swap(b,i+1,j-1) j = j - 1 else: # b[i+1] < x swap(b,i,i+1) i = i + 1 # post: b[h..i-1] < x, b[i] is x, and b[i+1..k] >= x return i partition(b,h,k), not partition(b[h:k+1]) Remember, slicing always copies the list! We want to partition the original list 11/27/18 Sequences (Continued) 5

Partition Algorithm Implementation def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]""" i = h; j = k+1; x = b[h] # invariant: b[h..i-1] < x, b[i] = x, b[j..k] >= x while i < j-1: if b[i+1] >= x: # Move to end of block. swap(b,i+1,j-1) j = j - 1 else: # b[i+1] < x swap(b,i,i+1) i = i + 1 # post: b[h..i-1] < x, b[i] is x, and b[i+1..k] >= x return i <= x x? >= x h i i+1 j k 1 2 3 1 5 0 6 3 8 11/27/18 Sequences (Continued) 6

Partition Algorithm Implementation def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]""" i = h; j = k+1; x = b[h] # invariant: b[h..i-1] < x, b[i] = x, b[j..k] >= x while i < j-1: if b[i+1] >= x: # Move to end of block. swap(b,i+1,j-1) j = j - 1 else: # b[i+1] < x swap(b,i,i+1) i = i + 1 # post: b[h..i-1] < x, b[i] is x, and b[i+1..k] >= x return i <= x x? >= x h i i+1 j k 1 2 3 1 5 0 6 3 8 h i i+1 j k 1 2 1 3 5 0 6 3 8 11/27/18 Sequences (Continued) 7

Partition Algorithm Implementation def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]""" i = h; j = k+1; x = b[h] # invariant: b[h..i-1] < x, b[i] = x, b[j..k] >= x while i < j-1: if b[i+1] >= x: # Move to end of block. swap(b,i+1,j-1) j = j - 1 else: # b[i+1] < x swap(b,i,i+1) i = i + 1 # post: b[h..i-1] < x, b[i] is x, and b[i+1..k] >= x return i <= x x? >= x h i i+1 j k 1 2 3 1 5 0 6 3 8 h i i+1 j k 1 2 1 3 5 0 6 3 8 h i j k 1 2 1 3 0 5 6 3 8 11/27/18 Sequences (Continued) 8

Partition Algorithm Implementation def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]""" i = h; j = k+1; x = b[h] # invariant: b[h..i-1] < x, b[i] = x, b[j..k] >= x while i < j-1: if b[i+1] >= x: # Move to end of block. swap(b,i+1,j-1) j = j - 1 else: # b[i+1] < x swap(b,i,i+1) i = i + 1 # post: b[h..i-1] < x, b[i] is x, and b[i+1..k] >= x return i <= x x? >= x h i i+1 j k 1 2 3 1 5 0 6 3 8 h i i+1 j k 1 2 1 3 5 0 6 3 8 h i j k 1 2 1 3 0 5 6 3 8 h i j k 1 2 1 0 3 5 6 3 8 11/27/18 Sequences (Continued) 9

Dutch National Flag Variant Sequence of integer values red = negatives, white = 0, blues = positive Only rearrange part of the list, not all pre: b post: b h h? < 0 = 0 > 0 k k h t i j k inv: b < 0? = 0 > 0 11/27/18 Sequences (Continued) 10

Dutch National Flag Variant Sequence of integer values red = negatives, white = 0, blues = positive Only rearrange part of the list, not all pre: b h? k post: b h < 0 = 0 > 0 h t i j k inv: b < 0? = 0 > 0 k pre: t = h, i = k+1, j = k post: t = i 11/27/18 Sequences (Continued) 11

Dutch National Flag Algorithm def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = k+1, j = k; # 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: swap(b,i-1,t) t = t+1 elif b[i-1] == 0: else: i = i-1 swap(b,i-1,j) i = i-1; j = j-1 # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) < 0? = 0 > 0 h t i j k -1-2 3-1 0 0 0 6 3 11/27/18 Sequences (Continued) 12

Dutch National Flag Algorithm def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = k+1, j = k; # 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: swap(b,i-1,t) t = t+1 elif b[i-1] == 0: else: i = i-1 swap(b,i-1,j) i = i-1; j = j-1 # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) < 0? = 0 > 0 h t i j k -1-2 3-1 0 0 0 6 3 h t i j k -1-2 3-1 0 0 0 6 3 11/27/18 Sequences (Continued) 13

Dutch National Flag Algorithm def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = k+1, j = k; # 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: swap(b,i-1,t) t = t+1 elif b[i-1] == 0: else: i = i-1 swap(b,i-1,j) i = i-1; j = j-1 # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) < 0? = 0 > 0 h t i j k -1-2 3-1 0 0 0 6 3 h t i j k -1-2 3-1 0 0 0 6 3 h t i j k -1-2 -1 3 0 0 0 6 3 11/27/18 Sequences (Continued) 14

Dutch National Flag Algorithm def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = k+1, j = k; # 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: swap(b,i-1,t) t = t+1 elif b[i-1] == 0: else: i = i-1 swap(b,i-1,j) i = i-1; j = j-1 # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) < 0? = 0 > 0 h t i j k -1-2 3-1 0 0 0 6 3 h t i j k -1-2 3-1 0 0 0 6 3 h t i j k -1-2 -1 3 0 0 0 6 3 h t j k -1-2 -1 0 0 0 3 6 3 11/27/18 Sequences (Continued) 15

Changing the Invariant Different invariants = different code Need to change how we initialize, stop Also need to change the body of the loop pre: b post: b h h? < 0 = 0 > 0 k k h t i j k inv: b < 0 = 0? > 0 11/27/18 Sequences (Continued) 16

Changing the Invariant Different invariants = different code Need to change how we initialize, stop Also need to change the body of the loop pre: b h? k post: b h < 0 = 0 > 0 h i t j k inv: b < 0 = 0? > 0 k pre: t = h, i = h, j = k post: t = j+1 11/27/18 Sequences (Continued) 17

Changing the Invariant def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = h, j = k; # inv: b[h..t-1] < 0, b[i..t-1] = 0, b[t..j]?, b[j+1..k] > 0 while t < j+1: if b[???] < 0:??? < 0 = 0? > 0 h i t j k -1-2 0 0 3-1 0 6 3 elif b[???] == 0:??? else:??? # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) 11/27/18 Sequences (Continued) 18

Changing the Invariant def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = h, j = k; # inv: b[h..t-1] < 0, b[i..t-1] = 0, b[t..j]?, b[j+1..k] > 0 while t < j+1: if b[t] < 0:??? < 0 = 0? > 0 h i t j k -1-2 0 0 3-1 0 6 3 elif b[t] == 0:??? else:??? # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) 11/27/18 Sequences (Continued) 19

Changing the Invariant def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = h, j = k; # inv: b[h..t-1] < 0, b[i..t-1] = 0, b[t..j]?, b[j+1..k] > 0 while t < j+1: if b[t] < 0:??? < 0 = 0? > 0 h i t j k -1-2 0 0 3-1 0 6 3 h i t j k -1-2 0 0 0-1 3 6 3 elif b[t] == 0:??? else: swap(b,t,j) j = j-1 # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) 11/27/18 Sequences (Continued) 20

Changing the Invariant def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = h, j = k; # inv: b[h..t-1] < 0, b[i..t-1] = 0, b[t..j]?, b[j+1..k] > 0 while t < j+1: if b[t] < 0:??? elif b[t] == 0: else: t = t+1 swap(b,t,j) j = j-1 # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) < 0 = 0? > 0 h i t j k -1-2 0 0 3-1 0 6 3 h i t j k -1-2 0 0 0-1 3 6 3 h i t/j k -1-2 0 0 0-1 3 6 3 11/27/18 Sequences (Continued) 21

Changing the Invariant def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = h, j = k; # inv: b[h..t-1] < 0, b[i..t-1] = 0, b[t..j]?, b[j+1..k] > 0 while t < j+1: if b[t] < 0: swap(b,t,i) i = i+1; t = t+1; elif b[t] == 0: else: t = t+1 swap(b,t,j) j = j-1 # post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0 return (i, j) < 0 = 0? > 0 h i t j k -1-2 0 0 3-1 0 6 3 h i t j k -1-2 0 0 0-1 3 6 3 h i t/j k -1-2 0 0 0-1 3 6 3 h i j t k -1-2 -1 0 0 0 3 6 3 11/27/18 Sequences (Continued) 22

Changing the Invariant def dnf(b, h, k): """Returns: partition points""" t = h; i = h, j = k; # b[h..t-1] <, b[i..t-1] =, b[t..j]?, b[j+1..k] > while t < j+1: if b[t] < 0: swap(b,t,i) i = i+1; t = t+1; elif b[t] == 0: else: t = t+1 swap(b,t,j) j = j-1 # b[h..i-1] <, b[i..j] =, b[j+1..k] > return (i, j) VS def dnf(b, h, k): """Returns: partition points""" t = h; i = k+1, j = k; # b[h..t-1] <, b[t..i-1]?, b[i..j] =, b[j+1..k] > while t < i: if b[i-1] < 0: swap(b,i-1,t) t = t+1 elif b[i-1] == 0: else: i = i-1 swap(b,i-1,j) i = i-1; j = j-1 # b[h..i-1] <, b[i..j] =, b[j+1..k] > return (i, j) 11/27/18 Sequences (Continued) 23

Now we have four colors! Flag of Mauritius Negatives: red = odd, purple = even Positives: yellow = odd, green = even pre: b h? k post: b inv: b h k < 0 odd < 0 even 0 odd 0 even h r s i t k < 0, o < 0, e 0, o? 0, e 11/27/18 Sequences (Continued) 24

Flag of Mauritius < 0, o < 0, e 0, o? 0, e h r s i t k -1-3 -2-4 7 5-5 -6 1 0 2 4 h r s i t k -1-3 -5-4 7 5-2 -6 1 0 2 4 One swap is not good enough 11/27/18 Sequences (Continued) 25

Flag of Mauritius < 0, o < 0, e 0, o? 0, e h r s i t k -1-3 -2-4 7 5-5 -6 1 0 2 4 h r s i t k -1-3 -5-4 -2 5 7-6 1 0 2 4 Need two swaps for two spaces 11/27/18 Sequences (Continued) 26

Flag of Mauritius < 0, o < 0, e 0, o? 0, e h r s i t k -1-3 -2-4 7 5-5 -6 1 0 2 4 h r s i t k -1-3 -5-4 -2 5 7-6 1 0 2 4 And adjust the loop variables 11/27/18 Sequences (Continued) 27

Flag of Mauritius < 0, o < 0, e? 0, e h r=s i t k -1-3 -7-4 -2-6 -5 1 0 2 4 h r=s i t k -1-3 -7-5 -2-6 -4 1 0 2 4 BUT NOT ALWAYS! 11/29/18 Sorting 28

Flag of Mauritius < 0, o < 0, e? 0, e h r=s i t k -1-3 -7-4 -2-6 -5 1 0 2 4 h r=s i t k -1-3 -7-4 -2-6 -5 1 0 2 4 BUT NOT ALWAYS! Have to check if second swap is okay 11/29/18 Sorting 29

Flag of Mauritius < 0, o < 0, e 0, o? 0, e h r s i t k -1-3 -2-4 7 5-5 -6 1 0 2 4 h r s i t k -1-3 -5-4 -2 5 7-6 1 0 2 4 See algorithms.py for Python code 11/27/18 Sequences (Continued) 30

Flag of Mauritius < 0, o < 0, e 0, o? 0, e h r s i t k -1-3 -2-4 7 5-5 -6 1 0 2 4 h r s i t k -1-3 -5-4 -2 5 7-6 1 0 2 4 See algorithms.py for Python code h r s i t k -1-3 -5-4 -2-6 7 5 1 0 2 4 11/27/18 Sequences (Continued) 31

Flag of Mauritius < 0, o < 0, e 0, o? 0, e h r s i t k -1-3 -2-4 7 5-5 -6 1 0 2 4 h r s i t k -1-3 -5-4 -2 5 7-6 1 0 2 4 See algorithms.py for Python code h r s i t k -1-3 -5-4 -2-6 7 5 1 0 2 4 h r s i t k -1-3 -5-4 -2-6 7 5 1 0 2 4 11/27/18 Sequences (Continued) 32

Extras Not Covered in Class 11/27/18 Sequences (Continued) 33

Loaded Dice Sequence p of length n represents n-sided die Contents of p sum to 1 p[k] is probability die rolls the number k 1 2 3 4 5 6 0.1 0.1 0.1 0.1 0.3 0.3 weighted d6, favoring 5, 6 Goal: Want to roll the die Generate random number r between 0 and 1 Pick p[i] such that p[i-1] < r p[i] 0.1 0.1 0.1 0.1 0.3 0.3 0.1 0.2 0.3 0.4 0.7 1.0 11/27/18 Sequences (Continued) 34

Loaded Dice Want: Value i such that p[i-1] < r <= p[i] pre: b post: b 0 n? 0 i n r > sum r <= sum 0 i n inv: b r > sum? Same as precondition if i = 0 Postcondition is invariant + false loop condition 11/27/18 Sequences (Continued) 35

Loaded Dice def roll(p): """Returns: randint in 0..len(p)-1; i returned with prob. p[i] Precondition: p list of positive floats that sum to 1.""" r = random.random() # r in [0,1) # Think of interval [0,1] divided into segments of size p[i] # Store into i the segment number in which r falls. i = 0; sum_of = p[0] # inv: r >= sum of p[0].. p[i 1]; pend = sum of p[0].. p[i] while r >= sum_of: sum_of = sum_of + p[i+1] i = i + 1 # post: sum of p[0].. p[i 1] <= r < sum of p[0].. p[i] return i r < sum Analyzing the Loop 1. Does the initialization make inv true? 2. Is post true when inv is true and condition is false? 3. Does the repetend make progress? 4. Does the repetend keep inv true? 0 r is not here pend inv 1 0 r post 1 p[0] p[1] p[i] p[n 1] p[0] p[1] p[i] p[n 1]

Reversing a Sequence h k pre: b not reversed post: b h reversed k change: b h k 1 2 3 4 5 6 7 8 9 9 9 9 h k into b 9 9 9 9 8 7 6 5 4 3 2 1 h i j k inv: b swapped not reversed swapped