CS200: Computer Science, Spring 2002

Problem Set 4: The Lorenz Cipher and the Postman's Computer
Selected Answers
Honor Code Reminder: If you are currently taking CS200, it is a violation of the course pledge to look at Problem Set answers and discussions from previous years. Please don't do it.
Question 1: Define the xor function that takes two bits as parameters and evaluates to 1 if exactly one of the parameters is 1 and evaluates to 0 otherwise. Your xor function should produce these evaluations:
> (xor 0 0)
0
> (xor 0 1)
1
> (xor 1 0)
1
> (xor 1 1)
0Rachel Dada had the simplest (best) answer:
(define (xor a b) (if (eq? a b) 0 1))
Question 2:
a. Write a function stringtobaudot that takes a string and transforms it into a list of Baudot codes.
b. Write the inverse function, baudottostring, that takes a list of lists of baudot codes and transforms it back into a string.The best answers used map, although it was reasonable to define this recursively also. Here's Jacques Fournier's answer:
(define (stringtobaudot string) (map chartobaudot (string>list string))) (define (baudottostring baudotlist) (list>string (map baudottochar baudotlist)))
Question 3: To rotate our wheels we will take the number at the front of the list and move it to the back. The first number in the list will represent the current position of the wheel. a. Define a function rotatewheel that takes one of the wheels as a parameter. It should return the wheel rotated once. For example, (rotatewheel (list 1 0 0 1 0)) should evaluate to (0 0 1 0 1). Although all the wheels in our simulated Lorenz cipher machine have five bits, your rotatewheel procedure should work for any length list.
b. Define a function rotatewheelby that takes a wheel as the first parameter and a number as the second. The function should return the wheel list rotated by the number passed in. For example, (rotatewheelby (list 1 0 0 1 0) 2) should evaluate to (0 1 0 1 0) and (rotatewheelby wheel 5) should evaluate to wheel.
Here's Spence Stockdate's answer:
Another approach is to remember ntimes from PS2. Here is Katie Winstanley's answer:(define (rotatewheel wheel) (append (cdr wheel) (list (car wheel)))) (define (rotatewheelby wheel n) (if (= n 0) wheel (rotatewheelby (rotatewheel wheel) ( n 1))))(define (rotatewheelby wheel n) ((ntimes rotatewheel n) wheel))
Question 4:
a. Define a function rotatewheellist that takes a list of wheels (like Kwheels) as a parameter and evaluates to a list of wheels where each of the wheels in the parameter list of wheels has rotated once. For example, (rotatewheellist Kwheels) should evaluate to
((1 0 1 0 1) (1 0 0 1 0) (0 0 1 0 1) (1 1 0 1 1) (0 0 0 1 1)).b. Define a function rotatewheellistby that takes a list of wheels and a number as parameters, and evaluates to a list of where where each of the wheels in the parameter list of wheels has rotated the number parameter times. For example, (rotatewheellistby Kwheels 5) should evaluate to the same list as Kwheels.
Here's Russ O'Reagan's answer:
Another approach is to use rotatewheelby, as was done by Jeff Taylor:(define (rotatewheellist wheellist) (map rotatewheel wheellist)) (define (rotatewheellistby wheellist n) ((ntimes rotatewheellist n) wheellist))(define (rotatewheellistby wheels n) (map (lambda (wheel) ((rotatewheelby wheel) n)) wheels))
Question 5: Define a function wheelencrypt that takes a Baudotencoded letter (a list of 5 bits) and a list of wheels as parameters. The function should xor each bit of the Baudot list with the first value of its respective wheel and return the resulting list. For example, (wheelencrypt (list 0 0 0 1 1) Kwheels) should produce (1 0 1 0 0). Jacques Fournier had the simplest answer:
(define (wheelencrypt baudotletter wheellist) (map xor baudotletter (map car wheellist)))
Question 6:
a. Define a function called dolorenz that takes a list of Baudot values, the K wheels, S wheels and M wheel. The function should encrypt the first Baudot code with the K and S wheels, then recursively encrypt the rest of the Baudot codes with the wheels rotated. The function should return the encrypted values in the form of a list of Baudot values.b. Define a function lorenz that takes four parameters: a string and three integers. The integers represent the starting positions of the three wheels, respectively. The function should call dolorenz with the string converted to Baudot and the wheels rotated to the correct starting positions. The function should return the ciphertext in the form of a string.
Here's Katie Winstanley's answer:
(define (dolorenz code kwheels swheels mwheel) (if (null? code) code (cons (wheelencrypt (wheelencrypt (car code) kwheels) swheels) (dolorenz (cdr code) (rotatewheellist kwheels) (if (= (car mwheel) 1) (rotatewheellist swheels) swheels) (rotatewheel mwheel))))) (define (lorenz string n1 n2 n3) (if (null? string) string (baudottostring (dolorenz (stringtobaudot string) (rotatewheellistby kwheels n1) (rotatewheellistby swheels n2) (rotatewheelby mwheels n3)))))
Question 7: Define a function called solvelorenz that takes a ciphertext and evaluates lorenz on the ciphertext for all 125 possible starting positions. It may be helpful to use the function printf that will print out a string to the interactions window. Here's Grace Deng's answer:
Here's Jeff Taylor's answer:(define (solvelorenz string position1 position2 position3) (if (<= position1 5) (if (<= position2 5) (if (<= position3 5) (stringappend (lorenz string position1 position2 position3) (solvelorenz string position1 position2 (+ 1 position 3))) (solvelorenz string position1 (+ 1 position2) 1)) (solvelorenz string (+ 1 position1) 1 1)) ""))The encrypted message: AT THE TIME, I HAD NO THOUGHT OR KNOWLEDGE OF COMPUTERS IN THE MODERN SENSE, AND HAD NEVER HEARD THE TERM USED EXCEPT TO DESCRIBE SOMEBODY WHO DID CALCULATIONS.  THOMAS FLOWERS.(define (solvelorenz ctext) (map (lambda (kstep) (map (lambda (sstep) (map (lambda (mstep) (printf (lorenz ctext kstep sstep mstep))) (intsto 5))) (intsto 5))) (intsto 5)))
University of Virginia Department of Computer Science CS 200: Computer Science 
David Evans evans@virginia.edu Using these Materials 