230 likes | 246 Views
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.
E N D
CSE 115 Introduction to Computer Science I
Today's Plan Review • Selection Statement: if-else • Selection Statement: if-elif-… Functions andcontrol flow
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.
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.
Math to Python • Now want to write this as a Python function # Returns [reason this matters]def f(x):
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
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
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
Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y):
Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y): if x <= y
Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y): if x <= y : return x else :
Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y): if x <= y : return x else : return y
Smaller (of 2) • One possible solution (in Python): # Return smaller of two inputsdef smaller(x, y): if x <= y : return x else : return y
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
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
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
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
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
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
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
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
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
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