9.22. Exercises¶
What is the result of each of the following:
‘Python’[1]
“Strings are sequences of characters.”[5]
len(“wonderful”)
‘Mystery’[:4]
‘p’ in ‘Pineapple’
‘apple’ in ‘Pineapple’
‘pear’ not in ‘Pineapple’
‘apple’ > ‘pineapple’
‘pineapple’ < ‘Peach’
‘Python’[1] = ‘y’
‘Strings are sequences of characters.’[5] = ‘g’
len(‘wonderful’) = 9
‘Mystery’[:4] = ‘Myst’
‘p’ in ‘Pineapple’ = True
‘apple’ in ‘Pineapple’ = True
‘pear’ not in ‘Pineapple’ = True
‘apple’ > ‘pineapple’ = False
‘pineapple’ < ‘Peach’ = False
-
In Robert McCloskey’s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop tries to output these names in order.
prefixes = "JKLMNOPQ" suffix = "ack" for p in prefixes: print(p + suffix)
Of course, that’s not quite right because Ouack and Quack are misspelled. Can you fix it?
Assign to a variable in your program a triple-quoted string that contains your favorite paragraph of text - perhaps a poem, a speech, instructions to bake a cake, some inspirational verses, etc.
Write a function that counts the number of alphabetic characters (a through z, or A through Z) in your text and then keeps track of how many are the letter ‘e’. Your function should print an analysis of the text like this:
Your text contains 243 alphabetic characters, of which 109 (44.8%) are 'e'.
-
Print out a neatly formatted multiplication table, up to 12 x 12.
-
Write a function that reverses its string argument.
Write a function that mirrors its string argument, generating a string containing the original string and the string backwards.
-
Write a function that removes all occurrences of a given letter from a string.
Write a function that recognizes palindromes. (Hint: use your
reverse
function to make this easy!).Write a function that counts how many non-overlapping occurences of a substring appear in a string.
-
Write a function that removes all occurrences of a string from another string.
Here is another interesting L-System called a Hilbert curve. Use 90 degrees:
L L -> +RF-LFL-FR+ R -> -LF+RFR+FL-
-
Here is a dragon curve. Use 90 degrees.:
FX X -> X+YF+ Y -> -FX-Y
Here is something called an arrowhead curve. Use 60 degrees.:
YF X -> YF+XF+Y Y -> XF-YF-X
-
Try the Peano-Gosper curve. Use 60 degrees.:
FX X -> X+YF++YF-FX--FXFX-YF+ Y -> -FX+YFYF++YF+FX--FX-Y
-
Write a function that implements a substitution cipher. In a substitution cipher one letter is substituted for another to garble the message. For example A -> Q, B -> T, C -> G etc. your function should take two parameters, the message you want to encrypt, and a string that represents the mapping of the 26 letters in the alphabet. Your function should return a string that is the encrypted version of the message.
Write a function that decrypts the message from the previous exercise. It should also take two parameters. The encrypted message, and the mixed up alphabet. The function should return a string that is the same as the original unencrypted message.
-
Write a function called
remove_dups
that takes a string and creates a new string by only adding those characters that are not already present. In other words, there will never be a duplicate letter added to the new string. Write a function called
rot13
that uses the Caesar cipher to encrypt a message. The Caesar cipher works like a substitution cipher but each character is replaced by the character 13 characters to ‘its right’ in the alphabet. So for example the letter a becomes the letter n. If a letter is past the middle of the alphabet then the counting wraps around to the letter a again, so n becomes a, o becomes b and so on. Hint: Whenever you talk about things wrapping around its a good idea to think of modulo arithmetic.-
Modify this code so it prints each subtotal, the total cost, and average price to exactly two decimal places.