# Homework 6

*Due by 11:59pm on Friday, 4/8*

## Instructions

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

**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. See Lab 0 for instructions on submitting
assignments.

**Using OK:** If you have any questions about using OK, please
refer to this guide.

**Readings:** You might find the following references
useful:

Our course uses a custom version of Scheme (which you will build for project 4) included in the starter ZIP archive. To start the interpreter, type

`python3 scheme`

. To exit the Scheme interpreter, type`(exit)`

.

### Question 1

Define the procedures `cadr`

and `caddr`

, which return the second
and third elements of a list, respectively:

```
(define (cddr s)
(cdr (cdr s)))
(define (cadr s)
'YOUR-CODE-HERE
nil
)
(define (caddr s)
'YOUR-CODE-HERE
nil
)
```

Use OK to unlock and test your code:

```
python3 ok -q cadr-caddr -u
python3 ok -q cadr-caddr
```

### Question 2

Implement a procedure `pow`

for raising the number `b`

to the power of integer
`n`

that runs in Θ(log n) time.

Hint:Using the built-in predicates`even?`

and`odd?`

, implement theRussian Peasants Algorithmfor exponentiation. This relies on the following observations:

- x
^{2n}= (x^{n})^{2}- x
^{2n+1}= x(x^{n})^{2}which together give you a simple iterative algorithm in Python:

`def pow(b, n): r = 1 while n > 0: if n % 2 == 1: r *= b b *= b n //= 2 return r`

Make it recursive to code it in Scheme.

```
(define (square x) (* x x))
(define (pow b n)
'YOUR-CODE-HERE
nil
)
```

Use OK to unlock and test your code:

```
python3 ok -q pow -u
python3 ok -q pow
```

### Question 3

Implement a function called `ordered?`

, which takes a list of numbers and
returns `True`

if the numbers are in nondescending order, and `False`

otherwise.

Hint: The built-in`null?`

function returns whether its argument is`nil`

.

```
(define (ordered? s)
'YOUR-CODE-HERE
nil
)
```

Use OK to unlock and test your code:

```
python3 ok -q ordered -u
python3 ok -q ordered
```

### Question 4

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)
'YOUR-CODE-HERE
'replace-this)
```

Use OK to unlock and test your code:

```
python3 ok -q no-repeats -u
python3 ok -q no-repeats
```

### Question 5

Implement the procedure `nodots`

, which takes a nested list of numbers that
may not be well-formed and returns a nested list with the same content and
structure, but which does not have any dots when displayed. Lists are not
well-formed if they do not properly terminate in a null list. In such cases,
the list will print with a dot before the final item to indicate that its
last two items are contained in a single pair. For example,

`(cons 1 (cons 2 3))`

would print as

`(1 2 . 3)`

for which `nodots`

should substitute

`(1 2 3)`

You may find the built-in

`null?`

and`pair?`

functions useful.

```
(define (nodots s)
'YOUR-CODE-HERE
nil
)
```

You can unlock and test using OK:

```
python3 ok -q nodots -u
python3 ok -q nodots
```

### Sets as Ordered Lists

The lecture on sets described one representation of a set using an ordered list, where the ordering was used to speed up search. The following few questions explore this idea, assuming a "set" is a Scheme list with no repeated elements that is already ordered from least to greatest.

### Question 6

Define `contains?`

, which returns whether a set `s`

contains value `v`

. The
Python implementation of this procedure is provided for your reference.

```
; Sets as sorted lists
(define (empty? s) (null? s))
(define (contains? s v)
(cond ((empty? s) false)
'YOUR-CODE-HERE
(else nil) ; replace this line
))
; Equivalent Python code, for your reference:
;
; def empty(s):
; return len(s) == 0
;
; def contains(s, v):
; if empty(s):
; return False
; elif s.first > v:
; return False
; elif s.first == v:
; return True
; else:
; return contains(s.rest, v)
```

You can unlock and test using OK:

```
python3 ok -q contains -u
python3 ok -q contains
```

### Question 7

Define `add`

, which takes a set `s`

and a value `v`

as arguments. It returns a
representation of a set containing the values in `s`

and the value `v`

. There
should be no repeated elements in the return value.

```
(define (add s v)
(cond ((empty? s) (list v))
'YOUR-CODE-HERE
(else nil) ; replace this line
))
```

You can unlock and test using OK:

```
python3 ok -q add -u
python3 ok -q add
```

### Question 8

Define `intersect`

, which returns a set containing only values that appear in
both sets `s`

and `t`

. Your implementation should run in linear time in the
length of the input sets. A Python implementation of this procedure is
provided for your reference.

Also, define `union`

, which returns a set containing all values that appear
in either set `s`

or `t`

.

```
(define (intersect s t)
(cond ((or (empty? s) (empty? t)) nil)
'YOUR-CODE-HERE
(else nil) ; replace this line
))
; Equivalent Python code, for your reference:
;
; def intersect(set1, set2):
; if empty(set1) or empty(set2):
; return Link.empty
; else:
; e1, e2 = set1.first, set2.first
; if e1 == e2:
; return Link(e1, intersect(set1.rest, set2.rest))
; elif e1 < e2:
; return intersect(set1.rest, set2)
; elif e2 < e1:
; return intersect(set1, set2.rest)
(define (union s t)
(cond ((empty? s) t)
((empty? t) s)
'YOUR-CODE-HERE
(else nil) ; replace this line
))
```

You can unlock and test using OK:

```
python3 ok -q intersect -u
python3 ok -q intersect
python3 ok -q union -u
python3 ok -q union
```

### Question 9

Another way to represent a set is to store the elements in a binary search tree. A binary search tree's branches are either empty or binary search trees. Also, the entry at the root of the tree must be greater than all entries in the left branch and less than all entries in the right branch.

The following procedures define a data abstraction for a binary search tree.

```
; A data abstraction for binary trees where nil represents the empty tree
(define (tree label left right) (list label left right))
(define (label t) (car t))
(define (left t) (cadr t))
(define (right t) (caddr t))
(define (empty? s) (null? s))
(define (leaf label) (tree label nil nil))
```

Define the procedure `in?`

, which returns whether a binary search tree `t`

contains an element `v`

. Your implementation should run in logarithmic time in
the number of entries in a balanced input tree. The Python implementation of
this procedure is provided for your reference.

```
(define (in? t v)
(cond ((empty? t) false)
'YOUR-CODE-HERE
(else nil)
))
; Equivalent Python code, for your reference:
;
; def contains(s, v):
; if s.is_empty:
; return False
; elif s.label == v:
; return True
; elif s.label < v:
; return contains(s.right, v)
; elif s.label > v:
; return contains(s.left, v)
```

You can unlock and test using OK:

```
python3 ok -q in -u
python3 ok -q in
```

### Question 10

Define `as-list`

, which takes a binary search tree and returns a sorted list
containing the entries of the tree. For an optional challenge, define the
procedure so that it runs in linear time in the length of the resulting list.
Converting to and from the sorted list representation in linear time allows
binary search trees to be intersected in linear time.

```
(define (as-list t)
'YOUR-CODE-HERE
(else nil)
)
```

You can unlock and test using OK:

```
python3 ok -q as-list -u
python3 ok -q as-list
```