9A. Iteration with range. Topics: Using for with range Summation Computing Min s Functions and for-loops A Graphics Applications

Similar documents
8. More on Iteration with For

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!

11. Iteration: The while-loop

Example Example Example Example

CS1110 Lab 3 (Feb 10-11, 2015)

6. How Functions Work and Are Accessed. Topics: Modules and Functions More on Importing Call Frames

CS177 Spring Midterm 2. April 02, 8pm-9pm. There are 25 multiple choice questions. Each one is worth 4 points.

Week 3: Simplex Method I

4.4 The Calendar program

n CS 160 or CS122 n Sets and Functions n Propositions and Predicates n Inference Rules n Proof Techniques n Program Verification n CS 161

CSE20: Discrete Mathematics

Lecture 7 Feb 4, 14. Sections 1.7 and 1.8 Some problems from Sec 1.8

Ann-Brith Strömberg. Lecture 4 Linear and Integer Optimization with Applications 1/10

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

CSCE 551 Final Exam, April 28, 2016 Answer Key

Algorithms CMSC Homework set #1 due January 14, 2015

Discrete Mathematical Structures: Theory and Applications

Loop Convergence. CS 536: Science of Programming, Fall 2018

CSE 311: Foundations of Computing. Lecture 26: Cardinality

MCS 260 Exam 2 13 November In order to get full credit, you need to show your work.

2-6 Algebraic Proof. State the property that justifies each statement. 1. If m 1 = m 2 and m 2 = m 3, then m 1 = m 3. SOLUTION:

Homework 3 Solutions, Math 55

16.1 Countability. CS125 Lecture 16 Fall 2014

CSCI-141 Exam 1 Review September 19, 2015 Presented by the RIT Computer Science Community

Chapter 2: The Basics. slides 2017, David Doty ECS 220: Theory of Computation based on The Nature of Computation by Moore and Mertens

CS177 Fall Midterm 1. Wed 10/07 6:30p - 7:30p. There are 25 multiple choice questions. Each one is worth 4 points.

MATH 104A HW 02 SOLUTION KEY

Turing Machine Recap

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

January 18, 2008 Steve Gu. Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,

Theory Bridge Exam Example Questions

Top Down Design. Gunnar Gotshalks 03-1

PS2 - Comments. University of Virginia - cs3102: Theory of Computation Spring 2010

Learning Objectives. Zeroes. The Real Zeros of a Polynomial Function

1.1 Basic Algebra. 1.2 Equations and Inequalities. 1.3 Systems of Equations

Lecture 2 31 Jan Logistics: see piazza site for bootcamps, ps0, bashprob

I have read and understand all of the instructions below, and I will obey the University Code on Academic Integrity.

9th Bay Area Mathematical Olympiad

Decision Problems with TM s. Lecture 31: Halting Problem. Universe of discourse. Semi-decidable. Look at following sets: CSCI 81 Spring, 2012

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Models of Computation, Recall Register Machines. A register machine (sometimes abbreviated to RM) is specified by:

Outline. policies for the second part. with answers. MCS 260 Lecture 10.5 Introduction to Computer Science Jan Verschelde, 9 July 2014

Lesson 12. Student Outcomes. Classwork. Opening Exercise (4 minutes) Discussion (4 minutes)

CS20a: Turing Machines (Oct 29, 2002)

Completing this project will help you learn about two-dimensional arrays, structures, and structure arrays. More on Matlab graphics and GUI design.

Climbing an Infinite Ladder

Algorithms Exam TIN093 /DIT602

CSCC63 Worksheet Turing Machines

Ch 3.2: Direct proofs

1.1.1 Algebraic Operations

9/2/2009 Comp /Comp Fall

1 Definition of a Turing machine

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

Introduction to Python

Mathematical Induction. Section 5.1

COMP 204. Object Oriented Programming (OOP) Mathieu Blanchette

ELECTIVE 2: MATH B String Art and Algebraic Equations

8/29/13 Comp 555 Fall

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

Designing Information Devices and Systems I Fall 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Lecture 4: DNA Restriction Mapping

Procedure for Graphing Polynomial Functions

arxiv: v2 [cs.ds] 17 Sep 2017

Massachusetts Institute of Technology Handout J/18.062J: Mathematics for Computer Science May 3, 2000 Professors David Karger and Nancy Lynch

Discrete Structures: Sample Questions, Exam 2, SOLUTIONS

Today s Topics. Methods of proof Relationships to logical equivalences. Important definitions Relationships to sets, relations Special functions

Assignment 4: Object creation

1. How many labeled trees are there on n vertices such that all odd numbered vertices are leaves?

A Review of Linear Programming

Cosets and Lagrange s theorem

Computer Science Introductory Course MSc - Introduction to Java

CS154, Lecture 10: Rice s Theorem, Oracle Machines

Most General computer?

CSE 105 THEORY OF COMPUTATION

Linear programs Optimization Geoff Gordon Ryan Tibshirani

Recursive Definitions

Systems Analysis and Control

Introduction to Python and its unit testing framework

Prime coordinates on a modulo map, and sine representation. Diego Alonso Cortez Abstract

1 Reals are Uncountable

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

Midterm 1. Name: TA: U.C. Berkeley CS70 : Algorithms Midterm 1 Lecturers: Anant Sahai & Christos Papadimitriou October 15, 2008

8.5 Taylor Polynomials and Taylor Series

Review Exercise 2. 1 a Chemical A 5x+ Chemical B 2x+ 2y12 [ x+ Chemical C [ 4 12]

Read all questions and answers carefully! Do not make any assumptions about the code other than those that are clearly stated.

Prompt. Commentary. Mathematical Foci

Systems Analysis and Control

IE 400 Principles of Engineering Management. The Simplex Algorithm-I: Set 3

CSCE 222 Discrete Structures for Computing. Dr. Hyunyoung Lee

Introduction to data-flow analysis. Data-flow analysis. Control-flow graphs. Data-flow analysis. Example: liveness. Requirements

Complex Numbers. Emily Lawson

= 1 when k = 0 or k = n. Otherwise, Fact 4.5 (page 98) says. n(n 1)(n 2) (n k + 3)(n k + 2)(n k + 1) (n k + 1)(n k + 2)(n k + 3) (n 2)(n 1) n

Package rnmf. February 20, 2015

ECS 20: Discrete Mathematics for Computer Science UC Davis Phillip Rogaway June 12, Final Exam

UNIVERSITETET I OSLO

MA554 Assessment 1 Cosets and Lagrange s theorem

Mathematics 222a Quiz 2 CODE 111 November 21, 2002

{ 0! = 1 n! = n(n 1)!, n 1. n! =

Lab Objective: Introduce some of the basic optimization functions available in the CVXOPT package

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

Transcription:

9A. Iteration with range Topics: Using for with range Summation Computing Min s Functions and for-loops A Graphics Applications

Iterating Through a String s = abcd for c in s: print c Output: a b c d In this example, the for-loop variable is c. One at a time, it takes on the value of each character in s. We learned about this in the previous lecture.

Iterating Through a Range n = 4 for k in range(n): print k Output: 0 1 2 3 How does this work? What does range(n) mean?

Note the Similarities n = 4 for k in range(n): print k s = abcd for c in s: print c Output: 0 1 2 3 Output: a b c d

Summation is a Good Example n = 4 s = 0 for k in range(n): x = 2**k s = s + x print s 1 + 2 + 4 + 8 = 15 Output: 15 We are repeating the purple box 4 times

for-loop Mechanics with range for k in range(4): Loop Body Let k = 0 and then execute the loop body. Let k = 1 and then execute the loop body. Let k = 2 and then execute the loop body. Let k = 3 and then execute the loop body. k is called the loop variable a.k.a. the count variable

Summation n = 4 s = 0 for k in range(n): x = 2**k s = s + x print s 1 + 2 + 4 + 8 = 15 Output: 15 Let s derive this code. It s about adding up powers of two

Summation: How Do We Do It? Let s add up powers of 2 1 = 1 3 = 1 + 2 7 = 1 + 2 + 4 15 = 1 + 2 + 4 + 8 And so on Do we start from scratch each time we generate a new sum?

Summation Let s add up powers of 2 1 = 1 3 = 1 + 2 7 = 1 + 2 + 4 15 = 1 + 2 + 4 + 8 1 = 0 + 1 3 = 1 + 2 7 = 3 + 4 15 = 7 + 8 And so on And so on Nope! We keep a running sum into which we add powers of 2

Summation s = 0 x = 2**0 s = s+x x = 2**1 s = s+x x = 2**2 s = s+x x = 2***3 s = s+x 1 = 0 + 1 3 = 1 + 2 7 = 3 + 4 15 = 7 + 8

Summation s = 0 x = 2**0 s = s+x x = 2**1 s = s+x x = 2**2 s = s+x x = 2**3 s = s+x 1 = 0 + 1 3 = 1 + 2 7 = 3 + 4 15 = 7 + 8 Note the pattern

Summation s = 0 x = 2**0 s = s+x x = 2**1 s = s+x x = 2**2 s = s+x x = 2**3 s = s+x s = 0 for k in range(4): x = 2**k s = s+x print s Let s step through the mechanics of this for-loop

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 0 Initialize the running sum s.

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 0 k -> 0 We enter the loop. The loop variable k is set to zero

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 0 k -> 0 k<4 is true so we execute the loop body with that value of k.

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 1 k -> 0 x -> 1

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 1 k -> 0 x -> 1 k is increased by 1

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 1 k -> 1 x -> 1

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 1 k -> 1 x -> 1 k<4 is true so we execute the loop body with that value of k.

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 3 k -> 1 x -> 2

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 3 k -> 1 x -> 2 k is increased by 1

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 3 k -> 2 x -> 2

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 3 k -> 2 x -> 2 k<4 is true so we execute the loop body with that value of k.

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 7 k -> 2 x -> 4

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 7 k -> 2 x -> 4 k is increased by 1

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 7 k -> 3 x -> 4

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 7 k -> 3 x -> 4 k<4 is true so we execute the loop body with that value of k.

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 15 k -> 3 x -> 8

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 15 k -> 3 x -> 8 k is increased by 1

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 15 k -> 4 x -> 8

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 15 k -> 4 x -> 8 k<4 is False so we exit the loop body and proceed with the next statement after the loop.

1 + 2 + 4 + 8 s = 0 for k in range(4): x = 2**k s = s + x print s s -> 15 k -> 4 x -> 8 Output 15

More General: 1 + 2 + 4 + + 2**(n-1) n = any positive integer s = 0 for k in range(n): x = 2**k s = s+x print s

for-loop Mechanics with range for k in range(n): Loop Body Let k = 0 and then execute the loop body. Let k = 1 and then execute the loop body. Let k = 2 and then execute the loop body. : Let k = n-1 and then execute the loop body.

for-loop Mechanics with range for k in range(n): x = 2**k s = s+x Let k = 0 and then execute the loop body. Let k = 1 and then execute the loop body. Let k = 2 and then execute the loop body. : Let k = n-1 and then execute the loop body.

Counting: A Special Type of Summation

How Many Integers < 10**6 are there that are divisible by 2, 3, and 5? N = 0 for k in range(10**6): if k%2==0 and k%3==0 and k%5==0: N = N+1 print N Output: 33334

Using a For-Loop to Enumerate all Possibilities

Left-Shifting a String Output: s = abcd n = len(s) for k in range(n): t = s[k:]+s[:k] print t abcd bcda cdab dabc If k==2, then s[2:]+s[:2] looks like this: cd + ab Iteration with strings doesn t always have the form for c in s

Looking for a Minimum

Assume this Function is Available def dist(t): Returns a float that is the distance between Earth and a rogue asteroid at time t (days). PreC: t is a nonnegative float.""" Problem: Which of the numbers dist(0), dist(1), dist(2),,dist(100000) is the smallest and what is its value?

Solution d_min = dist(0) t_min = 0 for t in range(100001): d_current = dist(t) if d_current < d_min: # A new minimum is found d_min = d_current # Remember the day it occurred t_min = t print t_min, d_min We need range(100001) because we want to check dist(100000)

More on range In all our examples, the loop variable steps from 0 to some number. There are other options.

Counting from 1 n = 4 for k in range(n): print k n = 4 for k in range(1,n): print k Output: 0 1 2 3 Output: 1 2 3

Counting from Here to (Almost) There Here = 20 There = 24 for k in range(here,there): print k Output: 20 21 22 23

Counting Down Here = 20 There = 24 for k in range(there,here,-1): print k Output: 24 23 22 21

Now Let Us Look at Functions and For Loops

Recall From SimpleMath def sqrt(x): x = float(x) L = x L = (L + x/l)/2 L = (L + x/l)/2 L = (L + x/l)/2 L = (L + x/l)/2 L = (L + x/l)/2 return L Let s implement this with a for-loop

For-Loop Implementation def sqrt(x): x = float(x) L = x L = (L + x/l)/2 L = (L + x/l)/2 L = (L + x/l)/2 L = (L + x/l)/2 L = (L + x/l)/2 return L def sqrt(x): x = float(x) L = x for k in range(5): L = (L + x/l)/2 return L

Another For-Loop Implementation def sqrt(x): x = float(x) L = x for k in range(5): L = (L + x/l)/2 return L def sqrt(x,n=5): x = float(x) L = x for k in range(n): L = (L + x/l)/2 return L Sample Call: y = sqrt(12345,20) The optional argument allows you to determine the number of iterations.

Now Let Us Look at Graphics Procedures and For Loops

Recall DrawRect This will draw a red square with side s and center (xc,yc): DrawRect(xc,yc,s,s,FillColor=RED) This will draw a white square with side s and center (xc,yc): DrawRect(xc,yc,s,s,FillColor=WHITE)

Let s Write a Procedure that Can Draw a Checkered Row Assume n squares each with side s. Assume (x0,y0) is the center of the leftmost square. Let c1 and c2 be the Colors of the first and second square

Solution def DrawRow(x0,y0,s,n,c1,c2): # Center of next square is (xc,yc) xc = x0, yc = y0 for k in range(n): % Draw the kth square if k%2==0: DrawRect(xc,yc,s,s,FillColor=c1) else: DrawRect(xc,yc,s,s,FillColor=c2) xc = xc+s

Now Let s Draw This

This Draws an 8x8 Checker Board y0 = -4; x0 = -3.5; n = 8; s = 1 #(x0,y0) is the center of the leftmost # square in the next row to draw for k in range(n): # Draw the kth row if k%2==0: DrawRow(x0,y0,s,n,RED,WHITE) else: DrawRow(x0,y0,s,n,WHITE,RED) # The next row is s units higher y0 = y0+s