Today's exercises have to be done with help of the python standard library alone! No external module can be used.
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:
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!
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:
For example, here are some examples of locations and their associated tile:
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.
Both scripts are asking the user for an input, but in a different way:
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