View
238
Download
1
Embed Size (px)
Citation preview
def f(x):
counter = 0
y = 0;
while counter <=x:
y += x
counter += 1
return(y)
assertEqual(f(4),______)
What does this do?
LOOPS:
WHILE LOOPS
def f():count = input("Enter a number") total = 0while count >= 1: total += count count = count -1return(total)
input: asks the user for some form of input – the user should type something in. Whatever the user types in is returned from input. In this case, it is put in the variable count.
def ThreeYearOld():
response = ""
while response != "Because.":
response = input("Why?")
print("Oh. Okay.")
ThreeYearOld()
WHILE LOOP
WHILE LOOPS
def f():total = 0while count >= 1: total += count count = count -1return(total)
What does this calculate?
WHILE LOOPS
def f():total = 0count = 4;while count >= 1: total += countreturn(total)
What does this calculate?
RULES:
If using an iterator, it must be initialized before we start the loop (outside the loop!)
count = 4;while count >= 1: ...
The loop has to stop. Inside the loop something must change each time so that we hit the stopping condition Like recursion, we must progress toward the
stopping conditionwhile count >= 1: total += count
count = count -1
def f(x):
return(x>5)
def y(ls):
ct = 0
while ((ct < len(ls)) and not(f(ls[ct]))):
ct += 1
return(ct)
list1 = [3,-1,2,4,8,7,5]
assertEqual(y(list1),______)
def z(ls):
ct = 0
x = 0
while (ct < len(ls)):
if ls[ct] > ls[x]:
x = ct
ct += 1
return(x)
list1 = [3,-1,2,4,8,7,5]
assertEqual(z(list1),______)
def z(ct):
x = 0
while (ct >= 0):
x += ct
ct = ct - 1
return(x)
assertEqual(z(5),______)
Versus:
def z(ct):
x = 0
while (ct >= 0):
ct = ct - 1
x += ct
return(x)
assertEqual(z(5),______)
def q(ls):
ct = 0
while (ct < len(ls)/2):
ls[len(ls)-(ct+1)],ls[ct] = ls[ct],ls[len(ls)-(ct + 1)]
ct += 1
return(ls)
list1 = [3,-1,2,4,8,7,5]
assertEqual(q(list1),________________)
FOR LOOPS
A for loop gets each successive element in a sequence Examples:for i in range(0,10): print(i)(equivalent to:i = 0while i < 10:
print(i)i = i+1
for i in range(0, 50, 5): print(i)
for i in range(10, 0, -1): print(i)
LEN, IN
def f(lv): x = len(lv) print(x) for y in range(0,x): print(lv[y])
listvar = ["ham","am","boat","goat","there","anywhere"]
f(listvar)
What does this do?
FOR LOOPS
def f(word):
for x in word:
print(x)
wvar = "birthday"
f(wvar)
svar=["I","do","not","like","green","eggs", "and","ham"]
f(svar)
What do you think this does?
def f(lv): x = len(lv) print(x) for y in range(0,x): if "t" in lv[y]: print(lv[y])
listvar = ["ham","am","boat","goat","there","anywhere"]
f(listvar)
What does this do?
def f(lv): x = len(lv) print(x) for y in range(0,x): for z in range(0,len(lv[y])): print(lv[y][z]) print
listvar = ["ham","am","boat","goat","there","anywhere"]f(listvar)
What does this do?
WHAT DOES THIS DO?
def f(word): newstr = "" for i in range(1,len(word)+1): newstr += word[-i] return(newstr)
wvar = "sesquipedalian"print(f(wvar))
import random
def f(word): high = len(word) low = -len(word) newstr = "" for i in range(10): position = random.randrange(low, high) newstr += word[position] return(newstr)
wvar = "sesquipedalian"print(f(wvar))
Tougher: What does this do?
WHAT DOES THIS DO?def f(message): new_message = "" SPECIALLETTERS = "dlmstp"
for letter in message: if letter.lower() not in SPECIALLETTERS: new_message += letter return(new_message)
mvar = "Hi, my name is Lassie"print("Your message is: now" + f(mvar))
SOMETHING YOU CAN’T DO: word = “game”; word[0] = “l”;Instead:def f(message): newmessage = ""
print("old string: " + message) for x in message: if x == "g": newmessage += "l" else: newmessage += x return(newmessage)
mvar = "game"print("new string: " + f(mvar))mvar = "pogysyggabicaggy"print("new string: " + f(mvar))
def SS(ls):
for i in range(len(ls)):
s=ls[i]
si = i
for j in range(i, len(ls)):
if (ls[j]<s):
s=ls[j]
si=j
ls[si]=ls[i]
ls[i]=s
return ls
a=[3,5,2,7,1]
print (a)
print ("=>", SS(a))
def is(ls): for i in range(0,len(ls)): x = ls[i] j = i-1 while (j >= 0) and (ls[j] > x): ls[j+1] = ls[j] j = j-1 ls[j+1] = x return(ls)ls=[3,1,2,6,4,7]is(ls)print(ls)