Immersion Practice invoking functions

Cryptic notes

You are studying one night in the library and came across a cryptic notebook that seems to penned by Mary Lyon. They're from 1836, when Mary Lyon was just beginning an endeavor to start a new institution for the education of women. She has been raising funds, despite the country being in an economic depression, and seems to be communicating with a team in secret.

We will be communicating through the new telegraph technology. Since the information will not be private, we will use the following scheme to keep my messages secure.

1. All messages sent through the telegraph will be hidden using a secret key, which will be a small number. To decode the message, simply retain every letter that falls in a spot that is a multiple of that key. Start labelling spots at 0, which is a multiple of everything. Then, for example, the key 3 unlocks the message "herita" by only keeping the letters at spot 0 and 3 --> "herita" --> "hi".

2. To make it hard to steal the key, I will break the key into four shares, and stash them in different places.

3. You will need to find two shares and use an equation to put them together to discover the key. Each share is a pair of numbers (x,y). When you get two shares, you can use them to determine the y-intercept of the line that passes through those two points. This is the key!

You find a telegraph transmission along with some scraps of paper.

TtLhOqeGkrWPeEJ CAiUnshJ SnnTqoPLtbvhmBiOpnLugIX UZironLV tAtqbhwpeIe XRuIlnjtiKlvxqebcrwXshreHI uTtAuhuGaJHtil EsIMY bZfwnepjaAjrvr,rN VTbjzuEatwy jmtQBhbKauwtiM IgIgb qYsOihUzaEClTMlKU grnFToNXtay DakFnnXooZFwuD NlaHvlGQlpA UVmRByjG UQdpruWvtisydu,Pm nBoIsrBY JwsZWhhKafZlUOlWy qcfeWaoGivpllF adtAboqA DddZHoYf CWikdton.YG

(10,-217)

(2,-41)

(-2,47)

(-10,223)

What's the first message?

Eager to find out what Mary Lyon has written, you quickly find your CS mentor and they help write some functions to help you crack the code...

""" Mary Lyon has left us two coded messages with some cryptic instructions about

using any two secret shares to find the keys for decoding each.

You must print both decode both messages to receive her wisdom! """


secretMessage1 = "TtLhOqeGkrWPeEJ CAiUnshJ SnnTqoPLtbvhmBiOpnLugIX UZironLV tAtqbhwpeIe XRuIlnjtiKlvxqebcrwXshreHI uTtAuhuGaJHtil EsIMY bZfwnepjaAjrvr,rN VTbjzuEatwy jmtQBhbKauwtiM IgIgb qYsOihUzaEClTMlKU grnFToNXtay DakFnnXooZFwuD NlaHvlGQlpA UVmRByjG UQdpruWvtisydu,Pm nBoIsrBY JwsZWhhKafZlUOlWy qcfeWaoGivpllF adtAboqA DddZHoYf CWikdton.YG"


# secret shares for message 1's key:

# (10,-217)

# (2,-41)

# (-2,47)

# (-10,223)


def shamirsSecretDecoder( x1, y1, x2, y2 ):

""" Given two points (x1, y1) and (x2, y2), unlock a secret value by

finding the y-intercept of the line determined by these two points.

This is a 2-dimensional version of Shamir's secret sharing. """


# compute the slope and assign it to a variable named m

yDiff = y2 - y1 # compute the difference between the y-coordinates

xDiff = x2 - x1 # compute the difference between the x-coordinates

m = yDiff/xDiff # the slope is the y-coordinate difference over the x-coordinate difference


# compute the y-intercept of the line and assign it to a variable b

b = y1 - m*x1


return b


def keepLettersAtPositions( message, x ):

""" Keep every xth letter. Note this always keeps the first letter, as 0 is

considered a multiple of every number.

Examples:

if multiple is 2, this will keep every letter whose index is a multiple of x

keepLettersAtPositions( "Hrenlwlnos", 2 ) -> "Hello"

if multiple is 3, this will keep every third letter

keepLettersAtPositions( "MecHiBCse", 3 ) -> "MHC"

"""

cleanedMessage = "" # we will accumulate into this variable


# iterate over the valid indices in the message string

for i in range( len(message) ):

if i%x == 0: # if the index is a multiple of x

cleanedMessage += message[i] # keep the letter by adding it to the string

return cleanedMessage # return this accumulated message


def main():

# What should we do?


if __name__ == "__main__":

main()

Let's work through this together: https://app.codingrooms.com/w/VgCuWBLsLw2F

Next: Types