CS 61A: Quiz 3

Due by 11:59pm on Thursday, 4/9


Download quiz03.zip. Inside the archive, you will find a file called quiz03.scm, along with a copy of the OK autograder.

Complete the quiz and submit it before 11:59pm on Thursday, 4/9. You must work alone, but you may talk to the course staff (see Asking Questions below). You may use any course materials, including an interpreter, course videos, slides, and readings. Please do not discuss these specific questions with your classmates, and do not scour the web for answers or post your answers online.

Your submission will be graded automatically for correctness. Your implementations do not need to be efficient, as long as they are correct. We will apply additional correctness tests as well as the ones provided. Passing these tests does not guarantee a perfect score.

Asking Questions: If you believe you need clarification on a question, make a private post on Piazza. Please do not post publicly about the quiz contents. If the staff discovers a problem with the quiz or needs to clarify a question, we will email the class via Piazza. You can also come to office hours to ask questions about the quiz or any other course material, but no answers or hints will be provided in office hours.

Submission: When you are done, submit with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be scored.

Using OK

The ok program helps you test your code and track your progress. The first time you run the autograder, you will be asked to log in with your @berkeley.edu account using your web browser. Please do so. Each time you run ok, it will back up your work and progress on our servers. You can run all the doctests with the following command:

python3 ok

To test a specific question, use the -q option with the name of the function:

python3 ok -q <function>

By default, only tests that fail will appear. If you want to see how you did on all tests, you can use the -v option:

python3 ok -v

If you do not want to send your progress to our server or you have any problems logging in, add the --local flag to block all communication:

python3 ok --local

When you are ready to submit, run ok with the --submit option:

python3 ok --submit

Readings: You might find the following references useful:

Table of Contents

The Scheme interpreter is included in the starter ZIP archive. To run the Scheme interpreter, use the following command:

python3 scheme

To load a file (such as quiz03.scm), use

python3 scheme -load quiz03.scm

You can also use our online Scheme interpreter.

You may use the map and filter procedures defined below in your solutions.

(define (map f s)
  ; List the result of applying f to each element in s.
  (if (null? s) s
    (cons (f (car s)) (map f (cdr s)))))

(define (filter f s)
  ; List the elements of s for which f returns a true value.
  (if (null? s) s
    (let ((rest (filter f (cdr s))))
      (if (f (car s)) (cons (car s) rest) rest))))

Question 1

Implement no-repeats, which takes a list of numbers s as input and returns a list that has all of the unique elements of s in the order that they first appear, but no repeats. For example, (no-repeats (list 5 4 5 4 2 2)) evaluates to (5 4 2).

Hints: To test if two numbers are equal, use the = procedure. To test if two numbers are not equal, use the not procedure in combination with =.

(define (no-repeats s)

For more examples, unlock the tests associated with this question.

python3 ok -q no-repeats -u

Question 2

Implement how-many-dots, which takes in a nested scheme list s and returns the number of dots that appear when it is displayed by the interpreter (not counting decimal points). You may assume that s is a nested list that contains only numbers.

Hints: A dot appears when the second element of a pair is not a well formed list. The procedures pair?, null?, and number? test whether a value is a pair, nil, or a number, respectively.

(define (how-many-dots s)

For more examples, unlock the tests associated with this question.

python3 ok -q how-many-dots -u