next up previous
Next: Sorting lists Up: No Title Previous: Abstract Data Types

Caesar ciphers

We want to write functions that encode words and sentences using the Caesar cipher. In the Caesar cipher a word is encoded by rotating the letters in the alphabet a given number of times. For example if we rotate by one letter, then CAESAR becomes DBFTBS. If we rotate the word ZOO by four letters it becomes DSS (note that rotating Z by one letter gets us back to A).

You may assume that there is a function rot which takes a letter and rotates it by one letter, i.e. (rot 'A) tex2html_wrap_inline266 B, (rot 'B) tex2html_wrap_inline266 C, etc, finally (rot 'Z) tex2html_wrap_inline266 A.

(a)
[5 points] Write a function caesar-word which takes a word and a number and encodes the word by rotating each letter in the word by the given number, e.g. (caesar-word 'CAESAR 1) tex2html_wrap_inline266 DBFTBS and (caesar-word 'ZOO 4) tex2html_wrap_inline266 DSS.
(b)
[5 points] Write a function caesar which takes a sentence and a number and encodes each word according to the Caesar cipher using the function caesar-word. For example: (caesar '(CAESAR IS HUNGRY) 1) tex2html_wrap_inline266 (DBFTBS JT IVOHSZ). (Hint: use every and lambda). (Note: you can solve part b even without having solved part a just assuming caesar-word works as described above).

(Please turn over)

(c)
[2 points] Write a function caesar-decode that takes an encoded sentence and the number it was encoded with and returns the original sentence, e.g.\ (caesar-decode '(DBFTBS JT IVOHSZ) 1) tex2html_wrap_inline266 (CAESAR IS HUNGRY). (Hint: there is a solution in two lines.)



Behfar Bastani-Booshehri
Wed Dec 9 10:04:14 CST 1998