Skip to content

Instantly share code, notes, and snippets.

@ljbelenky
Last active July 13, 2021 23:06
Show Gist options
  • Save ljbelenky/845ceb92207ab3b8b69697538575e2f6 to your computer and use it in GitHub Desktop.
Save ljbelenky/845ceb92207ab3b8b69697538575e2f6 to your computer and use it in GitHub Desktop.
OOP Exercise: Combination Locks
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@skylarenglish
Copy link

skylarenglish commented Aug 7, 2020

My current function for deciding to open the lock after Land has submitted some digits looks something like this:

def open_lock(input_sequence):
return input == 1234

My partner Land keeps peeking at the lock combination, I can feel it! I just can't trust Land not to inspect the object or the source .py file and look at what the key I am comparing to is! But I have a plan. I would like to make the function that unlocks the lock have only content that I don't mind Land seeing. I know I can use the hash function (a builtin) to map an object to a number. I am thinking I could implement something like the code below. My implementation of these security features should not break any of Land's testing code and vice versa.

def open_lock(input):
return hash(str(input)+"you'll never find it Land!") == -7068180340459975452

#possible test code to show unit tests
print("Here is how we figure out what our hashed number should be: " + str(hash(str(1234)+"you'll never find it Land!")))
assert open_lock(1235) == False
assert open_lock(1234) == True

@ljbelenky
Copy link
Author

Don't forget to implement a repr.

@ljbelenky
Copy link
Author

Suggestions for further improvement:

  1. As Skylar suggested, hash the combination to prevent peeking
  2. Ensure the user can only enter the code one digit at a time.
  3. Implement checking to see if the digit entered is allowable (i.e, is a number and within the correct range). Determine how to deal with incorrect input (i.e, raise an error, use a default or coerced value, reject the input without an error, display an error message, etc)
  4. Expand the number of allowable digits from 0-9 to 0-n.
  5. Make it send you a text message if someone successfully opens your lock
  6. Have it send a text message if someone attempts to open the lock X number of times and ask you to enable additional attempts.

@ljbelenky
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment