11. 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
Write a function that will return the number of digits in an integer.
Write a function that removes the first occurrence of a string from another string.
Write a function
reverse
that receives a string argument, and returns a reversed version of the string.Write a function that recognizes palindromes. (Hint: use your
reverse
function to make this easy!).Write a function that mirrors its argument. For example,
mirror('good')
should return a string holding the valuegooddoog
. (Hint: Make use of thereverse
function).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
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 (using the remainder operator).
Graded Lesson Assignment¶
Write a function analyze_text
that receives a string as input. Your function should count the number of alphabetic characters (a through z, or A through Z) in the text and also keep track of how many are the letter 'e'
(upper or lowercase).
Your function should return an analysis of the text in the form of a string phrased exactly like this:
“The text contains 240 alphabetic characters, of which 105 (43.75%) are ‘e’.”
You will need to make use of the isalpha
function, which can be used like this
"a".isalpha() # => evaluates to True
"3".isalpha() # => evaluates to False
"&".isalpha() # => False
" ".isalpha() # => False
mystr = "Q"
mystr.isalpha() # => True
Ready to submit your code? Time to fork, and submit a Pull Request to: this Github Repository.