Munch Lab

Applied programming week three

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.

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')

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')
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:

  • 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 Kasper_Munch_week3.py. Attach it to an email with subject “Assignment”
and send it to your TA.

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 )

w

Connecting to %s

%d bloggers like this: