Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sam wants to leave a secret message for Kim. They share an office on different s

ID: 3661788 • Letter: S

Question

Sam wants to leave a secret message for Kim. They share an office on different shifts in the data backup and recovery division of the Umbrella Corporation. They will leave a post-it note in the top drawer of a filing cabinet they share if they want to give each other a secret message. Each can check when they arrive on their shift, however Malfoy can snoop on them and read the message that is left for each because the top drawer of the filling cabinet is unlocked.

They decide to use a locking solution on the filing cabinet drawer. However, they only want one key each, and NOT the shared keys for the lock. Kim says why don’t they use two locks, but she cannot figure out how to lock the drawer without a hidden key to her lock and Sam’s lock put somewhere that Malfoy cannot find. If they drawer on the filing cabinet can accommodate two locks, how can this be solved?

Write an algorithm that solves the problem with Kim and Sam only having one key to their respective lock, and there are no keys to the locks hidden from Malfoy. The algorithm should also solve the problem of communicating between Kim and Sam that one of them left a message for the other by virtue of using the locks and the single filing cabinet drawer. HINT: THE MESSAGE CAN STAY IN THE LOCKED DRAWER FOR MULTIPLE DAYS.

Explanation / Answer

Good wishes,

Here we have two locks L1 and L2, L1 belongs to sam and L2 belongs to kim

we have two keys one for each lock K1 and K2, K1 belongs to lock L1 and K2 belongs to lock L2

Now let us maintain them in pairs like { (L1,K1) , (L2,K2) } and store it in LK

Thus LK= { (L1,K1) , (L2,K2) }

Now maintain a stack called 'msg' with capacity one, this is represent the single filling cabinet drawer. A variable 'top' is used to indicate the top position of stack 'msg'.

Initially top = -1.

Now the algorithm:

step 1: start

step 2: Accept key from the user and place it in 'key'

step 3: if key is inserted in L1, then check

if LK(K1) = key

if true goto step 5

else goto step 4

else if key is inserted in L2, then check

if LK(K2) = key

if true goto step 5

else goto step 4

step 4: print message "lock not opened " and goto step 7

step 5: if top = -1, then

print "No new msg, do you want to insert (Y/N)"

if 'Y' is inputed , then goto step 6

else goto step 7

else if top = 0, then

print the message in msg[top] and do top = top -1

print " do you want to insert (Y/N)"

if 'Y' is inputed , then goto step 6

else goto step 7

step 6: Accept message from user and insert it into msg[top]

and do top =top +1

step 7: stop

Explaination:

Initially when the user puts a key into a lock, I accepted it in step 2

Then in step 3 , i check whether the lock key pair matches the one I stored in LK, If yes then carried onto step 5 else to step 4

In step 4 , when the user reached this step means that either key is inserted into the wrong key or key itself is not valid. In either of the case a message "lock not opened " is prompted without telling the cause of it for security reasons and simplicity.

In step 5, I am checking to see if the top value is 0 or -1,

if top=-1

it means that no msg is there in the stack 'msg' and hence asking the user if he/she wants to enter one.

if the user wants to enter , we goto step 6 else to step 7

if top=0

it means that there is message left by the other person and hence the message is display/conveyed to the user and stack is made empty by doing top = top -1, hence it becomes top =0.

Now the user is asked if he/she wants to enter a message

if the user wants to enter , we goto step 6 else to step 7

In step 6, A message is accepted from the user and placed in stack msg and top is incremented to 1

Step 7 stops the process.

Hope this is clear.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote