CS8, 10F, H07, due Fri Lab 10.22—Miller/Ranum 3.4-3.6 (thru p. 93-108).—Total points: ?

Available online as http://www.cs.ucsb.edu/~pconrad/cs8/10F/homework/H07—printable PDF

Name: (4 pts)   Umail Address: (4 pts)   @umail.ucsb.edu
Lab Section (2 pts)—circle one:  9am   10am   11am   noon   unknown   crashing 
You may collaborate with at most ONE other person on this homework assignment. If you do, please enter his/her name here:  
  (He/she should also enter your name on her/his assignment.)

This assignment is due in Lab on Friday, 10.22.
It may ONLY be submitted in Lab, in ESB1003 (Cooper Lab) on Friday.

You must come IN PERSON to turn it in during your assigned Lab section.


Check this link for chapter 3 typos: j.mp/ch3typos-ppic

Read pages 93-108 in Chapter 3—sections 3.4, 3.5, and 3.6. Then answer these questions. You may want to have the code from these pages available—you can find it at the links below.

  1. Look at program listing 3.2 on p. 95.
    In plain english:

    1. (4 pts) What does this function "consume", i.e. take as an parameter to a function call?






    2. (4 pts) What does this function "produce", i.e. return as the result of a function call?








  2. In your book, the lines of listing 3.2 on p. 95 are numbered.

    On each of the following lines, there is a + sign. Does the + sign on that line stand for addition or concatenation?

    For each, circle the correct answer.

    1. (3 pt) Line 7    addition   concatenation

    2. (3 pt) Line 9    addition   concatenation

    3. (3 pt) Line 10    addition   concatenation

    4. (3 pt) Line 11    addition   concatenation

    Please turn over for more...

    ...continued from other side

  3. (4 pts) On p. 98, in listing 3.3, we see this line at line 2

    halfLength = len(cipherText) // 2

    We see that the value of halfLength is then used on lines 3 and 4, in a "slice" operation. (Slice operations are described earlier in Chapter 3, and in the text on pages 94-98).

    My question to you: in plain english, what is the reason for using the // operator here?

    Hints:
    It will definitely help you to read through the text on pages 94-98 that describes the encryption/decryption algorithm. However the answer to the question is not just "waiting for you in the text"—you'll have to do something additional thinking.

    Here are some clues to guide you:

    • Think about what happens when we encrypt a message with four characters—that is, first go back to listing 3.2, and run it on, for example, "pear".
    • Think about what happens when we encrypt a message with three characters, e.g. "dog"
    • Then think about what will happen when we want to decrypt either of those messages—particularly what happens on line 2, and then lines 3 and 4 of listing 3.3




  4. Section 3.6 discusses the problem of generating a key—and the first approach is to create a scramble of the letters a-z. The text says that the problem is broken up into two subproblems—the first of which is implemented by the function removeChar.

    1. (3 pts) Given the definition of removeChar in Section 3.6 what is the value returned by:
      removeChar("Python",3)


    2. (3 pts) Listing 3.7 on p. 105 contains an example of the accumulator pattern, similar to the accumulator pattern described in Section 2.5.1 (pp 54-55)—except here instead of accumulating the sum of numbers, we are accumulating a string.

      What line of code in Listing 3.7 corresponds to the line of code acc=0 from Session 2.5 on p. 55?


    3. (3 pts) What line of code in Listing 3.7 corresponds to the line of code acc = acc + x from Session 2.5 on p. 55?


    4. (3 pts) After Listing 3.7, the text goes on to describe a different approach to generating a key based on a password. The key is still a scramble of the letters a-z, but is derived in a different way. The book gives the example of a key based on "topsecret" as "topsecruvwxyzabdfghijklmnq".

      Using this same technique, what key would correspond to the password "embarcadero"?


    5. (2 pts) Listing 3.9 contains a function called removeMatches. If we call removeMatches with the call:
      removeMatches("isla","vista"), what will be returned?


    6. (2 pts) If we reverse that call, and write removeMatches("vista","isla") what will be returned?


End of H07