Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings: Strings? concatenate (join) + make multiple copies using *

Similar presentations


Presentation on theme: "Strings: Strings? concatenate (join) + make multiple copies using *"— Presentation transcript:

1 Strings: Strings? concatenate (join) + make multiple copies using *
compare: > < == >= <= != What else can we do?

2 A lot! Len, in def f(x): if "e" in x: return("e is in your message.")
else: return("e is not in your message.") strvar = “puppies are cute” print(f(strvar)) z = len("cat")

3 Strings Positional Get a character at a particular index within a string stringvar1 = “ binary” Index stringvar2 = “ computer” Index So to use a particular item in the list: x=stringvar2[3] #”p” y=stringvar1[1] #”i” z=stringvar2[0] #”c” w=stringvar1[6] #??? Example: def f(par): randnum = randrange(0,6) return(“you get “ + par[randnum]) print(f((stringvar1))

4 String: stringvar1 = “binary” Index 012345 stringvar2 = “ computer”
get the length of a string len(stringvar1) # will give you 6, not 5! len(stringvar2) #will give you 8, not 7 Example: def f(par): y = len(par) randnum = randrange(0,y) #Why does this work? return(“you get “ + par[randnum]) print(f(stringvar2))

5 We can now do: def f(x,y): f(0,’kluge’) if (x == len(y)): else: return
print(y[x]) return(f(x+1,y)) f(0,’kluge’)

6 Slicing (Different from Indexing)
Copying parts of strings: | p | i | z | z | a | def f(word): return(word[2:5] ) print(f(“pizza”)) def g(): word = “pizza” return(word[1:3]) def h(): return(word[-4:-2]) def i(): return(word[-4:3])

7 Shortcuts | p | i | z | z | a | word=“pizza” word[0:4] pizz word[:4] word[2:5] zza word[2:]

8 # display a slice def g(s,f,wd): return("wd["+str(s)+":"+str(f)+"] is "+wd[s:f]) print(g(3,7,"sesquipedalian"))

9 Strings are Immutable word of the day
Can: x = "catamaran" print(x.count("a")) print(x.index('a')) Can’t (if it changes the string, we can’t do it) x[3] = “y” #can’t do!

10 What does this do? def f(str1): str2 = “WuGmUmP” if str1.upper() == str2.upper(): return(“You’re one of us!”) else: return(“You’re not one of us!”) newstr = “wuGMuMp” print(f(newstr))

11 def rec(a,x,y): if (x == len(a)): return 0 elif (a[x] in y): return 1 + rec(a,x+1,y) else: return rec(a, x+1, y) a = "catamaran" y = "aeiou" print(str(rec(a,0,y)))

12 def rec(a,x,y): if (x == len(a)): return "" elif (a[x] in y): return rec(a,x+1,y) else: return (a[x] + rec(a, x+1, y)) a = "catamaran" y = "aeiou" print(str(rec(a,0,y)))

13 def f(x,y,z): if y == len(x): return(z) else: if (x[y] == "p"): return(f(x,y+1,z+"m")) return(f(x,y+1,z+x[y])) print(f("puppy",0,""))

14 def f(x,a): if (x == len(a)//2): return a[x]==a[len(a)//2] else: if a[x] != a[len(a)-(x+1)]: return a[x] == a[len(a)-(x+1)] return(f(x+1,a)) print(f(0,"mom")) print(f(0,"mommy"))

15 def g(x,y,z,q): if (x == len(y)): return z else: if (y[x] not in " ,
def g(x,y,z,q): if (x == len(y)): return z else: if (y[x] not in " ,!.:;?"): if q: return(g(x+1,y,z+1,False)) return(g(x+1,y,z,False)) return(g(x+1,y,z,True)) print(g(0,"Hello there, how are you today?",0,True))

16 def h(x,y,z,q): if x == len(y): return 0 else: if y[x] in "\"'": if (q == y[x]): return(z) elif q != y[x]: return h(x+1,y,0,y[x]) return h(x+1,y,z+1,q) print(h(0,"He said, 'Houston, we have a problem' in the movie",0,'+'))

17

18 While Loop Look at this code: def ThreeYearOld(): x = ""
while (x != "Because."): x = input("But why?") return("Oh. Okay.") print(ThreeYearOld()) Look at this code: What is our starting condition? What must be true in order for the loop to start? What will make the loop end? What must become false? Does something INSIDE the loop that changes so that eventually the loop will end? Does this all remind you of something?

19 Loops: While Loops: What does this code print out?
def f(x, counter): while counter < x: print(counter) counter = counter + 1 return(counter) print(f(5, 0)) def f2(x,counter): if (counter >= x): return (counter) else: print(counter) return(f2(x,counter+1)) print(f2(5,0)) While Loops: Continue while condition is true Must initialize to make condition true In recursion we stop when the stopping contion is true – in while loops we stop when the while condition is false. Something inside the loop must change so that the condition becomes false eventually. What does this code print out?

20 While loops Starting condition (What must be true to enter loop)?
def f(total,count): while count >= 1: #This is the part that loops total += count count = count -1 return(total) print(f(0, int(input("Enter a number")))) Starting condition (What must be true to enter loop)? What makes the loop stop? What inside the loop changes so that the loop will stop?

21 While loops Starting condition? What makes the loop stop?
def f(total): while count >= 1: total += count count = count -1 return(total) print(f(0)) Starting condition? What makes the loop stop? What inside the loop changes so that the loop will stop?

22 While loops def f(total, count): while count >= 1: total += count count = count + 1 return(total) print(f(0, 4)) Starting condition? What makes the loop stop? What inside the loop changes so that the loop will stop?

23 Loops: Anything that can be done recursively can be done with a while loop (and vice versa) Certain problems work better with certain loops loop must have a True/False condition while count >= 1: While loops starts when the condition is True It continues while the condition is True It stops when the condition is False Something must change inside the loop that will eventually make the True/False condition False total += count count = count -1

24 Differences (syntactic):
We can initialize variables used by the while loop inside the function (BUT BEFORE THE WHILE LOOP!) Why can’t we do this with recursion? def f(): x = 5 counter = 0 while counter < x: print(counter) counter = counter + 1 return(counter) print(f()) def f(x, counter): while counter < x: print(counter) counter = counter + 1 return(counter) print(f(5, 0)) In general, unless you know what you are doing, while loops should not call the function from within the function In general, unless you know what you are doing, recursive function should not use while loops inside the function.

25 Write a function that takes as an input parameter an integer and uses a while loop to print out "ha" that number of times. Starting condition? What makes the loop stop? (your True/False condition?) What inside the loop changes so that the loop will stop? def func(x): while (x > 0): print("ha ",end= " ") x -= 1 return func(5) def func(x): stringvar = "" while (x > 0): stringvar += "ha ") x -= 1 return(stringvar) print(func(5)) Recursive: def func(x): if x <= 0: return else: print("ha ") return(func(x-1)) func(5) Recursive: def func(x,stringvar): if x <= 0: return(stringvar) else: return(func(x-1,stringvar+"ha ")) print(func(5, ""))

26 What does this do? def w2(x,y): tot = 0 while (y > 0): tot += x y -= 1 return(tot) print(w2(3,5))

27 What about this? def g(y,z): x = len(y)-1 while x >= 0: z += y[x] x-=1 return z print(g("nilbog",""))

28 What does this do? def f(message): newmessage = "" x = 0 while x < len(message): if message[x] == "g": newmessage += "l" else: newmessage += message[x] x += 1 return(newmessage) print(f("pogysyggabicaggy“))

29 This one? def c2(x): y = 0 k = 0 while (x > 0): y += x%2 * 10**k k += 1 x = x//2 return(y) print(c2(10))

30 What does this one do? def w4(x, z): y = randrange(0,x) print(y) g = -1 #Why did I do this? while (g != y): g = randrange(0,x) print(g) z+= 1 print(z) return("It took " + str(z)) print(w4(10,0))

31 Try: exponent: Write a function that takes as input parameters 2 integers and uses a while loop to calculate x to the power of y (don’t use **) def c(x,y): z = 1; while (y > 0): z *= x y -= 1 return z

32 Write a while loop that converts a binary number to an integer
def c3(x): y = 0 k = 0 while (x > 0): y += x%2 * 2 **k k+=1 x = x//10 return(y)

33 Versus: def z(ct): x = 0 while (ct >= 0): ct = ct - 1 x += ct return(x) print(z(5)) def z(ct): x = 0 while (ct >= 0): x += ct ct = ct - 1 return(x) print(z(5))

34 def f5(x,y): k = 0 v = "" while k < len(y): if y[k] not in x: v+=y[k] k+=1 print(v) z = True q = len(v) while k < q: if v[k] != v[q-k-1]: z = False return(z) print(f5("?.,!","udel.edu"))

35 Largest of x random numbers
Write a function that takes 2 integers (x and y) as input parameters , and generates x random numbers between 0 and y. It finds the largest of those random numbers. def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50))

36 What do we get? def q(x,y): ct = 0 while (ct < x): ct2 = 0 while (ct2 < y): print(ct + ct2) ct2 += 1 ct += 1 return q(4,3) Rules: The contents of the while loop is only what is indented under the while loop We do the contents of the while loop over and over until the while loop’s condition becomes false. Only then do we do what is below the while loop.

37 What does this print out?
def h(x): ct = 1 while (ct <= x): ct2 = ct while (ct2<=x): print(ct2, end=“\t") #prints a tab after every ct2 ct2 += 1 ct += 1 print("\n") #this is a line break (starts a new line) return h(5)

38 Can you reverse it def h(x): ct = 1 while (ct <= x): ct2 = 1
Output: 1 1 2 1 2 3 Output: 3 4 5 4 5 5 def h(x): ct = 1 while (ct <= x): ct2 = ct while (ct2<=x): print(ct2, end=" ") ct2 += 1 ct += 1 print("\n") return h(5) def h(x): ct = 1 while (ct <= x): ct2 = 1 while (ct2<=ct): print(ct2, end=" ") ct2 += 1 ct += 1 print("\n") h(5)

39 What do we get? def g(a,b): while (a != b): while (a > b): a = a-b while(b>a): b = b-a return(a) print(g(6,20))

40 def f14(x): k = 0 n = -1 v = x while k <= (x
def f14(x): k = 0 n = -1 v = x while k <= (x*2): m = 0 while (m < v): print(v, end = "") m += 1 if (v == 0): n = 1 else: print() v += n k+=1 f14(3)


Download ppt "Strings: Strings? concatenate (join) + make multiple copies using *"

Similar presentations


Ads by Google