Practice Exercises
Practice Exercises 1A
Using the cipher wheel or St. Cyr Slide, encrypt the following sentences with key 5.
- Eat food.
- Mostly plants.
- Not too much.
Using the cipher wheel or St. Cyr Slide, decrypt the following sentences with key 17.
- Z YRMV DP WRKYVI'J VPVJ.
- REU DP DFKYVI’J EFJV.
- ZE R ARI LEUVI DP SVU.
Practice Exercises 1B
Encrypt the following sentences in the Caesar cipher with the given keys:
- "AMBIDEXTROUS: Able to pick with equal skill a right-hand pocket or a left." with the key 4.
- "GUILLOTINE: A machine which makes a Frenchman shrug his shoulders with good reason." with key 17.
- "IMPIETY: Your irreverence toward my deity." with key 21.
Decrypt the following ciphertexts with the given keys:
- "ZXAI: P RDHIJBT HDBTIXBTH LDGC QN HRDIRWBTC XC PBTGXRP PCS PBTGXRPCH XC HRDIAPCS." with key 15.
- "MQTSWXSV: E VMZEP EWTMVERX XS TYFPMG LSRSVW." with key 4.
Encrypt the following sentence with the key 0:
- "This is a silly example."
Here are some words and next to them the same word encrypted. What key was used?
- ROSEBUD – LIMYVOX
- YAMAMOTO – PRDRDFKF
- ASTRONOMY – HZAYVUVTF
The following sentence was encrypted with the key 8. What does it decrypt to when you use the key 9 instead?
- "UMMSVMAA: Cvkwuuwv xibqmvkm qv xtivvqvo i zmdmvom bpib qa ewzbp epqtm."
Practice Exercises 3A
- What are the operators for addition, subtraction, and multiplication?
- Which is the operator for division:
/
or\
? - Which of these is an integer value, and which is a floating point value?
42
3.141592
- Which of these lines are not expressions?
4 x 10 + 2
3 * 7 + 1
2 +
42
Practice Exercises 3B
- If you enter this code into the interactive shell, what does it print out?
>>> spam = 20 >>> spam
- If you enter this code into the interactive shell, what does it print out?
>>> spam = 20 >>> spam = 30 >>> spam
- If you enter this code into the interactive shell, what does it print out?
>>> spam = 20 >>> spam = spam + 20 >>> spam
- If you enter this code into the interactive shell, what does it print out?
>>> spam = 20 >>> spam + 20 40 >>> spam
- If you enter this code into the interactive shell, what does it print out?
>>> spam = 20 >>> SPAM = 30 >>> spam
- If you enter this code into the interactive shell, what does it print out?
>>> spam =
- If you enter this code into the interactive shell, what does it print out?
>>> monkeyplumber
- Which of these lines is an expression, and which of these is a statement?
>>> 2 + 2 4 >>> spam = 42
- Is this line an expression or a statement?
>>> 5 5
Practice Exercises 4A
- If you enter this code into the interactive shell, what does it print out?
- If you enter this code into the interactive shell, what does it print out?
- If you enter this code into the interactive shell, what does it print out?
- If you enter this code into the interactive shell, what does it print out?
- If you enter this code into the interactive shell, what does it print out?
>>> spam = 'Cats' >>> spam
>>> spam = 'Cats' >>> spam + spam + spam
>>> spam = 'Cats' >>> spam * 3
>>> print("Dear Alice,\nHow are you?\nSincerely,\nBob")
>>> print("Hello" + 'Hello')
Practice Exercises 4B
If you enter the following pieces of code into the interactive shell, what does it print out?
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[0])
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[5])
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[-1])
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[-3])
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[0:4] + spam[5])
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[-3:-1])
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[:10])
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[-5:])
>>> spam = 'Four score and seven years is eighty seven years.' >>> print(spam[:])
Practice Exercises 4C
- Which window has the
>>>
prompt? The interactive shell or the file editor? - Which window runs one Python instruction at a time, immediately? The interactive shell or the file editor?
- What does the following Python instruction print out?
- What function displays text on the screen?
- What function lets the user type text into the program?
- What does it mean if your program calls
input()
and you type inblah
, and the program displays this error:
#print('Hello world!')
NameError: name 'Albert' is not defined
Practice Exercises 5A
What do the following pieces of code display on the screen?
print(len('Hello') + len('Hello'))
i = 0 while i < 3: print('Hello') i = i + 1
i = 0 spam = 'Hello' while i < 10: spam = spam + spam[i] i = i + 1 print(spam)
i = 0 while i < 4: while i < 6: i = i + 2 print(i)
Practice Exercises 6A
Using the cipher program, encrypt the following sentences with the given keys:
- “'You can show black is white by argument,' said Filby, 'but you will never convince me.'” with the key 8.
- “1234567890” with key 21.
Using the cipher program, decrypt the following ciphertexts with the given keys:
- “'Kv uqwpfu rncwukdng gpqwij.'” with key 2.
- “Xqp whh ahoa kb pda sknhz swo ejreoexha.” with key 22.
Using the cipher program, encrypt the following sentence with the key 0:
- “This is still a silly example.”
Practice Exercises 6B
- What Python instruction would import a module named
watermelon.py
? - The variable
SPAM
is a constant. Will this code cause an error?:SPAM = 'hello'
What do the following pieces of code display on the screen?
for i in 'hello': print(i)
print('Hello'.lower())
print('Hello'.upper())
print('Hello'.upper().lower().upper().lower())
BONUS: What does this program display on the screen? (Guess, and then type it in and run it to find out.)
spam = 'foo' for i in spam: spam += i print(spam)
Practice Exercises 6C
What do the following pieces of code display on the screen?
if 10 < 5: print('Hello')
if 10 < 5: print('Hello') else: print('Goodbye')
if 10 < 5: print('Hello') elif 5 == 5: print('Alice') else: print('Goodbye')
if 10 < 5: print('Hello') elif False: print('Alice') else: print('Goodbye')
if 10 < 5: print('Hello') elif False: print('Alice') elif 5 != 5: print('Bob') else: print('Goodbye')
if 10 < 5: print('Hello') elif 5 == 5: print('Alice') else: print('Goodbye')
if 10 < 5: print('Hello') else: print('Goodbye') elif 5 == 5: print('Alice')
print('f' in 'foo')
print('f' not in 'foo')
print('foo' in 'f')
print('hello'.find('o'))
print('hello'.find('oo'))
Practice Exercises 7A
Break the following ciphertexts:
- R UXEN VH TRCCH,
- FR DBMMR EHOXL FX,
- CXPNCQNA FN'AN BX QJYYH,
- OBR OZKOMG QOFSTFSS.
- PDKQCD IU DAWZ DWO OQOLEYEKJO,
- FTMF U WQQB GZPQD YK TMF,
- AR ITMF YUSTF TMBBQZ,
- DA D NCMVIF OJ OCZ NDUZ JA V MVO.
- ZFBI. J'N QSFUUZ TVSF NZ DBU XPVME FBU NF.
Practice Exercises 8A
With paper and pencil, encrypt the following messages with the key 9 using the transposition cipher. The (s) parts mark a single space. The number of characters has been provided for your convenience.
- Underneath(s)a(s)huge(s)oak(s)tree(s)there(s)was(s)of(s)swine(s)a(s)huge(s)company, (61 characters)
- That(s)grunted(s)as(s)they(s)crunched(s)the(s)mast:(s)(s)For(s)that(s)was(s)ripe,(s)and(s)fell(s)full(s)fast. (79 characters)
- Then(s)they(s)trotted(s)away,(s)for(s)the(s)wind(s)grew(s)high:(s)(s)One(s)acorn(s)they(s)left,(s)and(s)no(s)more(s)might(s)you(s)spy. (96 characters)
Practice Exercises 8B
In the following programs, is spam
a global or local variable? Or are there both a global and local variables named spam
?
spam = 42
def foo(): spam = 42
spam = 42 def foo(): spam = 42
spam = 42 def foo(): print(spam)
spam = 42 def foo(): global spam print(spam)
spam = 42 def foo(): global spam spam = 99 print(spam)
spam = 42 def foo(): spam = 99 print(spam)
Practice Exercises 8C
What value do each of the following expressions evaluate to?
- [0, 1, 2, 3, 4][2]
- [1, 2, 3, 4][2]
- [[1, 2], [3, 4]][0]
- [[1, 2], [3, 4]][1]
- [[1, 2], [3, 4]][0][1]
- [[1, 2], [3, 4]][1][1]
- ['hello'][0]
- ['hello'][0][1]
- [2, 4, 6, 8, 10][1:3]
- list('Hello world!')
- list(range(10))
- list(range(10))[2]
Practice Exercises 8D
What value do each of the following expressions evaluate to?
- len([2, 4])
- len([])
- len(['', '', ''])
- [4, 5, 6] + [1, 2, 3]
- 3 * [1, 2, 3] + [9]
- 42 in [1, 2, 3]
- 42 in [41, 42, 42, 42]
Practice Exercises 8E
- What are the four augmented assignment operators?
What do the following pieces of code display on the screen?
spam = 'hello' spam += 'world' print(spam)
spam = 3 spam += 3 spam += 3 print(spam)
''.join(['hello', 'world'])
' '.join(['my', 'very', 'special', 'mudder'])
Practice Exercises 9A
With paper and pencil, decrypt the following messages with the key 9. The (s) parts mark a single space. The total number of characters is already counted for you.
- H(s)cb(s)(s)irhdeuousBdi(s)(s)(s)prrtyevdgp(s)nir(s)(s)eerit(s)eatoreechadihf(s)paken(s)ge(s)b(s)te(s)dih(s)aoa.da(s)tts(s)tn (89 characters)
- A(s)b(s)(s)drottthawa(s)nwar(s)eci(s)t(s)nlel(s)ktShw(s)leec,hheat(s).na(s)(s)e(s)soogmah(s)a(s)(s)ateniAcgakh(s)dmnor(s)(s) (86 characters)
- Bmmsrl(s)dpnaua!toeboo'ktn(s)uknrwos.(s)yaregonr(s)w(s)nd,tu(s)(s)oiady(s)hgtRwt(s)(s)(s)A(s)hhanhhasthtev(s)(s)e(s)t(s)e(s)(s)eo (93 characters)
Practice Exercises 9B
If you enter the following pieces of code into the interactive shell, what does it print out?
>>> math.ceil(3.1)
>>> math.ceil(3.0)
>>> math.floor(3.1)
>>> math.floor(3.0)
>>> round(3.1)
>>> round(3.0)
>>> round(3.5)
>>> True and True
>>> True and False
>>> False and True
>>> False and False
>>> True or True
>>> True or False
>>> False or True
>>> False or False
>>> not True
>>> not not True
Practice Exercises 9C
- Draw out the complete truth tables for the
and
,or
, andnot
operators. - Which is correct?
if __name__ == '__main__':
if __main__ == '__name__':
if _name_ == '_main_':
if _main_ == '_name_':
Practice Exercises 10A
- If you ran this program and it printed the number 8, what would it print the next time you run it?
import random random.seed(9) print(random.randint(1, 10))
- What does this program print out?
spam = [1, 2, 3] eggs = spam ham = eggs ham[0] = 99 print(ham == spam)
- What module contains the
deepcopy()
function? - What does this program print out?
import copy spam = [1, 2, 3] eggs = copy.deepcopy(spam) ham = copy.deepcopy(eggs) ham[0] = 99 print(ham == spam)
Practice Exercises 11A
- Which is correct:
os.exists()
oros.path.exists()
- When is the UNIX epoch?
What do the following expressions evaluate to?
- 'Foobar'.startswith('Foo')
- 'Foo'.startswith('Foobar')
- 'Foobar'.startswith('foo')
- 'bar'.endswith('Foobar')
- 'Foobar'.endswith('bar')
- 'The quick brown fox jumped over the yellow lazy dog.'.title()
Practice Exercises 12A
- What is a dictionary file?
- Why is being able to detect English useful for cryptanalysis?
- Would a dictionary file that only had a couple hundred words work for detecting English?
Practice Exercises 12B
- What is a Python dictionary?
- Python lists use square brackets [ and ] to type them out. What do dictionaries use?
- What does the following code print?
>>> spam = {'name': 'Al'}
>>> spam['name']
>>> print(spam['name'])
Practice Exercises 12C
- What does this code print?
>>> spam = {'eggs': 'bacon'}
>>> print(len(spam)) - What does this code print?
>>> spam = {'eggs': 'bacon'}
>>> print('eggs' in spam) - What does this code print?
>>> spam = {'eggs': 'bacon'}
>>> print('bacon' in spam) - What
for
loop code would print out the values in the followingspam
dictionary?
>>> spam = {'name': 'Zophie', 'species':'cat', 'age':8}
Practice Exercises 12D
- What are three differences between dictionaries and lists?
- What does the following print?
>>> print('Hello, world!'.split())
- What is the None value?
- How many different values does the NoneType data type have?
- What happens if your code attempts to divide by zero?
Practice Exercises 12E
- Where does the
append()
method add values to a list? - What is a default argument?
- What will the following print?
def spam(eggs=42): print(eggs)
spam()
spam('Hello') - What percentage of words in this sentence are valid English words?
"Whether it's flobulllar in the mind to quarfalog the slings and arrows out outrageous guuuuuuuuur."
Practice Exercises 13A
- What does this expression evaluate to:
' Hello world'.strip()
- Which characters are whitespace characters?
- Why does
'Hello world'.strip('o')
evaluate to a string that still has o's in it? - Why does
'xxxHelloxxx'.strip('X')
evaluate to a string that still has x's in it?
Practice Exercises 14A
- What do the following expressions evaluate to?
11 % 5
21 % 7
17 % 1000
5 % 5
Practice Exercises 14B
- Is 4 a factor of 8?
- Is 4 a factor of 10?
- Is 4 a factor of 12?
- What is the greatest common factor of 10 and 15?
- What is the greatest common factor of 6 and 8?
Practice Exercises 14C
- What does spam contain after executing
spam, eggs = 'hello', 'world'
? - The greatest common factor of 17 and 31 is 1. Are 17 and 31 relatively prime?
- Why aren't 6 and 8 relatively prime?
- If two numbers are realtively prime, are they also coprime?
- What's the difference between a divisor and a factor?
- Write out the code for the Python function that calculates greatest common divisor.
- Write it out again, just to help yourself memorize it.
Practice Exercises 14D
- In the shift cipher, the symbol's number is added to the key's number. What is done in the multiplicative cipher?
Practice Exercises 14E
- What is the multiplicative inverse a number A?
- What is a modular inverse of A mod C?
Practice Exercises 15A
- The affine cipher can be thought of as the combination of which two other ciphers?
- What is a tuple?
- How is a tuple different from a list?
- Why does having Key A be 1 make the affine cipher weak?
- Why does having Key B be 0 make the affine cipher weak?
Practice Exercises 16A
- What does
2 ** 5
evaluate to? - What does
6 ** 2
evaluate to? - What does the following code print out?
for i in range(5): if i == 2: continue print(i)
- Does the
main()
function of affineHacker.py get called if another program runsimport affineHacker
?
Practice Exercises 17A
- Why can't brute force be used against a simple substitution cipher, even with a powerful supercomputer?
Practice Exercises 17B
- What does the spam variable contain after running this code?
spam = [4, 6, 2, 8]
spam.sort()
- What is a wrapper function?
- What does
'Hello'.isupper()
evaluate to? - What does
'hello'.islower()
evaluate to? - What does
'HELLO 123'.isupper()
evaluate to? - What does
'123'.islower()
evaluate to?
Practice Exercises 17C
- How can you modify an encryption algorithm to encrypt spaces and punctuation characters?
Practice Exercises 18A
- What is the word pattern for the word "hello"?
- Do "mammoth" and "goggles" have the same word pattern?
- If the variable spam contains a list of lists of strings, how can you display this is an orderly, pretty way on the screen?
- Which could be the possible plaintext word for the cipherword PYYACAO: ALLEGED, EFFICIENTLY, or POODLE?
Practice Exercises 19A
- The vigenere is similar to which other cipher?
- How many possible keys are there for a simple substitution key with a key length of 10?
* Hundreds
* Thousands
* Millions
* Much, much more than a million
Practice Exercises 20A
- What is frequency analysis?
- What are the six most common letters in English?
- What does the spam variable contain after running this code?
spam = [4, 6, 2, 8]
spam.sort(reverse=True)
Practice Exercises 21A
- What is a dictionary attack?
- What does Kasiski Examination of a ciphertext reveal?
- What two things does converting a list value to a set value with the set() function do?
- If the spam variable contains ['cat', 'dog', 'mouse', 'dog']. This list has 4 items in it. How many items does the list returned from list(set(spam)) have?
- What does the following code print?
print('Hello', end='')
print('World')
Practice Exercises 22A
- Why is there no "one-time pad" program presented in this chapter?
- What cipher is the "two-time pad" equivalent to?
- Would using a key twice as long as the plaintext message make the one-time pad twice as secure?
Practice Exercises 23A
- How many primes numbers are there?
- What are integers that are not prime called?
- What are two algorithms for finding prime numbers?
Practice Exercises 24A
- What's the difference between a symmetric cipher and an asymmetric cipher?
- Alice generates a public key and a private key. After a while, she accidentally deletes her private key. Will other people be able to send her encrypted messages? Will she be able to decrypt messages previously sent to her?
- What is authentication and confidentiality? How are they different?
Practice Exercises 24B
- What is nonrepudiation?
- Alice generates a public key and a private key. After a while, she loses her private key. Will she be able to digitally sign documents? Will other people able to verify her previously signed documents?
- The function call
ord('A')
returns the integer65
. What doeschr(65)
return?
Practice Exercises 24C
- In cryptography, what is a block?
- What's the data type of the value
b'hello'
? - The
encode()
string method returns a value of what data type? - The
decode()
bytes method returns a value of what data type?
Practice Exercises 24D
- The variable spam contains the list
['cat', 'dog', 'mouse']
. What does it contain afterspam.insert(1, 'hello')
runs? - What does the function call
pow(2, 4)
evaluate to? - What does the function call
pow(2, 4, 10)
evaluate to?