1 / 23

CSE 115

CSE 115. Introduction to Computer Science I. Today's Plan. Review Selection Statement: if-else Selection Statement: if- elif - … F unctions and c ontrol flow. Function in Math.

Download Presentation

CSE 115

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. CSE 115 Introduction to Computer Science I

  2. Today's Plan Review • Selection Statement: if-else • Selection Statement: if-elif-… Functions andcontrol flow

  3. Function in Math • Want function which takes in a single input. The function should return the sum of:the input squared;the input multiplied by 5; andthe number 2.

  4. Function in Math • Want function which takes in a single input. The function should return the sum of:the input squared;the input multiplied by 5; andthe number 2.

  5. Math to Python • Now want to write this as a Python function # Returns [reason this matters]def f(x):

  6. Math to Python • Now want to write this as a Python function # Returns [reason this matters]def f(x): return(x ** 2)+(x * 5) +2

  7. Smaller (of 2) • Want function which returns smaller param(For purposes of this exercise, cannot use built-in min() function) • Assumptions: • 1) values can be ordered with respect to each other • 2) if smallest value is not unique, return any one • Work with neighbors and be prepared to:explain your answer (in English) &use subgoals to write this function

  8. Smaller (of 2) • Want function which returns smaller param(For purposes of this exercise, cannot use built-in min() function) • Work with neighbors and be prepared to:explain your answer (in English) & use subgoals to write this function

  9. Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y):

  10. Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y): if x <= y

  11. Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y): if x <= y : return x else :

  12. Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y): if x <= y : return x else : return y

  13. Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y): if x <= y : return x else : return y

  14. Smaller (of 2) • One possible solution (in Python): • Mistakes happen; critical to verify code works # Return smaller of two inputsdef smaller(x, y): if x <= y : return x else : return y smaller(4, 5) has value 4 smaller(5, -1) has value -1 smaller(8, 8) has value 8

  15. Smaller (of 2) • One possible solution (in Python): • Mistakes happen; critical to verify code works # Return smaller of two inputsdef smaller(x, y): if x <= y : return x else : return y smaller(4, 5) has value 4 smaller(5, -1) has value -1 smaller(8, 8) has value 8

  16. Smallest (of 3) • Function which returns smallest params(For learning purposes, min() still banned) • Assumptions: • 1) values can be ordered with respect to each other • 2) if smallest value is not unique, return any one • Work with neighbors and be prepared to:explain your answer (in English) & use subgoals to write this function

  17. Smallest (of 3) • Function which returns smallest params(For learning purposes, min() still banned) • Work with neighbors and be prepared to:explain your answer (in English) & use subgoals to write this function

  18. Smallest (of 3) • One possible solution (in Python): # Return small*est* of three inputs def smallest(x, y, z): if x <= y <= z or x <= z <= y : return x elif y <= x <= z or y <= z <= x : return y else : return z

  19. Smallest (of 3) • One possible solution (in Python): # Return small*est* of three inputs def smallest(x, y, z): if x <= y <= z or x <= z <= y : return x elif y <= x <= z or y <= z <= x : return y else : return z

  20. Smallest (of 3) • One possible solution (in Python): • (I have made too many mistakes to live dangerously) # Return small*est* of three inputs def smallest(x, y, z): if x <= y <= z or x <= z <= y : return x elif y <= x <= z or y <= z <= x : return y else : return z

  21. Smallest (of 3) • One possible solution (in Python): • This code checks out! # Return small*est* of three inputs def smallest(x, y, z): if x <= y <= z or x <= z <= y : return x elif y <= x <= z or y <= z <= x : return y else : return z smallest(4, 395, 7) has value 4 smallest(1, 0, 74) has value 0 smallest(-1, -2, -3) has value -3

  22. Smallest (of 3) • There are lots of possible solutions • This code also checks out! # Return small*est* of three inputs def smallest(x, y, z): if x <= y and x <= z : return x elif y <= x and y <= z : return y else : return z smallest(4, 395, 7) has value 4 smallest(1, 0, 74) has value 0 smallest(-1, -2, -3) has value -3

  23. Smallest (of 3) • “Reduce, reuse, recycle” solution # Return small*est* of three inputs def smallest(x, y, z): temp = smaller(x, y) retVal = smaller(temp, z) return retVal

More Related