Вы находитесь на странице: 1из 31

Algorithms and Methods

CS 111: Computer Science for Scientists


Making a sandwich
What is the algorithm for making a Peanut Butter and Jelly
Sandwich?
MakePBJ
get bread out of pantry
take bread out of bag

get peanut butter out of pantry


spread peanut butter on bread
put peanut butter back in pantry

get jelly out of pantry


spread jelly on bread
put jelly back in pantry

put bread back in pantry


give person their sandwich
Peanut Butter and Marshmallow fluff?
Suppose you want to make a Fluffernutter (PB and
Marshmallow), how do you do that?
MakeFluff
get bread out of pantry
take bread out of bag

get peanut butter out of pantry


spread peanut butter on bread
put peanut butter back in pantry

get Marshmallow Fluff out of pantry


spread Marshmallow Fluff on bread
put Marshmallow Fluff back in pantry

put bread back in pantry


give person their sandwich
Anything Similar?
MakePBJ MakeFluff
get bread out of pantry get bread out of pantry
take bread out of bag take bread out of bag

get peanut butter out of pantry get peanut butter out of pantry
spread peanut butter on bread spread peanut butter on bread
put peanut butter back in pantry put peanut butter back in pantry

get jelly out of pantry get Marshmallow Fluff out of pantry


spread jelly on bread spread Marshmallow Fluff on bread
put jelly back in pantry put Marshmallow Fluff back in pantry

put bread back in pantry put bread back in pantry


give person their sandwich give person their sandwich
Anything Similar?
MakePBJ MakeFluff
get bread out of pantry get bread out of pantry
take bread out of bag take bread out of bag

get peanut butter out of pantry get peanut butter out of pantry
spread peanut butter on bread spread peanut butter on bread
put peanut butter back in pantry put peanut butter back in pantry

get jelly out of pantry get Marshmallow Fluff out of pantry


spread jelly on bread spread Marshmallow Fluff on bread
put jelly back in pantry put Marshmallow Fluff back in pantry

put bread back in pantry put bread back in pantry


give person their sandwich give person their sandwich
Anything Similar?
MakePBJ
get bread out of pantry
take bread out of bag

get peanut butter out of pantry


spread peanut butter on bread
put peanut butter back in pantry

get jelly out of pantry


spread jelly on bread
put jelly back in pantry

put bread back in pantry


give person their sandwich
Make ANY Peanut Butter Sandwich
MakePeanutButterSandwichWith(condiment):
get bread out of pantry
take bread out of bag

get peanut butter out of pantry


spread peanut butter on bread
put peanut butter back in pantry

get condiment out of pantry


spread condiment on bread
put condiment back in pantry

put bread back in pantry


give person their sandwich
Algorithm Practice: Milkshakes
Chocolate milkshake
Strawberry milkshake
ANY milkshake
MakePeanutButterSandwichWith(condiment):
get bread out of pantry

Calling It take bread out of bag

get peanut butter out of pantry


spread peanut butter on bread
put peanut butter back in pantry

get condiment out of pantry


spread condiment on bread
put condiment back in pantry

put bread back in pantry


give person who asked, their sandwich

The Main
MakePeanutButterSandwichWith(Jelly)
MakePeanutButterSandwichWith(Marshmallow Fluff)
MakePeanutButterSandwichWith(Anchovies)
Methods
In Python (and all programming languages) a method is a
focused algorithm defined to solve a specific problem
For example:
Make PB Sandwich
Calculate sine, cosine, and tangent.
Draw a square
Methods can be passed parameters so they are more general
For example:
Condiment in makePeanutButterSandwichWith
Methods can have any number of parameters (zero to many)
Formally
Formal Method
Methods in Python consist primarily of two components:
Signature
Body
The Signature consist of
The method name
The parameter list
The Body consists of
The algorithm code
An optional return statement.
Well talk about this later
Example

Method signature def addTwoValues(value1, value2):


summation = value1 + value2
Body return summation
Example

Parameter
Name List

def addTwoValues(value1, value2):


summation = value1 + value2
return summation

Note: The method signature begins with def and ends with :
Example

def addTwoValues(value1, value2):


Body
summation = value1 + value2
Return statement return summation

Note: The body of the method is


