1 / 25

Loops:

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

papina
Télécharger la présentation

Loops:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loops: def f(x): counter = 0 y = 0; while counter <=x: y += x counter += 1 return(y) assertEqual(f(4),______) • What does this do?

  2. 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.

  3. While Loop def ThreeYearOld(): response = "" while response != "Because.": response = input("Why?") print("Oh. Okay.") ThreeYearOld()

  4. While loops def f(): total = 0 while count >= 1: total += count count = count -1 return(total) What does this calculate?

  5. While loops def f(): total = 0 count = 4; while count >= 1: total += count return(total) What does this calculate?

  6. 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

  7. 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),______)

  8. 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),______)

  9. 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),______)

  10. 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),________________)

  11. def q(x,y): ct = 0 while (ct < x): ct2 = 0 while (ct2 < y): print ct + ct2 ct2 += 1 ct += 1 q(4,3)

  12. def q(x,y): ct = 0 while (ct < x): ct2 = ct while (ct2 < y): print ct + ct2 ct2 += 1 ct += 1 q(4,3)

  13. def q(x): ct = 0 while (ct < x): ct2 = x while (ct2 > ct): print ct + ct2 ct2 -= 1 ct += 1 q(4)

  14. 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)

  15. 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?

  16. 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?

  17. 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?

  18. 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?

  19. 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))

  20. 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?

  21. 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))

  22. 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))

  23. 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))

  24. 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)

  25. 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))

More Related