Assignment #02: using the standard library

Today's exercises have to be done with help of the python standard library alone! No external module can be used.

Exercise #02-01: cryptography for dummies Roman Emperors

A very simple encryption technique is called the Caesar cipher. The basic idea is that each letter is replaced by a letter that is a certain number of letters away, so for example if the shift was 2, then A would become C, B would become D, and Z will become B.

A. Write a function that given a string and a shift, will produce the encrypted string for that shift. The rules are:

  • you should accept lowercase and uppercase letters, and return letters of the same case
  • spaces or other punctuation characters should not be changed.

There are several ways to reach this result. Just pick the one which makes more sense to you. Then, decrypt the following message, which was encrypted with a shift of 13:

Pbatenghyngvbaf, lbh unir fhpprrqrq va qrpelcgvat gur fgevat.

B. Now write a decoding script which, when run in the linux or windows command line, prints the decoded phrase after asking the user to enter a phrase and a shift with which it was encrypted. Test your script on the sentence above.

C. Now try to decrypt this sentence, for which the shift is unknown:

Gwc uivioml bw nqvl bpm zqopb apqnb.

One way to solve this problem involves human decision, but another way can be fully automated (and implies more work). Pick the one you want!

Exercise #02-02: automated data download

SRTM is a digital elevation model at ~90 m resolution covering almost the entire globe (up to $\pm$ 60° latitude). The data is organized in 5°x5° tiles. To see a map of the tiles have a look at this download page. This tool is nice to use if you know which tile you want, but not very useful if you want more than one tile at different places of the globe.

Fortunately, the entire dataset is available on this server: http://droppr.org/srtm/v4.1/6_5x5_TIFs (click on the link to display the data). The file naming convention is very simple:

  • the first two digits number gives the location of the tile in the longitudes (starting at 180° West)
  • the last two digits gives the location of the tile in the latitudes (starting at 60° North and going southwards)

For example, here are some examples of locations and their associated tile:

  • (-179, 59) -> 'srtm_01_01.zip'
  • (-179, 51) -> 'srtm_01_02.zip'
  • (-174, 54) -> 'srtm_02_02.zip'

And so forth.

A. Write a script which, given a longitude and a latitude as arguments, downloads the corresponding file in the current directory. The function should raise an error when the given location is not valid.

Hints: define "valid" locations first: some are easy to catch, some cannot be caught automatically. Do we really have to deal with those?

B. Extend this script to be a bit more clever: download the data file only if the file isn't already available in the current directory.

C. Optional: extend this script to be even more clever: given a range of longitudes and latitudes, it should download all the files convering this area. For example, the range 9°W to 18°W and 44°N to 47°N would download 6 files.

Note 1: we will have a look at the data later in the course. But If you want to display them you can open them with the qgis software for example.

Note 2: at the edge of the tiles (i.e. coordinates -175, 55) the problem might be not well defined because of accuracy errors (we will get back to these). You shouldn't care about this for now and try to get things right for everywhere but at the exact boundaries.

Tips

Both scripts are asking the user for an input, but in a different way:

  • for #02-01, I am asking you to make use of python's input() function, which asks the user to type in the command line.
  • for #02-02, I am asking you to use the script with command line arguments.

You'll find numerous examples online for both procedures. Both work fine with ipython and/or spyder. For example, here is my script call for the optional exercise #02-02-C:

%run download_srtm.py 9 44 18 47

Back to the table of contents