happy%adalovelace%day% · - ada byron, lady lovelace, 5 january 1841 the anatomy of a for loop for...

Post on 09-Jul-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

10/11/16  

1  

CSE  111  Bio:  Program  Design  I  Lecture  14:  s/ll  loops  

Tanya  Berger-­‐Wolf  (CS)  &  Boris  Igić  (Bio)  University  of  Illinois,  Chicago  

October  11,  2016  

Happy  Ada  Lovelace  Day  We talk much of Imagination. We talk of the Imagination of Poets, the Imagination of Artists etc.; I am inclined to think that in general we don’t know very exactly what we are talking about… It is that which penetrates into the unseen world around us, the world of Science. It is that which feels and discovers what is, the real which we see not, which exists not for our senses. Those who have learned to walk on the threshold of the unknown worlds… may then with the fair white wings of Imagination hope to soar further into the unexplored amidst which we live.

- Ada Byron, Lady Lovelace, 5 January 1841

The Anatomy of a for loop

for myVariable in myList: Do all the stuff that’s Indented beneath the for loop

Stuff at this level of indentationis done afterwards!

Collatz Revisiteddef collatz(n): """Returns n/2 if n is even and returns 3n+1 otherwise"""

if n % 2 == 0: return n/2 else: return 3*n + 1

>>> testNum(16, 10)True

If we start with 16 and apply collatz repeatedly, do we getto 1 within the first 10 repeats?

def testNum(number, repeats):

10/11/16  

2  

Collatz Revisiteddef collatz(n): """Returns n/2 if n is even and returns 3n+1 otherwise"""

if n % 2 == 0: return n/2 else: return 3*n + 1

>>> testNum(16, 10)True

If we start with 16 and apply collatz repeatedly, do we getto 1 within the first 10 repeats?

def testNum(number, repeats): “”” Returns True if the number collatzes within the given number of repeats “”” for i in range(repeats): number = collatz(number) if number == 1: return True return False

Collatz Revisiteddef collatz(n): """Returns n/2 if n is even and returns 3n+1 otherwise"""

if n % 2 == 0: return n/2 else: return 3*n + 1

>>> testNum(16, 10)True

If we start with 16 and apply collatz repeatedly, do we getto 1 within the first 10 repeats?

def testNum(number, repeats): “”” Returns True if the number collatzes within the give number of repeats “”” for i in range(repeats): number = collatz(number) if number == 1: return True return False

Collatz Revisiteddef collatz(n): """Returns n/2 if n is even and returns 3n+1 otherwise"""

if n % 2 == 0: return n/2 else: return 3*n + 1

>>> testNum(16, 10)True

If we start with 16 and apply collatz repeatedly, do we getto 1 within the first 10 repeats?

def testNum(number, repeats): “”” Returns True if the number collatzes within the give number of repeats “”” for i in range(repeats): number = collatz(number) if number == 1: return True else: return False Indentation of else OK?

Collatz Re-Revisited

>>> testConjecture(20, 10)False>>> testConjecture(20, 50)True

def testConjecture(upTo, repeats): “”” Determines if all numbers from 1 to upTo collatz to 1 within given number of repeats”””

for number in range(2, upTo + 1): if ???:

Try all numbers from 1 to 20 Up to this many repeatseach time!

def testNum(number, repeats): “”” Returns True if the number collatzes within the give number of repeats “””

10/11/16  

3  

Collatz Re-Revisited

>>> testConjecture(20, 10)False>>> testConjecture(20, 50)True

def testConjecture(upTo, repeats): “”” Determines if all numbers from 1 to upTo collatz to 1 within given number of repeats”””

for number in range(2, upTo + 1): if not testNum(number, repeats): return False return True

Try all numbers from 1 to 20 Up to this many repeatseach time!

def testNum(number, repeats): “”” Returns True if the number collatzes within the give number of repeats “””

top related