Munch Lab

Applied programming week four

Lectures

At this week’s double we will look dictionaries. Dictionaries are a convenient way to
handle collections of things that you need to be able to access directly. We will also be
looking at how to read and write files.

At the single lecture will will walk through the assignment from last week and talk about
aliasing.

Computer exercises

Complete the the Dictionaries exercise. Then complete the Reading and Writing Files
exercise. Also try this set of interactive code snippets to help you understand scope and references. read this bit on Scope and references and try the interactive snippets of code to help you understand it.

Reading material

Chapter twelve and of How to think like a computer scientist and the following tutorial on
file processing.

Weekly assignment

This weeks assignment will walk you through a number of list manipulations. All the
functions you are asked to code up return a list. So the basic structure is looping over a
list (or lists) with a for loop, and in doing so, appending to a new list that is finally
returned by the function. Remember that apending to a list goes as follows:

L = []
L.append(x)

and that you have to define a list before you can append to it — which makes sense :-).

There is one caveat that you might run into so here is a heads up: if you use the
following construct:

lst = [2, 3, 4]
for i in lst:
    i = i**2

you will not be changing the lst list. The reason is that the i variable in the loop holds
copies of the elements in lst
not the elements themself. To change the input lst
parameter you would need to do the following:

lst = [2, 3, 4]
for i in range(len(lst)):
    lst[i] = lst[i]**2

Any way — this not a good way to implement it because this way you (maybe
unintentionally) change the list you give as as argument to your function. This happens
because list variables are references. To get arround this the first thing you need to do
in each function is to define a new list, e.g. L = [], and then fill results into that as
mentioned above.

The actual assignment

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 asand@cs.au.dk before 10am the following Wednesday.

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: