Munch Lab

Applied programming 2014 week three


This week I will talk about Lists, which are Pythons way of representing – well lists. I will also talk about how to interact with files for input and output, and I will introduce the extremely useful dictionary data type.

Computer exercises

Complete the the exercises in Comparing HIV sequences.

Reading material

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

Weekly assignment

This weeks assignment will walk you through some of the most common string manipulations.

A palindrome is a string that is spelled the same way backwards and forwards.

Write a function, isPalindrome(s), that returns True if s is a palindrome and False

Example usage:

print isPalindrome('abcba')

Should print True

print isPalindrome('foo')

Should print False

One approach to this is to run through s from the first to the middle character and for
each character check if the character is equal to the character at the same index from the
right rather than the left. Remember that the first character of a string is at index 0
and the last at index -1, the second character is at index 1 and the second last at index
-2 and so forth.

Since you need to run through the string from the first to the middle character you first
need to figure out how many characters that corresponds to. Say your palindrome is
p="ACTGTCA", then the number of indexes you need to loop over with a for loop is
len(p)/2. Figure out how to make range() return indexes you can use to access the
characters in the first half of the sequence. Then make a for loop where you iterate
over the indexes you get from range(). Try to make the for loop print out the first
half of the characters.

Once you get this far you need to compare each character from the first half to the
corresponding ones starting from the other end of the palindrome. Figure out how to change
each index used for the first half to the corresponding index for the other half so you
can compare the relevant pairs. (You need to compare index 0 with -1, 1 with -2 and so on…)

Now try to make the for loop print both the character from the first half and the
corresponding character from the other end. If you got the indexes right you will see that
the A print with the A from the other end, the C with the C and so on.

Write an if statement in the for loop that tests if the two corresonding characters are the same. If your string is a palindrome all pairs are the same. So as soon as you see a
pair that is not the same, you know it is not a palindrome and you can let your function
return False like this:

if leftCharacter != rightCharacter:
    return False

On the other hand, if all pairs pass this test then it is a palindrome and the function
should return True when exiting the for loop.

Word count

Write a function, wordCount(s), that counts the number of words in a string.

Example usage:

print wordCount('foo bar')

Useful method:

Re-formatting text

Write a function, reformat(s), that takes a string, s, and replaces all white spaces
(spaces, tabs) with a single space.

Example usage:

s = "foo    bar  baz"
print reformat(s)
foo bar baz

Useful methods:

  • split()
  • join()

go look them up – or just google “python join”.

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 Attach it to an email with subject “Assignment” and send it to Dan ( 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: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: