Week 7

Import, File I/O and Consolidation

Note

As this is the last week of "Python Basics", there is a large consolidation exercise (number 3) which we highly recommend you try and do prior to next week's session - Solutions will be released by Thursday evening!

1 Import-ant Syntax

Import the following two packages, renaming them, such that you can get the following code snippet to run:

  • Import "numpy", renamed to "np"

  • Import the "pyplot" module from the "matplotlib" package, renamed to "plt"

Then run:

# Scary block of awful code
plt.scatter([0.35 * (2/7) + 0.15, 0.5 - 0.35 * (2/7) ], [2, 2], c='b', marker='s', s=300 )
plt.scatter(0.325, 0.75, c='k', marker='^', s=150);l = np.arange(0.15, 0.51, 0.01)
plt.plot(l ,np.sin(np.linspace(np.pi, 2*np.pi, len(l))), c="r")
plt.axis('off');plt.margins(0.25);plt.title("?sracs eseht tog I woh wonk annaW"[::-1])
plt.show()

You'll know when it's working ;)

2 Basic File I/O

Create

Create a file called musings.txt and write some some of your thoughts into it (be sure to write multiple lines)

Read

Close and reopen the musings.txt file, and use a for loop to go through and print out every line)

3 Wherefore art thou paragraph

Read the shakespeare.txt file:

  • Create a function which returns the first N paragraphs from the file

  • Make use of the fact that the line preceding each paragraph contains the corresponding paragraph number

There are two approaches to this problem:

  1. Use .readlines() and treat this as a list manipulation problem

  2. Use loops and .readline() to "walk through" the file, and format it as you go

The second approach is more instructive!

  1. Use a while loop to carry out .readline() until you reach the end of a paragraph

  2. Create a function which returns one paragraph, and put it inside another function which calls it N times

4 Caesar's Cipher Consolidates Cool Class

Hold on to your hats, because this is a big one! To complete this exercise you'll need to use almost everything we've learned so far, so make sure you're sitting comfortably (preferably with a caffeinated beverage at hand) and let's go!

This is quite a tricky exercise, but I strongly suggest you give it a go, as we'll be moving onto specialized topics next week, and an understanding of the python "basics" will be key.

The goal of this exercise to to create a function which will decipher files, given a decode word, and it's frequency. To succeed you should find the source of each of the encrypted messages (the file names offer a slight clue)

More specifically, you will be provided with a word in normal English, and the number of times it is used in the paragraph, and must use this to find out the shift of the alphabet that has been applied.

You'll want to count the frequency of each encrypted word, and find the one that matches the decode word - dictionaries might be a helpful way of doing this ;)

The type of cipher being used is the simple Caesar Cipher, which works by shifting the alphabet:

The files containing the encrypted messages, and there corresponding decode word, are given below:

For Mary, the decode word is "the" and the frequency is 4

For Charles, the decode word is "of" and the frequency is 12

For Agatha, the decode word is "train", and the frequency is 2

Challenge

For Jane, you are given that "is" and "in" occur the same number of times within the text

Be careful with punctuation in both of the following approaches!

Decode Approach 1 - Alphabet List

Use the alphabet list provided below, and the .index() function, to find the shift being applied in the cipher:

You'll find the .index(element) function useful - when called on a list, it returns the first index at which a matching element was found

Here's a list of the alphabet as well:

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Decode Approach 2 - ASCII Conversion

Using the typecasts which convert characters (strings of length 1) to ASCII numbers, and vice versa, find the shift being applied by the cipher.

Typecasts:

  • Character -> (ASCII) Integer: ord()

  • (ASCII) Integer: chr()

Last updated