Exercises next week

Next week we'll attempt something a little different with the exercises: today we'll randomly assign a single question to an individual to complete before next week's club, where they will explain the solution to everyone else. Next week there are nine exercises in total to pick from so there will be some leftovers for us to attempt in the class too.


In [2]:
import numpy as np

def assign_question(people, questions):
    questions = np.random.permutation(questions)
    for ip, p in enumerate(people):
        print('{}\t:{}'.format(p, questions[ip]))

In [3]:
assign_question(['Dan', '...who else will be here...'], range(1, 9+1)) # Why did use range(1, 9+1)?


Dan	:7
...who else will be here...	:3

All exercises from Downey, Allen. Think Python. Green Tea Press, 2014. http://www.greenteapress.com/thinkpython/

Exercise 8.1

Write a function that takes a string as an argument and displays the letters backward, one per line.

Exercise 8.2

Modify the following programming to print Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack

prefixes = 'JKLMNOPQ'
suffix = 'ack'

for letter in prefixes:
    print letter + suffix

Exercise 8.3

Given that fruit is a string, what does fruit[:] mean?

Exercise 8.4

Modify find so that it has a third parameter, the index in word where it should start looking.

def find(word, letter):
    index = 0
    while index < len(word):
        if word[index] == letter:
            return index
        index = index + 1
    return -1

Exercise 8.5

Encapsulate this code in a function named count, and generalize it so that it accepts the string and the letter as arguments.

Exercise 8.6

Rewrite this function so that instead of traversing the string, it uses the three-parameter version of find from the previous section.

Exercise 8.7

There is a string method called count that is similar to the function in the previous exercise. Read the documentation of this method and write an invocation that counts the number of as in 'banana'.

Exercise 8.8

Read the documentation of the string methods at http://docs.python.org/2/library/stdtypes.html#string-methods. You might want to experiment with some of them to make sure you understand how they work. strip and replace are particularly useful.

Exercise 8.10

A string slice can take a third index that specifies the “step size;” that is, the number of spaces between successive characters. A step size of 2 means every other character; 3 means every third, etc.

>>> fruit = 'banana'
>>> fruit[0:5:2]
'bnn'

A step size of -1 goes through the word backwards, so the slice [::-1] generates a reversed string. Use this idiom to write a one-line version of is_palindrome from Exercise 7.6.

Exercise 8.12

ROT13 is a weak form of encryption that involves “rotating” each letter in a word by 13 places. To rotate a letter means to shift it through the alphabet, wrapping around to the beginning if necessary, so ’A’ shifted by 3 is ’D’ and ’Z’ shifted by 1 is ’A’. Write a function called rotate_word that takes a string and an integer as parameters, and that returns a new string that contains the letters from the original string “rotated” by the given amount.

For example, “cheer” rotated by 7 is “jolly” and “melon” rotated by -10 is “cubed”.

You might want to use the built-in functions ord, which converts a character to a numeric code, and chr, which converts numeric codes to characters.