EECS 110: Lec 10: Definite Loops and User Input

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Introduction to Computing Science and Programming I
CS0004: Introduction to Programming Repetition – Do Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Complexity (Running Time)
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Lists Computers and Programming. Agenda What is a list? How to access elements in the list? The for statement Operations on lists Looping with.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Visual Basic Programming
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?
Loops and Simple Functions CS303E: Elements of Computers and Programming.
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
Code Compression the benefits of looping... Today in CS 5 HW 4 - (3 problems) M/T sections W/Th sections due Sunday, 9/26 at midnight due Monday, 9/27.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Spring  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums.
CS 101 – Oct. 15 Common elements in solutions –Types of variables –Traversing values in a list –Assigning vs. checking if equal –Printing things on the.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Sum of Arithmetic Sequences. Definitions Sequence Series.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
EECS 110: Lec 12: Mutable Data
While loop statement condition list
EECS 110: Lec 17: Review for the Final Exam
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 5: List Comprehensions
CS5 Stylin' !.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
While Loops in Python.
EECS 110: Lec 4: Functions and Recursion
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Arrays, For loop While loop Do while loop
EECS 110: Lec 10: Definite Loops and User Input
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
3 Control Statements:.
EECS 110: Lec 4: Functions and Recursion
Programming Languages
For loops Taken from notes by Dr. Neil Moore
More While Loops.
Loops and Simple Functions
Programming Dr. Jalal Kawash.
Spring 2018 EECS 110: Homework IV.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
List Comprehensions Problem: given a list of prices, generate a new list that has a 20% discount to each. Formally: input: list of old prices; output:
CSCE 206 Lab Structured Programming in C
While Loops in Python.
REPETITION Why Repetition?
Lecture 6 - Recursion.
Presentation transcript:

EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University http://networks.cs.northwestern.edu/EECS110-s17/

Loops! [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range?

for! for x in range(8): print('x is', x) print('Phew!') 1 3 2 4 x is assigned each value from this sequence 1 for x in range(8): print('x is', x) print('Phew!') 3 LOOP back to step 1 for EACH value in the list 2 the BODY or BLOCK of the for loop runs with that x 4 Code AFTER the loop will not run until the loop is finished.

four on for for x in range(8): print('x is', x) factorial function? sum the list? construct the list?

Fact with for def fact( n ): answer = 1 for x in range(n): answer = answer * x return answer

Fact with for def fact( n ): answer = 1 for x in range(1,n+1): answer = answer * x return answer

Accumulating an answer… Finding the sum of a list: def sum( L ): """ returns the sum of L's elements """ sum = 0 for x in L: sum = sum + x return sum Accumulator! shortcuts? vs. recursion? sum every OTHER element?

Shortcut Shortcuts for changing variables: k = 38 k = k + 1 k += 1 #shortcut for k = k + 1

Two kinds of for loops sum = 0 for x in L: sum += x L = [ 42, -10, 4 ] Element-based Loops sum = 0 for x in L: sum += x L = [ 42, -10, 4 ] x "selfless"

Two kinds of for loops sum = 0 for x in L: sum += x sum = 0 for i in : Element-based Loops Index-based Loops sum = 0 for x in L: sum += x sum = 0 for i in : sum += i 1 2 L = [ 42, -10, 4 ] L = [ 42, -10, 4 ] x

Two kinds of for loops sum = 0 for x in L: sum += x sum = 0 Element-based Loops Index-based Loops sum = 0 for x in L: sum += x sum = 0 for i in range(len(L)): sum += L[i] i 1 2 L = [ 42, -10, 4 ] L = [ 42, -10, 4 ] x L[i]

Sum every other element Finding the sum of a list: def sum( L ): """ returns the sum of L's elements """ sum = 0 for i in range(len(L)): if ________: sum += L[i] return sum Accumulator! shortcuts? vs. recursion? sum every OTHER element?

Sum every other element Finding the sum of a list: def sum( L ): """ returns the sum of L's elements """ sum = 0 for i in range(len(L)): if i%2 == 0: sum += L[i] return sum Accumulator! shortcuts? vs. recursion? sum every OTHER element?

Extreme Looping What does this code do? print('It keeps on’) while True: print('going and') print('Phew! I\'m done!')

Extreme Looping Anatomy of a while loop: print('It keeps on') while True: print('going and') print('Phew! I\'m done!’) the loop keeps on running as long as this test is True “while” loop This won't print until the while loop finishes - in this case, never! alternative tests?

the loop keeps on running as long as this test is True Extreme Looping Slowing things down… import time print('It keeps on') while True: print('going and') time.sleep(1) print('Phew! I\'m done!') the loop keeps on running as long as this test is True “while” loop

Making our escape! import random escape = 0 while escape != 42: print('Help! Let me out!’) escape = random.choice([41,42,43]) print('At last!’) how could we count the number of loops we run?

Loops aren't just for lists… for c in 'down with CS!': print(c)

"Quiz" def min( L ): n = 0 for c in 'forty-two': if c not in 'aeiou': Names: Write a loop to find and return the min of a list, L def min( L ): L is a list of numbers. What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print(n) Write a loop so that this function returns True if its input is prime and False otherwise: def isPrime( n ): n is a positive integer n = 3 while n > 1: print(n) if n%2 == 0: n = n/2 else: n = 3*n + 1

What do these two loops print? while n > 1: print(n) if n%2 == 0: n = n/2 else: n = 3*n + 1 n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print(n) ??

What do these two loops print? while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 ?? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7

What do these two loops print? while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7

What do these two loops print? while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7

What do these two loops print? while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7

What do these two loops print? while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 16 n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7

What do these two loops print? while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 16 8 n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7

What do these two loops print? while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 16 8 4 n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7

What do these two loops print? while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 16 8 4 2 n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7

def min( L ): def isPrime( n ): L is a list of numbers. n is a positive integer

for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn L is a list of numbers. def isPrime( n ): n is a positive integer

for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn mn=L[0] for s in L: if s < mn: mn = s L is a list of numbers. def isPrime( n ): n is a positive integer

for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn mn=L[0] for s in L: if s < mn: mn = s L is a list of numbers. def isPrime( n ): for i in range (n): if i not in [0,1]: if n%i == 0: return False return True n is a positive integer

Lab 8: the Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c If z does not diverge, c is in the M. Set. Benoit M. Imaginary axis z3 c z1 z2 z4 Real axis z0

Lab 8: the Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c If c does not diverge, it's in the M. Set. Benoit M. Imaginary axis z3 c z1 z2 z4 z0 Real axis example of a non-diverging cycle

Lab 8: the Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c The shaded area are points that do not diverge.