Munch Lab

Applied programming 2014 week four

Lectures

At the single lecture I will talk about recursion – which is a powerful programming technique.

At the double lecture I will talk about how to read from and write to files. We will also talk about how to break up a large problem into a set of smaller and problems that are more easily solved.

Computer exercises

Complete the the exercise:  Base composition of HIV sequences. Also try this set of interactive code snippets to help you understand scope and references.

Reading material

The chapters: Files, and Recursion from  (our version of) How to think like a computer scientist.

Weekly assignment

For the assignment you must write four functions. Most of them can be implemented with list comprehensions if you have the guts.

Write a function square(L) that takes a list of numbers and returns the list of those
numbers squared.

Example usage:

print(square([2, 5, 3, 6]))
[4, 25, 9, 36]

Write a function even(L) that takes a list of numbers and returns a list of only those
numbers that are even. Remember that you can test if a number is even using n % 2 == 0.

Example usage:

print(even([2, 5, 3, 6]))
[2, 6]

Write a function, differences(xs,ys) that takes two lists of equal length, xs and ys, and
returns a list of the pairwise differences. One way to solve it is similar to the way you
did the pairwiseDifferences function in last weeks exercise. An other way is to use the
built in function zip(xs,ys) to get a list of all the matching pairs of xs and ys to
iterate through (look at the documentation yourself).

Example usage:

print(differences([2, 5, 3, 6], [1, 5, 6, 3]))
[1, 0, -3, 3]

Write a function squaredDifferences(xs, ys) that takes two lists of equal length, xs and
ys, and returns a list of the pairwise differences squared. You can use the
differences(xs,ys) function to make it easier to write this function.

Example usage:

print(squaredDifferences([2, 5, 3, 6], [1, 5, 6, 3]))
[1, 0, 9, 9]

Handing in

To hand in the assignment put the code in a file named after your self and the week. If it was me it would be Kasper_Munch_week4.py. Attach it to an email with subject “Assignment” and send it to Dan (das@birc.au.dk). You can see your hand in deadline on the main course page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: