Homework Nine

Loops

Comp210

Due: 97.Apr.fools (Wed.) in class

You don't need to write templates if you don't want, but (as always) every function must have a contract, and (if it's not obvious from the name) a short description of what it does. A large component of the grade is based not so much on correctness, as on having a clearly organized, well thought out, straightforward solution. If you notice yourself repeating similar chunks of code, abstract over it!
  1. In the kingdom of Tuhundredtenia, the jail system is extensive, with 150 cells all in a row. Each cell's door has a key, which when turned toggles the door's lock. That is, if the door was unlocked, then turning the key locks it; if locked, then turning unlocks it.

    The Royal Jailer has a curious habit: Every evening, he turns the key in every door---numbers 1 through 150. Then, he goes back and turns the key in every other door---numbers 2, 4, 6, ..., 150. Then, he goes back and turns the key in every third door---numbers 3, 6, 9, ..., 150. Then, he turns the key in every fourth door---numbers 4, 8, 12, ..., 148. And so on, until he finishes by turning the key in every 150th door (just number 150). After that, he falls asleep, and anybody in an unlocked cell can escape during the night. (At the start of the evening, all cells are locked.)

    You and your friends have been framed, and falsely convicted of Meandering with Intent to Jay-walk, and are scheduled to be executed at dawn. Fortunately, each of you can name which cell to be locked away in. Which cell numbers do you-all ask for?

    Hints:

    To think about: Once your program tells you the answer, can you augment this with an explanation for the particular answer? (This is a great example showing the difference of knowing an answer, and understanding the answer.)

  2. We revisit the password problem from an earlier homework. Your task is to allow modification of the password in an object and check for possible security breakins.

    Write a function make-passwd-object that takes an initial password and returns a password object. This password object is a function that takes two inputs, an action (a symbol) and a list. There are two possible actions.

    The first possible action is 'check-passwd. In this case the list argument contains only one item, the password to be checked. If this password agrees with the current password for the object, then the result of the function call is #t (true). If the password is not correct, the object returns #f (false).

    The second possible action is 'set-passwd. In this case the list argument contains two items, the old password and a new password. If the old password agrees with the current password for the object, then the current password is updated to be the new password. Otherwise, the current password for the object remains unchanged. The return value is a boolean indicating whether or not the password was successfully changed.

    To prohibit repeated attempts to guess the current password, the object should keep track of failed attempts to check a password or update a password. If there are 4 or more consecutive failed attempts, the object should lock the password object, causing the action invoked via 'check-passwd or 'set-passwd to always fail.

    Hint: Maintain the current password and number of consecutive failed attempts as local variables.

    Example:

    (define my-passwd (make-passwd-object 'secret-handshake))
    (define your-passwd (make-passwd-object 'open-sesame))
     
    (my-passwd 'check-passwd (list 'is-this-it?))                    ;  = #f
    (my-passwd 'check-passwd (list 'secret-handshake))               ;  = #t
    (my-passwd 'set-passwd   (list 'I-forgot 'treasure-map))         ;  = #f
    (my-passwd 'check-passwd (list 'treasure-map))                   ;  = #f
    (my-passwd 'check-passwd (list 'secret-handshake))               ;  = #t
    (my-passwd 'set-passwd   (list 'secret-handshake 'PROFS-email))  ;  = #t
    (my-passwd 'check-passwd (list 'PROFS-email))                    ;  = #t
    (my-passwd 'check-passwd (list 'clumsy-breakin-attempt))         ;  = #f
    (my-passwd 'check-passwd (list 'clumsy-breakin-attempt))         ;  = #f
    (my-passwd 'check-passwd (list 'PROFS-email))                    ;  = #t
    (my-passwd 'check-passwd (list 'another-clumsy-breakin-attempt)) ;  = #f
    (my-passwd 'check-passwd (list 'if-at-first)                     ;  = #f
    (my-passwd 'check-passwd (list 'i-dont-succeed))                 ;  = #f
    (my-passwd 'check-passwd (list 'persistence-gets-me-arrested))   ;  = #f
    (my-passwd 'check-passwd (list 'PROFS-email))                    ;  = #f
    (my-passwd 'set-passwd   (list 'PROFS-email 'ollie))             ;  = #f
    (your-passwd 'check-passwd (list 'open-sesame)                   ;  = #t
    

  3. Using the JAM inspector as seen in lab,
    1. Enter the following values into memory locations 101 through 106:
      17039, 103460, 159, 54820, 103870, 0. Also, enter any value you like into memory location 170.
    2. Examine the contents of the registers.
    3. Use the inspector to start the Jam machine executing the program starting at location 101 (make sure you set the PC register).
    4. Re-examine the contents of the registers, and memory location 170. Submit the before-and-after contents of any locations whose value has changed.
    5. What is the machine instruction corresponding to the assembly command (add 8 4 5)? How about (ldi 5 777)?
      (Use the JAM reference sheet.)
    We will learn more later, about exactly what each command does, and how to write more interesting programs.

¹ While this skip-a-location approach isn't usually needed for vectors, the numeric properties of the jailer's walk make it convenient in this case. To think about: What is the extra space overhead incurred by this approach? Is it significant? [back]