1 / 21

Example

Example. from cisc106 import * def f(x): if x < 10: return (x+9) elif x < 5: return (x + 4) elif x < 0: return (x) else: return(0) assertEqual (f(-1), ______). Aside (strings). def addstrings (par1): return(par1 + " ubba ")

cala
Download Presentation

Example

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. Example from cisc106 import * def f(x): if x < 10: return (x+9) elif x < 5: return (x + 4) elif x < 0: return (x) else: return(0) assertEqual(f(-1), ______)

  2. Aside (strings) def addstrings(par1): return(par1 + "ubba") assertEqual(addstrings("gub"),"gububba") def addmore(par1): return(addstrings(par1) +addstrings(par1)) assertEqual(addmore(“ha"), "_____________")

  3. def makestr(x): • if (dow(x) !="Error"): • return("Today is " + dow(x)) • else: • return("Bad day") • assertEqual(makestr(4),________) • assertEqual(makestr(9),________) • def makestr(x): • if (dow(x)=="Error"): • return("Bad day") • else: • return("Today is " + dow(x)) • assertEqual(makestr(9),________) • assertEqual(makestr(-1),________) def dow(x): if (x == 1): return("Sunday") elif (x == 2): return("Monday") elif (x == 3): return("Tuesday") elif (x == 4): return("Wednesday") elif(x == 5): return("Thursday") elif (x == 6): return("Friday") elif (x == 7): return ("Saturday") else: return("Error") assertEqual(dow(3),"Tuesday")

  4. and def q(x): if (x>5) and (x < 10): return("just enough") elif (x > 5) and (x < 15): return("too much") else: return("no idea") assertEqual(q(12), "___________")

  5. diff? def q(x): if (x>5) or (x < 10): return("just enough") elif (x > 5) or (x < 15): return("too much") else: return("no idea") assertEqual(q(13), "________") def q(x): if (x>5) and (x < 10): return("just enough") elif (x > 5) and (x < 15): return("too much") else: return("no idea") assertEqual(q(7), "________")

  6. Eval? def q(x,y): if ((x<10) and (x >5)) or ((y <30 ) and (y > 25)): return(“hi") else: return(“low") What do we need to input to get “hi”? What do we need to input to get “low”?

  7. Nested assertEqual(f(4),____) • assertEqual(f(13),____) • assertEqual(f(0),_____) def f(x) if (x > 10): if ((x%2) == 0): return(‘a big, even number’) else: return(‘a big, odd number’) elif (x > 3): if ((x%2) == 0): return(‘a small, even number’) else: return(‘a small, odd number’) else: return(‘a puny number’)

  8. What happens? def ReturnSomething(value): if value = 1: return “glub” else: return “blug” assertEqual(ReturnSomething(1),________)

  9. Nested If: def nested(x,y): if (y < 0): if (x == "abs"): if (((y * -1) % 2)== 0): return(str(y) + "is even") else: return(str(y) + "is odd") else: if (y % 2 == 0): return(str(y) + "is negative but even") else: return(str(y) + "is negative but odd") else: return("we really don't care if it's even or odd") assertEqual(nested(“abs”,-4),_____________)

  10. Same? • def g(x): • if (x > 5): • if (x < 10): • return("just enough") • elif (x < 15): • return("too much") • else: • return("no idea") • assertEqual(g(12),________) • What about: • assertEqual(g(17),________) def q(x): if (x>5) and (x < 10): return("just enough") elif (x > 5) and (x < 15): return("too much") else: return("no idea")

  11. def multof3(x): return(x%3) def func(x): if(multof3(x) == 0): return(str(x) + " is a multiple of 3") else: return(str(x) + " is not a multiple of 3") def ismultof3(x): if ((x%3) == 0): return(True) else: return(False) def func2(x): if (ismultof3(x)): return(str(x) + " is a multiple of 3") else: return(str(x) + " is not a multiple of 3")

  12. def ismultof3(x): if ((x%3) == 0): return(True) else: return(False) When python executes the following statement, what is the result? (x%3)==0 def ismultof3(x): return((x%3) == 0): def func2(x): if (ismultof3(x)): return(str(x) + " is a multiple of 3") else: return(str(x) + " is not a multiple of 3")

  13. Function to represent this: #Determines if input value (x) will solve the #problem: # x2 -3x – 4 = 0 #Input: x: a number #Output:aboolean value def eqcheck(x): return x**2 –3*x – 4 == 0 Given assertEqual(eqcheck(3),______________) What is returned? assertEqual(eqcheck(4),______________)

  14. Remember? • from cisc106 import * • def StateHealth(w,pl,pp,s): • return((w * .25) + (pl * .25) + ((100-pp) * .20) + ((100 - s) * .30)) • def StateGrade(w,p1,pp,s): • if (StateHealth(w,p1,pp,s) > 93): • return ("A") • elif (StateHealth(w,p1,pp,s) > 84): • return('B') • elif (StateHealth(w,p1,pp,s) > 75): • return('C') • elif (StateHealth(w,p1,pp,s) > 66): • return('D') • else: • return('F') • assertEqual(StateGrade(80,73,18,19),'C') • assertEqual(StateGrade(57,96,55,16),'D') • assertEqual(StateGrade(100,100, 0, 0),'A')

  15. Instead • from cisc106 import * • def StateHealth(w,pl,pp,s): • return((w * .25) + (pl * .25) + ((100-pp) * .20) + ((100 - s) * .30)) • def StateGrade(w,p1,pp,s): • var1 = StateHealth(w,p1,pp,s) • if (var1 > 93): • return ("A") • elif (var1> 84): • return('B') • elif (var1> 75): • return('C') • elif (var1> 66): • return('D') • else: • return('F') • assertEqual(StateGrade(80,73,18,19),'C') • assertEqual(StateGrade(57,96,55,16),'D') • assertEqual(StateGrade(100,100, 0, 0),'A')

  16. Add a Curve to the Grade? • from cisc106 import * • def StateHealth(w,pl,pp,s): • return((w * .25) + (pl * .25) + ((100-pp) * .20) + ((100 - s) * .30)) • def StateGrade(w,p1,pp,s): • var1 = StateHealth(w,p1,pp,s) + 10 • if (var1 > 93): • return ("A") • elif (var1> 84): • return('B') • elif (var1> 75): • return('C') • elif (var1> 66): • return('D') • else: • return('F') • assertEqual(StateGrade(80,73,18,19),‘B’) • assertEqual(StateGrade(57,96,55,16),‘C') • assertEqual(StateGrade(100,100, 0, 0),'A')

  17. More Variables from cisc106 import * def calcsomething(par1): if ((par1%2) == 1): return(par1 * -1) else: return(par1) def finddif(par1,par2): var1 = calcsomething(par1) if (var1 < 0): var1 = 0 var2 = calcsomething(par2) if (var2 < 0): var2 = 0 if ((var1 - var2) < 0): return(var2 - var1) else: return(var1 - var2) assertEqual(finddif(3,7),____) assertEqual(finddif(4,7),____) assertEqual(finddif(2,4),____)

  18. def MakeDate(par1,par2,par3): var1 = "Today is " var1 += GetDOW(par1) var1 += ", " var1 += GetMonth(par2) var1 += " " var1 += "20"+str(par3) return(var1) def GetMonth(x): if (x == 1): return("January") elif (x == 2): return("February") elif (x == 3): return("March") elif (x == 4): return("April") elif(x == 5): return("May") elif (x == 6): return("June") elif (x == 7): return ("July") elif (x == 8): return("August") elif (x == 9): return("September") elif (x == 10): return("October") elif(x == 11): return("November") elif (x == 12): return("December") def GetDOW(x): if (x == 1): return("Sunday") elif (x == 2): return("Monday") elif (x == 3): return("Tuesday") elif (x == 4): return("Wednesday") elif(x == 5): return("Thursday") elif (x == 6): return("Friday") elif (x == 7): return ("Saturday")

  19. if statements: Same? def fu1(x): y = x*3 if (y < 0): y = y * -1 elif (y < 10): y = y + 4 return(y) assertEqual(fu1(-2),__) def fu2(x): y = x*3 if (y < 0): y = y * -1 if (y < 10): y = y + 4 return(y) assertEqual(fu2(-2),__)

  20. What about? def f(x): if (x == 1): return x else: return(x + f(x-1)) assertEqual(f(4),______) def f2(x): if (x == 1): return str(x) else: return(str(x) + f2(x-1)) assertEqual(f2(4),_______)

  21. Try: def f3(x): if (x == 1): return x else: return(x+ f2(x-2)) assertEqual(f2(4),_______) def f(x): return(x + f(x-1)) assertEqual(f(4),______) def f2(x): if (x == 1): return x else: return(x+ f2(x+1)) assertEqual(f2(4),_____)

More Related