Midterm 2 15-112 Fundamentals of Programming 11/14/2013

advertisement
11/14/2013
Midterm 2
15-112 Fundamentals of
Programming
November 12th , 2013
Project
Due Tuesday December 3rd in Class
 A 3-4 minute video demo with explanation of
features of your project
 Source code to be submitted on autolab
before video demo
 Project description – a minimum one page
document describing what your project does
and some of the algorithms and libraries used
by your code
Next Tuesday in class
Topics







String processsing
Recursion
Networking and Sockets
Dictionaries
Event Driven Programming with Tkinter
Searching and Sorting
Complexity Analysis
Project Milestones
 Due Sunday November 14th in class
 Final project description (2 points)
 Description of features that will be demoed at the second milestone (see
next item)
 Due Sunday November 24th
 Demo a working set of feature for your project to a TA. Setup an
appointment for no more than 10 minutes (10 points)
 Submit your current code for the demo on autolab.
 Due Sunday December 1st
 Demo a working project to a TA. (5 points)
 Your final project should be a cleaned up version of this demo.
 Submit code on autolab.
 Due Tuesday December 3rd
 Final presentation and code submission (85 points)
 See project document under assignments to see point distrubution
1
11/14/2013
What are we doing today?
Functional Programming
Imperative Programming
The variables hold values that determine a state
within a program
Sequence of commands manipulate data in the
program
Values are modified incrementally to reach a
solution to the computational problem
Functional Programming
Functional Programming
Functional programming is more
declarative in nature, where more
importance is given to what the program
should do
Data is transformed at a large scale rather
than being modified incrementally
Three main classes of data transformation
 Mapping: One-to-One transformation: each
element in the source is converted to a new
value
 Filtering: Filtering out values that don’t meet a
criteria, or retaining only those values that
meet a specific criteria
 Reduction: Applying a binary function to each
value of list in a cumulative fashion.
2
11/14/2013
Mapping Example
Take a list of values and produce a list of
squares of each value
Imperative Approach
Create an empty list
Iterate through each value in the source list
 Find the square of the value
 Add it to the squares list
a = [1,2,3,4,5,6]
squares = []
for i in a:
squares += [i*i]
print squares
Functional Approach
Lambda functions
Define a transformation and apply to all
values
A lot of times, we need to define functions
that will be used as transformations.
We use lambda functions where we:
def square(x):
return x*x
 Define and call functions at the same time
squares = map(square,a)
print squares
squares = map(lambda x: x*x ,a)
print squares
3
11/14/2013
Filtering Example
Given a list, print all the odd numbers
Imperative Approach
a = [1,2,3,4,5,6]
for i in a:
if i %2 == 1:
print i
Functional Approach
a = [1,2,3,4,5,6]
print filter(lambda x: x%2==1,a)
Print the sum of all numbers
that are odd
a = [3,6,3,5,6,7,5,4,6,7,8]
print reduce(lambda x,y:x+y,
filter(lambda x: x%2==1,a))
Reduction Example
Find the sum of all values in a list
Imperative Approach
a = [1,2,3,4,5,6]
Sum = 0
for i in a:
Sum = Sum + i
print Sum
Functional Approach
print reduce(lambda x,y: x+y, a)
Finding common elements
Find a list of elements common in lists a
and b?
Imperative Approach?
Functional Approach?
4
11/14/2013
Exercises
Write a function that takes a list as input
and returns a list containing the cubes of
those numbers that are divisible by 3.
Sieve of Eratosthenes
 Find all the prime numbers less than or
equal to a given integer n
 Write an imperative form of this function
 Write a functional form of this function using
map and filter.
Using reduce, find the average of a list of
numbers.
5
Download