250 likes | 473 Views
Loops:. def f(x): counter = 0 y = 0; while counter <=x: y += x counter += 1 return(y) assertEqual (f(4),______) What does this do?. While loops. def f(): count = input("Enter a number") total = 0 while count >= 1: total += count count = count -1
E N D
Loops: def f(x): counter = 0 y = 0; while counter <=x: y += x counter += 1 return(y) assertEqual(f(4),______) • What does this do?
While loops def f(): count = input("Enter a number") total = 0 while count >= 1: total += count count = count -1 return(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.
While Loop def ThreeYearOld(): response = "" while response != "Because.": response = input("Why?") print("Oh. Okay.") ThreeYearOld()
While loops def f(): total = 0 while count >= 1: total += count count = count -1 return(total) What does this calculate?
While loops def f(): total = 0 count = 4; while count >= 1: total += count return(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 condition while 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),________________)
def q(x,y): ct = 0 while (ct < x): ct2 = 0 while (ct2 < y): print ct + ct2 ct2 += 1 ct += 1 q(4,3)
def q(x,y): ct = 0 while (ct < x): ct2 = ct while (ct2 < y): print ct + ct2 ct2 += 1 ct += 1 q(4,3)
def q(x): ct = 0 while (ct < x): ct2 = x while (ct2 > ct): print ct + ct2 ct2 -= 1 ct += 1 q(4)
For loops • A for loop gets each successive element in a sequence • Examples: for i in range(0,10): print(i) (equivalent to: i = 0 while 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)
def bs(list2): i = 0; swap_test = False while ((i < len(list2)-1) and (swap_test == False)): swap_test = True for j in range(0, len(list2) - i - 1): if list2[j] > list2[j + 1]: list2[j], list2[j + 1] = list2[j + 1], list2[j] swap_test = False i += 1 return(list2) ls = [3,2,4,7,1] print(bs(ls))