indented.
Calling the Method
You can call the method
anywhere you like in your code
once its been defined.
def addTwoValues(value1, value2):
You call a method by using the summation = value1 + value2
methods name and filling in return summation
values for each of the
parameters. addTwoValues(2,3) #Calls the method
addTwoValues(10,20) #Calls the method
Ok great?
So, weve called the method
twice, great OK...
It doesnt seem like weve done
def addTwoValues(value1, value2):
anything... summation = value1 + value2
return summation
...
... addTwoValues(2,3) #Calls the method
Thats because we havent addTwoValues(10,20) #Calls the method
really.
Lets talk about return.
Return
The return statement is how we
extract data out of the method
We want that sum of two values!
def addTwoValues(value1, value2):
To extract a value we need both summation = value1 + value2
the return statement and an return summation
assignment statement
The return tells us what data will x = addTwoValues(2,3)
be extracted y = addTwoValues(10,20)
The assignment tells us where to
store it.
Why Return
Once we return a value, we can
use it wherever the method was
called.
def addTwoValues(value1, value2):
summation = value1 + value2
return summation

x = addTwoValues(2,3)
y = addTwoValues(10,20)
Another Example

def largerValueTimes10(a, b):


largeValue = a
if b > a:
largeValue = b
largeValue *= 10
return largeValue

x = largerValueTimes10(2,3)
print(x)
Another Example

def threeValuesLargerValueTimes10(a, b, c):


largeValue = a
if b > a:
if c > b:
largeValue = c
else :
largeValue = b
else:
if c > a:
largeValue = c
largeValue *= 10
return largeValue

x = threeValuesLargerValueTimes10(2,3,15)
print(x)
No Inputs

def returnTheValueFour():
return 4

x = returnTheValueFour()

Note: When you define a method with no parameters, you still need the ()
Scope
Variables created in a method die at the end of method
A horrible painful variable death
This is called scope
This also includes parameters
def fooBar(var1, hello, bye):
thing = 2
goo = 55
trick = 100
bah = var1
boo = bye
gah = hello
return thing + goo +trick + bah + boo - gah

thing, goo, trick, bah, boo, gah, var1, x = 55


hello, and bye, are not accessible here. y = 1000
fooBar(2,x,y)
Names
Along with scope, the names of variables inside and outside
the method dont matter. Its the values that matter

def addThreeWeird(a,b,c):
return a + b*2 + c*3

a = 1
b = 100
c = 1000
x = addThreeWeird(c,a,b)

So, the value returned is 1000+100*2 + 1*3 = 1,203.


The names in the main dont matter, the order does.
Where to create methods?
Methods (for this class) should live in one of two places
At the beginning of your main file.
Dont do ANYTHING else in your main until you define all your methods
In their own separate file
In the main youd need to type import fileName
You call each method by using fileName.method()
Function or Method
Technically, a function is a method that returns a value.
However, most people use the terms interchangeably.
I dont care
Functional Decomposition
Functional Decomposition is the technique of solving a
problem by breaking it down into smaller problems.
Then you write a method for each of these smaller problems.
Since methods can call methods, you can further decompose
the problem.
At some point you have to write the solution, its a judgment
call of when you stop.
Factorial
The factorial of a non-negative integer n (written as n!) is the
product of all positive integers less than or equal to n. For
example, 5! = 5*4*3*2*1. Write a method with one parameter,
an integer n, that returns n!.
Recursion
A recursive function is a function that calls itself.

def hello():
print(Hello world)
hello()

What does this function do?


Recursion
Recursive functions have two parts:
1. Base case: Tells the computer when to stop
2. Recursive step: Simplifies the problem and moves closer to the
base case.

Lets rewrite the factorial function as a recursive function.


Practice
1. Write a method with one parameter, an integer, that returns the cube of that integer.
2. Write a method that takes in an integer n as input adds up the cubes of the numbers
from 1 to n. Your method should call your method from #1.
3. Use function decomposition to write a method with two integer parameters that
returns the square of the smaller number.
4. Write two functions that compute 1 + 2 + + n. One should be recursive and one
should not.
5. Write a method that takes three integers as parameters and returns the largest.
6. A year is a leap year if it is divisible by 4 unless it is a century that is not divisible by
400. So, 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300, and
2500 are not. Write a method that takes a year as a parameter and returns True if
the year is a leap year and False otherwise.
7. A fruit company sells oranges for 32 cents a pound plus $7.50 per order for
shipping. If an order is over 100 pounds, shipping cost is reduced by $1.50. Write a
method that will take the number of pounds of oranges as a parameter and return
the cost of the order.

Вам также может понравиться