Lectures

At this week's double lecture we will look at strings which is Python's way of storing a sequence of characters like "Hello". We will also look at lists which is pythons way of handling - well lists of things. Your will also learn about "methods" which is a special kind of functions that are packaged with complex types like strings and lists.

We will spend some of the single lecture going through the assignment and but also talk more about functions, modules and scopes of variables.

Reading material

Chapter seven and nine of How to think like a computer scientist.

Computer exercises

Complete the the Strings exercise and then the Lists exercise.

Weekly assignment

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

Palindromes

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 otherwise.

Example usage:

print isPalindrome('abcba')
True
print isPalindrome('foo')
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')
2

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:

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 Kasper_Munch_week3.py. Attach it to an email with subject "Assignment" and send it to asand@cs.au.dk before Wednesday at 10am.

Weekly evaluation

EvaluationWeek3

Index

Contact info

Office address:
Bioinformatics Research Centre (BiRC)
Aarhus University
C.F. Møllers Allé 8
DK-8000 Aarhus C
Denmark
Office phone:
+45 871 55558
Mobile phone:
3013 8342
Email:
kaspermunch@birc.au.dk