valentineap / ComputationalGeoscienceCourse

Materials for an introductory course in Python programming for geoscientists
7 stars 1 forks source link

Exercise 7 ready for review #12

Closed valentineap closed 6 years ago

valentineap commented 6 years ago

String parsing....

https://github.com/valentineap/ComputationalGeoscienceCourse/tree/master/Practicals/Exercise%2007%20-%20Strings

charlesll commented 6 years ago

Reviewed, happy about it!

oscarbranson commented 6 years ago

Nice and clear, and covers the basics.

I wonder whether it might be useful to go a bit further into string parsing (i.e. regular expressions), and/or have an exercise working with stringsthat goes a bit deeper?

Regular expressions might be a bit much at this point - they could be a practical on their own, which I'd be happy to put together.

A possible string-based exercise could be implementing a basic substitution cipher? For example, something along the lines of:

def encode_substitution(text, keyword='scramble'):
    # make sure they're both lower case
    text = text.lower()
    keyword = keyword.lower()

    # generate alphabet lookup table
    alphabet = [chr(c) for c in range(97,123)]  # introduces chr and ord for unicode lookups
    encoded = [k for k in keyword] + [a for a in alphabet if a not in keyword]  # generate substituted alphabet
    lookup = dict(zip(alphabet, encoded))  # combine into a dictionary

    # add any special characters to lookup
    for c in text:
        if c not in lookup:
            lookup[c] = c

    # encode text
    return ''.join([lookup[c] for c in text])

def decode_substitution(text, keyword='scramble'):
    # make sure they're both lower case
    text = text.lower()
    keyword = keyword.lower()

    # generate alphabet lookup table
    alphabet = [chr(c) for c in range(97,123)]
    encoded = [k for k in keyword] + [a for a in alphabet if a not in keyword]
    lookup = dict(zip(encoded, alphabet))

    # add any special characters to lookup
    for c in text:
        if c not in lookup:
            lookup[c] = c

    # decode text
    return ''.join([lookup[c] for c in text])

This might be a nice gently introduction to the idea of lookup tables in general, and spcifically to unicode character mappings.

If you think this is a good idea, I'd be happy to put it in. Probably provide the above functions as .pyc files, and ask them to reproduce the functionality?

valentineap commented 6 years ago

Nice idea @oscarbranson , have added a simple example using Caesar cipher (i.e. shunting everything up or down the alphabet by N places).

Now ready for @rmcgirr94 .

oscarbranson commented 6 years ago

Oooh neat - then maybe you could bring this in later where N is unknown and they have to decipher some text?!

valentineap commented 6 years ago

Already there :)

On 12 Sep 2018, at 11:48, Oscar notifications@github.com wrote:

Oooh neat - then maybe you could bring this in later where N is unknown and they have to decipher some text?!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

-- Dr Andrew Valentine Fellow, Seismology & Mathematical Geophysics

Research School of Earth Sciences The Australian National University 142 Mills Road, Acton ACT 2601

+61 (0)2 612 53424 Office: Jaeger 2, 147a

http://rses.anu.edu.au/~andrewv

rebecca-mcgirr commented 6 years ago

Tested, no changes to make. Closing issue.