Tutorial Python 02

download Tutorial Python 02

of 2

Transcript of Tutorial Python 02

  • 8/13/2019 Tutorial Python 02

    1/2

    Technische Universitat Munchen GRS, June 2013Institut fur InformatikTobias Neckel

    Python course - Tutorial 2

    Greatest Common Divisor

    To get more familiar with functions, write a function that computes the greates commondivisor for two integers a, b N.

    Using a loop, determine the gcd.

    Now write a function that recursively determines the solution. You can make use of thefollowing recursive formulation

    gcd(a, b) = a, if b=0

    = gcd(b, a mod b), else

    Caesars Cipher

    One of the oldest known cryptographic schemes is called Caesars Cipher. It is named af-ter Julius Caesar, as he has reportedly used this scheme to encrypt confidential (military)messages.

    To this end, he shifted all letters in the alphabet three positions to the right. I.e., A is replacedby D, B by E, . . . Therefore, the alphabet is wrapped around: at the end of the alphabet, thenext letter is A again. To conceal the regular structure of words in a text, whitespaces haveto be deleted.

    Implement the encryption and decryption algorithm for Caesars shift pattern. Deleteall whitespaces in the original text and take care to output only either lowercase oruppercase letters.

    To this end, you might find the following two functions useful: ord(c) returns theindex of the character c in the ASCII-table, chr(n) the character at index n;chr(ord(c))==c.

  • 8/13/2019 Tutorial Python 02

    2/2

    2

    Extend your Python program so that it has command-line parameters for

    a filename of a text file to encrypt/decrypt,

    an output filename to which the result is saved,

    the shift (allowing for arbitrary shifts), assuming 3 as default,

    and so that it provides help on the command-line parameters with -hand --help.

    If no filename is specified, read a text from standard input and write the result tostandard output. This allows, e.g., the following usage:

    $ echo H e l l o C ae sa r | . / c a e s a r s . pyKHOORFDHVDU

    Extend your program once more to a more secure encryption scheme. Allow a user to

    specify a custom (bijective) mapping from a-z to an arbitrary permutation of a-z. Forexample, one mapping could be

    source: ABCDEFGHIJKLMNOPQRSTUVWXYZ

    target: BSVIHJZDRGPKTQUWXLNYACOEMF

    With this mapping, a shift of 3 for character a leads to n by ABSN. The-refore, let the user specify the target permutation (in the example "BSVI...EMF") as acommand line parameter.

    Hint: one possible way is to use the sequence types (lists, dictionaries, . . . ) to achievethis.

    Chocolate option!Extend your previous program so that it automatically tries to de-crypt an encrypted text by finding out which shift to use. You can assume:

    That the text has been encrypted usingthe procedure mentioned above using Caesars Cipherwith an arbitrary shift.

    That the text is in English!

    That the text contains no whitespaces.

    Send your result until 13:30 to [email protected] (or hand it inin some other way). The result working best on some texts we havegets a bar of chocolate.

    (Optional, if we already covered modules in the lecture:) If you like to practise howmodules work, extend/rewrite your Python program, so that it can be both executed asa program and used as a module, providing the basic functionality such as encryptionand decryption of a text.