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)?
All exercises from Downey, Allen. Think Python. Green Tea Press, 2014. http://www.greenteapress.com/thinkpython/
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.
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.
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.