1 / 17

def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)

def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3). What about?. def f(x): if (x == 1): return x else: return(x + f(x-1)) print(f(4)) def f2(x): if (x == 1): return str (x) else:

prem
Download Presentation

def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)

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. def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)

  2. What about? def f(x): if (x == 1): return x else: return(x + f(x-1)) print(f(4)) def f2(x): if (x == 1): return str(x) else: return(str(x) + f2(x-1)) print(f2(4))

  3. Try: def f3(x): if (x == 1): return x else: return(x+ f3(x-2)) print(f3(4)) def f(x): return(x + f(x-1)) print(f(4)) def f2(x): if (x == 1): return x else: return(x+ f2(x+1)) print(f2(4))

  4. What about? Stack in memory f(4) = 4 + ?f(3)? f(3) = 3 + ?f(2)? f(2) = 2 + ?f(1)? f(1) = 1 __________________________ f2(4) = "4" + f2(3) f2(3) = "3" + f2(2) f2(2) = "2" + f2(1) f2(1) = "1" def f(x): if (x == 1): return x else: return(x + f(x-1)) print(f(4)) def f2(x): if (x == 1): return str(x) else: return(str(x) + f2(x-1)) print(f2(4))

  5. Recursion: 27 9 16 8 4 Memory Stack f3(3,3) = 3*f3(3,2) f3(3,2) = 3*f3(3,1) f3(3,1) = 3 f3(2,4) 2*f3(2,3) f3(2,3) = 2*f3(2,2) f3(2,2) = 2*f3(2,1) f3(2,1) = 2 def f3(x,y): if (y == 1): return (x) else: return(x * f3(x,y-1)) print(f3(3,3)) print(f3(2,4))

  6. Recursion: Try def f7(a,b): if (b <= 0): return(a) elif((b%3)== 0): return(f7(a+1,b-1)) else: return(f7(a,b-1)) print(f7(0,13)) def f5 (a): if (a <= 0): return(1) elif ((a%2) ==0): return (f5(a - 1)) else: return (a*f5(a-1)) print(f5(6)) print(f5(5)) print(f5(-1)) def f6(x): if (x <= 1): return str(x) else: return(f6(x-1) + str(x) ) print(f6(5))

  7. How about these? def listrec(x,ls): if x == len(ls): return(0) else: return(ls[x]+ listrec(x+1,ls)) listarr = [3,2,7,3,1] print(listrec(0,listarr)) def lr2(x,y,ls): if x == len(ls): return(y) elif (ls[x] > y): y=ls[x] return(lr2(x+1,y,ls)) else: return(lr2(x+1,y,ls)) listarr = [3,2,7,3,1] print(lr2(0,0,listarr))

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

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

  10. def f2(lv,x,num,ct): if (x == len(lv)): return ct elif (lv[x] == num): return(f2(lv,x+1,num,ct+1)) else: return(f2(lv,x+1,num,ct)) listvar = [1,3,7,2,1,8,0,1,6,4,1,2,3,7] print (f2(listvar,0,1,0))

  11. from random import * def sl(list,new): if len(list) == 1: new.append(list.pop()) return(new) x = randrange(0,len(list)) new.append(list.pop(x)) return(sl(list,new)) list = ["m","o","t","h","e","r"] new = [] print(sl(list,new))

  12. (Cool, but challenging) def f4 (a, b): if (b == 0): return (a) if (a < b): return f4 (b, a) else: return f4 (b, a%b) print(f4(27,12)) print(f4(25,50)) print(f4(15,20))

  13. # display a slice def g(start,finish,lv): return("lv[" + str(start) + ":" + str(finish) + "] is" + str(lv[start:finish])) listvar = ["cat","dog","bug","echidna","aardvark"] print(g(2,4,listvar))

  14. Global Variables • Variables defined outside of all functions • These variables can then be used by all functions • example: s = 32 def f(): print (s ) f() def g() return(s/8) #did I change s? print(g()) def h(): ls = [0] * s return(ls) print(h())

  15. Scope: x = [] y = 6 def f(): for k in range(y): x.append(0); return f() def g(): print(x); return g() def h(): y = "hello"; z = "bye" return(y) print(h()); print(z) #??? def j(): print(y) j()

  16. a = "catamaran" y = "aeiou" def rec(x): if (x == len(a)): return "" elif (a[x] in y): return rec(x+1) else: return (a[x] + rec(x+1,)) a = (rec(0)) print (a) def addit(x): if x == len(a): return"" elif x %2 == 1: return("ipa"+ a[x] + addit(x+1)) else: return(a[x] + addit(x+1)) print(addit(0)) a = "catamaran" y = "aeiou" def rec(x): if (x == len(a)): return "" elif (a[x] in y): return rec(x+1) else: return (a[x] + rec(x+1,)) print(rec(0)) print (a) def addit(x): if x == len(a): return"" elif x %2 == 1: return("ipa"+ a[x] + addit(x+1)) else: return(a[x] + addit(x+1)) print(addit(0))

  17. from random import * mat = [] x = -1 def createmat(): x = randrange(5,10) y = randrange(5,10) for k in range(x): mat.append([]) for l in range(y): mat[k].append(0) return def modifydi(): for k in range(len(mat)): for l in range(len(mat[0])): if (k == l): mat[k][l]=x if (len(mat) - k) == l: mat[k][l]=x return def main(): createmat() modifydi() main()

More Related