Vous êtes sur la page 1sur 11

15-112: Fundamentals of Programming 2/24/18, 4(43 AM

CMU 15-112: Fundamentals of Programming and Computer Science


Class Notes: Loops

1. for loops
2. nested for loops
3. while loops
4. break and continue
5. isPrime
6. nthPrime

1. for loops  video


def sumFromMToN(m, n):
total = 0
# note that range(x, y) includes x but excludes y
for x in range(m, n+1):
total += x
return total

print(sumFromMToN(5, 10) == 5+6+7+8+9+10)

 Select  Visualize  Run

Actually, we don't need a loop here...

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 1 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

def sumFromMToN(m, n):


return sum(range(m, n+1))

print(sumFromMToN(5, 10) == 5+6+7+8+9+10)

# And we can even do this with a closed-form formula,


# which is the fastest way, but which doesn't really
# help us demonstrate loops. :-)

def sumToN(n):
# helper function
return n*(n+1)//2

def sumFromMToN_byFormula(m, n):


return (sumToN(n) - sumToN(m-1))

print(sumFromMToN_byFormula(5, 10) == 5+6+7+8+9+10)

 Select  Visualize  Run

What if we omit the first parameter?


def sumToN(n):
total = 0
for x in range(n+1):
total += x
return total

print(sumToN(5) == 0+1+2+3+4+5)

 Select  Visualize  Run

What if we add a third parameter?


def sumEveryKthFromMToN(m, n, k):
total = 0
for x in range(m, n+1, k):
total += x
return total

print(sumEveryKthFromMToN(5, 20, 7) == (5 + 12 + 19))

 Select  Visualize  Run

Sum just odd numbers from m to n:

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 2 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

def sumOfOddsFromMToN(m, n):


total = 0
for x in range(m, n+1):
if (x % 2 == 1):
total += x
return total

print(sumOfOddsFromMToN(4, 10) == sumOfOddsFromMToN(5,9) == (5+7+9))

 Select  Visualize  Run

Once again:
def sumOfOddsFromMToN(m, n):
if (m % 2 == 0):
# m is even, add 1 to start on an odd
m += 1
total = 0
for x in range(m, n+1, 2):
total += x
return total

print(sumOfOddsFromMToN(4, 10) == sumOfOddsFromMToN(5,9) == (5+7+9))

 Select  Visualize  Run

And again:
# Here we will range in reverse
# (not wise in this case, but instructional)

def sumOfOddsFromMToN(m, n):


if (n % 2 == 0):
# n is even, subtract 1 to start on an odd
n -= 1
total = 0
for x in range(n, m-1, -2): # be careful here!
total += x
return total

print(sumOfOddsFromMToN(4, 10) == sumOfOddsFromMToN(5,9) == (5+7+9))

 Select  Visualize  Run

And again:

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 3 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

def sumOfOddsFromMToN(m, n):


if (m % 2 == 0): m += 1
return sum(range(m, n+1, 2))

print(sumOfOddsFromMToN(4, 10) == sumOfOddsFromMToN(5,9) == (5+7+9))

 Select  Visualize  Run

And again:
# This is the worst way so far -- too confusing.

def sumOfOddsFromMToN(m, n):


return sum(range(m + (1 - m%2), n+1, 2)) # this works, but it's gross!

print(sumOfOddsFromMToN(4, 10) == sumOfOddsFromMToN(5,9) == (5+7+9))

 Select  Visualize  Run

2. Nested for loops  video


def printCoordinates(xMax, yMax):
for x in range(xMax+1):
for y in range(yMax+1):
print("(", x, ",", y, ") ", end="")
print()

printCoordinates(4, 5)

 Select  Visualize  Run

How about some stars?


def printStarRectangle(n):
# print an nxn rectangle of asterisks
for row in range(n):
for col in range(n):
print("*", end="")
print()

printStarRectangle(5)

 Select  Visualize  Run

And again:

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 4 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

# What would this do? Be careful and be precise!

def printMysteryStarShape(n):
for row in range(n):
print(row, end=" ")
for col in range(row):
print("*", end=" ")
print()

printMysteryStarShape(5)

 Select  Visualize  Run

3. while loops  video


# use while loops when there is an indeterminate number of iterations

def leftmostDigit(n):
n = abs(n)
while (n >= 10):
n = n//10
return n

print(leftmostDigit(72658489290098) == 7)

 Select  Visualize  Run

Example: nth positive integer with some property

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 5 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

# eg: find the nth number that is a multiple of either 4 or 7


def isMultipleOf4or7(x):
return ((x % 4) == 0) or ((x % 7) == 0)

def nthMultipleOf4or7(n):
found = 0
guess = -1
while (found <= n):
guess += 1
if (isMultipleOf4or7(guess)):
found += 1
return guess

print("Multiples of 4 or 7: ", end="")


for n in range(15):
print(nthMultipleOf4or7(n), end=" ")
print()

 Select  Visualize  Run

Misuse: While loop over a fixed range


# sum numbers from 1 to 10
# note: this works, but you should not use "while" here.
# instead, do this with "for" (once you know how)
def sumToN(n):
# note: even though this works, it is bad style.
# You should do this with a "for" loop, not a "while" loop.
total = 0
counter = 1
while (counter <= n):
total += counter
counter += 1
return total

print(sumToN(5) == 1+2+3+4+5)

 Select  Visualize  Run

Once again, but with a bug!:

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 6 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

def buggySumToN(n):
# note: this not only uses a "while" instead of a "for" loop,
# but it also contains a bug. Ugh.
total = 0
counter = 0
while (counter <= n):
counter += 1
total += counter
return total

print(buggySumToN(5) == 1+2+3+4+5)

 Select  Visualize  Run

And once again, with a for loop:


# A for loop is the preferred way to loop over a fixed range.
def sumToN(n):
total = 0
for counter in range(n+1):
total += counter
return total

print(sumToN(5) == 1+2+3+4+5)

 Select  Visualize  Run

4. break and continue  video


for n in range(200):
if (n % 3 == 0):
continue # skips rest of this pass (rarely a good idea to use "continue")
elif (n == 8):
break # skips rest of entire loop
print(n, end=" ")
print()

 Select  Visualize  Run

Infinite "while" loop with break:

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 7 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

def readUntilDone():
linesEntered = 0
while (True):
response = input("Enter a string (or 'done' to quit): ")
if (response == "done"):
break
print(" You entered: ", response)
linesEntered += 1
print("Bye!")
return linesEntered

linesEntered = readUntilDone()
print("You entered", linesEntered, "lines (not counting 'done').")

 Select  Visualize  Run

5. isPrime  video
# Note: there are faster/better ways. We're just going for clarity and simplicit
y here.
def isPrime(n):
if (n < 2):
return False
for factor in range(2,n):
if (n % factor == 0):
return False
return True

# And take it for a spin


for n in range(100):
if isPrime(n):
print(n, end=" ")
print()

 Select  Visualize  Run

fasterIsPrime:

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 8 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

#Note: this is still not the fastest way, but it's a nice improvement.
def fasterIsPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True

# And try out this version:


for n in range(100):
if fasterIsPrime(n):
print(n, end=" ")
print()

 Select  Visualize  Run

Verify fasterIsPrime is faster:

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 9 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

def isPrime(n):
if (n < 2):
return False
for factor in range(2,n):
if (n % factor == 0):
return False
return True

def fasterIsPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True

# Verify these are the same


for n in range(100):
assert(isPrime(n) == fasterIsPrime(n))
print("They seem to work the same!")

# Now let's see if we really sped things up


import time
bigPrime = 499 # Try 1010809, or 10101023, or 102030407
print("Timing isPrime(",bigPrime,")", end=" ")
time0 = time.time()
print(", returns ", isPrime(bigPrime), end=" ")
time1 = time.time()
print(", time = ",(time1-time0)*1000,"ms")

print("Timing fasterIsPrime(",bigPrime,")", end=" ")


time0 = time.time()
print(", returns ", fasterIsPrime(bigPrime), end=" ")
time1 = time.time()
print(", time = ",(time1-time0)*1000,"ms")

 Select  Visualize  Run

6. nthPrime  video

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 10 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM

def isPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True

# Adapt the "nth" pattern we used above in nthMultipleOf4or7()

def nthPrime(n):
found = 0
guess = 0
while (found <= n):
guess += 1
if (isPrime(guess)):
found += 1
return guess

# and let's see a list of the primes


for n in range(10):
print(n, nthPrime(n))
print("Done!")

 Select  Visualize  Run

https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 11 of 11

Vous aimerez peut-être aussi