Skip to main content

# Exam 1: Starting Scheme

Assigned: Wednesday, 13 September 2017

Prologue Due: Friday, 15 September 2017 by 10:30pm

Exam Due: Tuesday, 19 September 2017 by 10:30pm

Cover Sheet Due: Wednesday, 20 September 2017 by the start of class

Epilogue Due: Wednesday, 20 September 2017 by 10:30pm

Please read the exam procedures page for policies, turn-in procedures, and grading details. If you have any questions about the exam, check the Q&A at the bottom of this page. We will generally spend some time in class every day on questions and answers while the exam is in progress.

While the exam is out, please check back periodically to see if we have reported any new errata.

Complete the exam using the exam01.rkt starter source code. Please rename this file to 000000.rkt, but replace 000000 with your generated random number.

## Prologue

The prologue for this examination will be via email, which you should send to your instructor. Your message should be titled CSC 151.01: Exam 1 Prologue (your name).

1. For each problem, please include a short note about something that will help you solve the problem. Mostly, we want to see some evidence that you’ve thought about the problem. You might note some similar procedures you’ve written or problems you’ve solved in the past (e.g., in a lab or on a homework assignment). You might note procedures that you expect to use. You might sketch an algorithm. You might pose a question to yourself. (We won’t necessarily read this in a timely fashion, so if you have questions for your instructor, you should ask by email or in person.)

If, when looking at a problem, you think you already know the answer, you can feel free to write something short like “solved” or “trivial”.

2. Which of those six problems do you expect to be the most difficult for you to solve? Why?

3. Conclude by answering the question What is an approach that you expect will help you be successful on this exam? For example, you might suggest that you will work thirty minutes on the exam each day, or work on the exam at 7pm each day, when your brain is most able to process information.

## Epilogue

The epilogue for this examination will also be via email, which you should send to your instructor. Your message should be titled CSC 151.01: Exam 1 Epilogue (your name). Include answers to the following questions.

What was the most difficult part of the exam?

What made that part difficult?

What are two things you can do to be more successful on the next exam?

## Problem 1: Adding three

Topics: documentation, defining procedures with compose, defining procedures with section, defining procedures with lambda

We have now learned three different ways to write procedures.

1. We can use function composition.

2. We can use sectioning.

3. We can use lambda.

Consider a procedure, add3, that takes one parameter, a number, and adds three to that number.

> (add3 1)
4
> (add3 7.5)
10.5
> (add3 -1/2)
2 1/2
> (add3 4+5i)
7+5i


a. Write the 6P-style documentation for add3.

b. Implement add3 using function composition. To avoid name overlap, you can call this add3b. You may not use sectioning, lambda, or other procedures you define in implementing add3b.

c. Implement add3 using sectioning. To avoid name overlap, you can call this add3c. You may not use composition, lambda, or other procedures you define in implementing add3c.

d. Implement add3 using lambda. To avoid name overlap, you can call this add3d. You may not use sectioning, composition, or other procedures you define in implementing add3d.

## Problem 2: Computing sublists

Topics: lists, take, drop

In many situations, such as in the lab on tables, we find that we want to extract a sublist of another list: All the entries starting at position s and ending right before position t. Document and write a procedure, sublist that takes three inputs — a list, a starting position, and an ending position — and returns the sublist that starts at the starting position and ends right before the ending position.

> (define words (list "alpha" "beta" "gamma" "delta" "epsilon" "phi"))
> (sublist words 1 4)
'("beta" "gamma" "delta")


As the example suggests, you should keep the elements in the same order.

Note: DrRacket may have a built-in sublist command. You may not use it or any similar procedure. You should rely on the procedures you’ve already learned.

## Problem 3: Whatzitdo

Topics: Map, Compose and Section, Lists, Characters and Strings

Some time ago, a student who we will call “Gle” came up with something like the following as an alternative to an existing procedure.

(define f (lambda (a b c) (list->string (map1 (o (section string-ref a <>) (section + b <>)) (iota (- c b))))))


Figure out what the procedure does and then do the following.

a. Rename the procedure and the parameters so that their type and purpose is clear.

b. Reformat the code.

c. Document the updated procedure.

Hints

• Figure out what type a, b, and c are from their context.
• Think about the individual parts.
• Think about the input and output types of list->string.

## Problem 4: Preparing to remove odd values

Topics: Filtering, Sorting, Testing, Documentation

We’ve seen that filtering is one of the core processes we use in extracting meaning from data. For example, we’ve seen how to remove values outside of a range (or inside a range, for that matter).

Suppose we want to remove all the odd numbers from a list of exact integers.

Before writing such a procedure, we should, of course, write the documentation and some tests.

a. Write 6P-style documentation for a procedure, remove-odds, that removes all odd numbers in a list of integers. (The odd? predicate lets you test if something is odd.) You may choose whether or not remove-odd needs to preserve order, but we suggest that it need not preserve order. (That does complicate the documentation and tests slightly, but it’s worth it for the freedom.)

b. Write a comprehensive test suite for remove-odds. Since you’ll need a procedure to test, start with the following incredibly incorrect version.

(define remove-odds
(lambda (lst)
null))


Here’s the start of a test suite.

(define tests-remove-odds
(test-suite
"Tests of the remove-odds procedure"
(test-case
"Empty lists"
(check-equal? (remove-odds (list)) (list)))
(test-case
"Non-empty lists with no odd numbers"
(check-equal? (sort (remove-odds (list 2 10 8 6 4)) <)
(list 2 4 6 8 10)
"A few small even integers"))))


Note that we accommodated the “we don’t know the order” issue by sorting the result of remove-odds and comparing it to a sorted list.

## Problem 5: Removing odd values

Topics: Filtering, Sorting, Testing

We’ve seen a variety of techniques for removing negative numbers from a list of numbers. One of the most promising (as long as we don’t care about ordering) is to add a 0 to the front of the list, sort the list, find the index of the 0, and then drop everything up to that 0. You can, for example, find that approach in the lab on unit testing.

Can we use the same technique to eliminate the odd integers in a list of integers? Surprisingly, yes. How? It requires that we devise a different way of comparing numbers. As you’ve seen, the sort routine will order numbers “any” way you like, as long as there’s a way to say whether or not one number comes before another. When we sort a list of numbers with <, we get them ordered from smallest to largest. When we sort a list of numbers with >, we get them ordered from largest to smallest.

But we can certainly choose others. For example, here’s how we might compare two numbers according to their absolute value.

;;; Procedure:
;;;   abs<?
;;; Parameters:
;;;   num1, a real number
;;;   num2, a real number
;;; Purpose:
;;;   Compare the absolute value of num1 to the absolute value of num2
;;; Produces:
;;;   less?, a truth value
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   * if (abs num1) < (abs num2) then less? is #t.
;;;   * otherwise, less? is #f.
(define abs<?
(lambda (num1 num2)
(< (abs num1) (abs num2))))


We can then use this to sort a list of numbers by their absolute value.

> (sort (list 42 1 -5 3 18 -0.5 -11 22 -4) abs<?)
'(-0.5 1 3 -4 -5 -11 18 22 42)


We could also write

> (sort (list 42 1 -5 3 18 -0.5 -11 22 -4) (lambda (a b) (< (abs a) (abs b))))
'(-0.5 1 3 -4 -5 -11 18 22 42)


How does that help us remove all the odd numbers from a list? We can write a comparator that says that any odd number is less than any other number.

> (sort (iota 10) (lambda (a b) ___))
'(9 7 5 3 1 0 2 4 6 8)
> (sort (list 1 2 5 1 2 4 3 -1 -11 12 14 -2) (lambda (a b) ___))
'(-11 -1 3 1 5 1 2 2 4 12 14 -2)


Implement remove-odds using that technique as well as the ideas from remove-negatives.

Note: If the lambda expression in those examples is confusing, you could write a separate procedure.

;;; Procedure:
;;;   odd-first?
;;; Parameters:
;;;   a, an integer
;;;   b, an integer
;;; Purpose:
;;;   Determine if a comes before b according to the metric
;;;   "odd numbers come before even numbers.
;;; Produces:
;;;   first?, a Boolean value
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   * If a is odd, first? is #t
;;;   * Otherwise, first? is #f
;;; Philosophy:
;;;   Used to order lists of integers to put odd numbers first.
(define odd-first?
(lambda (a b)
#t)) ; STUB

> (sort (iota 10) odd-first?)
'(9 7 5 3 1 0 2 4 6 8)
> (sort (list 1 2 5 1 2 4 3 -1 -11 12 14 -2) odd-first?)
'(-11 -1 3 1 5 1 2 2 4 12 14 -2)


Note: You may not use the filter procedure to implement remove-odds.

## Problem 6: Reporting on teaching responsibilities

Topics: Compound data, Strings

As you’ve seen in your initial explorations with compound data, we sometimes want to write instructions to transform an entry from one form to another. Suppose we have information about the teaching responsibilities of Grinnell’s CS faculty in Fall 2017.

(define teaching-fall-2017
(list
(list "Curtsinger"
"Charlie"
"Professor"
(list "TUT-100" "CSC-211"))
(list "Klinge"
"Titus"
"Professor"
(list "CSC-151" "CSC-341" "CSC-395"))
(list "Osera"
"Peter-Michael"
"Professor"
(list "TUT-100" "CSC-207" "MAT-208"))
(list "Rebelsky"
"Samuel"
"Campus Curmudgeon"
(list "CSC-151" "CSC-301" "CSC-321" "CSC-322"))
(list "Vostinar"
"Anya"
"Professor"
(list "CSC-207" "CSC-301"))
(list "Weinman"
"Jerod"
"Department Chair"
(list "CSC-161"))))


Write, but do not document, a procedure, describe-teaching, that takes one of these entries as input and generates a string of the form

"TITLE FIRST LAST is teaching COURSE AND COURSE"


For example,

> (describe-teaching (list "Vostinar" "Anya" "Professor" (list "CSC-207" "CSC-301")))
"Professor Anya Vostinar is teaching CSC-207 and CSC-301"


We can also map describe-teaching on to the whole list to get a list of descriptions.

> (map describe-teaching teaching-fall-2017)
'("Professor Charlie Curtsinger is teaching TUT-100 and CSC-211"
"Professor Titus Klinge is teaching CSC-151 and CSC-341 and CSC-395"
"Professor Peter-Michael Osera is teaching TUT-100 and CSC-207 and MAT-208"
"Campus Curmudgeon Samuel Rebelsky is teaching CSC-151 and CSC-301 and CSC-321 and CSC-322"
"Professor Anya Vostinar is teaching CSC-207 and CSC-301"
"Department Chair Jerod Weinman is teaching CSC-161")


## Questions and answers

We will post answers to questions of general interest here while the exam is in progress. Please check here before emailing questions!

### General questions and answers

Are we allowed to talk to others about general issues from the first
nine days of class, such as the syntax of section?
No. We’ve had too many problems with “slippery slope” issues.
Questions should go to the instructors.
What is a general question?
A question that is about the exam in general, not a particular problem.
Do all the sections have the same exam?
Yes.
Can we still invoke the “There’s more to life” clause if we spend more than five hours on the exam?
Yes. However, we really do recommend that you stop at five hours unless you are very close to finishing. It’s not worth your time or stress to spend more effort on the exam. It is, however, worth your time to come talk to us, and perhaps to get a mentor or more help (not on this exam, but on the class). There’s likely some concept you’re missing, and we can help figure that out.
What do you mean by “implement?”
Write a procedure or procedures that accomplish the given task.
Do we have to make our code concise?
You should strive for readable and correct code. If you can make it concise, that’s a plus, but concision is secondary to readability and correctness. Long or muddled code is likely to lose points, even if it is correct.
Much of your sample 6P-style documentation has incomplete sentences. Can we follow that model? That is, can we use incomplete sentences in our 6P-style documentation?
Yes, you can use incomplete sentences in 6P-style documentation.
You tell us to start the exam early, but then you add corrections and questions and answers. Isn’t that contradictory? Aren’t we better off waiting until you’ve answered the questions and corrected any errors?
We think you’re better able to get your questions answered early if you start early. Later questions will generally receive a response of “See the notes on the exam.”
How do we know what our random number is?
You should have received instructions on how to generate your random number on the day the exam was distributed. If you don’t have a number, ask your professor for one before submitting your exam.
To show we’ve tested the code informally, would you just like us to just post the inputs we used to test the procedure? If so, how should we list those?
Copy and paste the interactions pane into the appropriate place in the definitions pane. Select the text. Under the Racket menu, use “Comment out with semicolons.”
Should we include examples and, if so, how do we include them?
You should certainly include examples. We would recommend that you copy and paste them from the interactions pane to right below the problem in the definitions pane, and then comment them out with semicolons. Select and then choose “Comment out with semicolons” from the “Racket” menu. Do not use “Comment out with a box!”
Should we cite our partner from a past lab or assignment if we use code from a past lab or assignment?
You should cite both yourself and your partner, although you should do so as anonymously as possible. For example “Ideas taken from the solution to problem 7 on assignment 2 written by student 641321 and partner.”
If we write a broken procedure and replace it later, should we keep the previous one?
Yes! This will help us give you partial credit if your final procedure isn’t quite right. But make sure to comment out the old one with semicolons and perhaps add a note or two as to what you were trying.
Do I need to document helper procedures?
You must document helpers using the 4Ps.
Can I use procedures that we have not covered in class? [Added ca. 14:30, 2017-09-16]
Probably not. Ask about individual procedures if you’re not sure.
Can I use list-ref? I’d swear we’ve used it, but I can’t find where. [Added ca. 17:00, 2017-09-16]
You may certainly use list-ref.
I discovered a way to define procedures without using lambda that looks like (define (proc params) body). Can I use that? [Added ca. 14:30, 2017-09-16]
No.
Is extra credit available on this exam? [Added ca. 14:30, 2017-09-16]
No explicit extra credit is available. However, we may find that you’ve come up with such wonderfl answers that we cannot help but give you
extra credit. (Don’t laugh. It happens almost every time.)
DrRacket crashed and ate my exam. What do I do? [Added ca. 14:30, 2017-09-16]
Send your instructor the exam file and any backups that DrRacket seems to have made. (Those tend to have a similar file name, but with pound signs or tildes added at the front or back.)
How do I know when you’ve added a new question or answer? [Added ca. 14:30, 2017-09-16]
We try to add a date stamp to the questions and answers.
Should we cite readings from the CSC 151 Web site? If so, how much information should we give. [Added ca. 14:30, 2017-09-16]
Yes. The title and URL should suffice.
Can I use DrRacket to test my work? [Added ca. 14:30, 2017-09-16]
Yes. We expect that you will be doing all of your work in DrRacket and checking your answers as you go.
Can I use some other websites (different from ours websites) to solve problems in the exam? [Added ca. 14:30, 2017-09-16]
As the exam policies page states, “This examination is open book, open notes, open mind, open computer, open Web.” Note that it also states “ If you find ideas in a book or on the Web, be sure to cite them appropriately.”
Do we have to cite the exam itself? [Added ca. 16:30, 2017-09-16]
No.
Can I use ideas from the reading/lab on Boolean values? [Added ca. 9:00 2017-09-18]
Yes.
How do I generate a random six digit number? [Added ca. 9:00 2017-09-18]
(random 1000000)
How do I submit my exam? [Added ca. 9:00 2017-09-18]
Name it 012345.rkt (substituting your random number)
Send it to your instructor, not the grader, as an attachment in an email message entitled “CSC 151 Exam 1” (or something similar)
Can I use the procedure string-length? [Added ca. 9:00 2017-09-18]
Sure. I don’t know where it’s useful, but you can certainly use it.
How should we write our tests and examples? [Added ca. 9:00 2017-09-18]
Tests - You know this already.
Examples: Do what you do when taking notes on labs. (1) Type a sample input in the interactions pane. (2) Copy the input and output to the definitions pane. (3) Comment it out.

### Time logs

What should I record for “Elapsed” in the time log? [Added ca. 14:30, 2017-09-16]
The difference between the time your finished and started the entry. (E.g., 30 min.)
What should I record for “Activity” in the time log? [Added ca. 14:30, 2017-09-16]
What you spent the time on. For example, you might have been writing test cases, or failed solutions, or ….
Can you provide a sample? [Added ca. 14:30, 2017-09-16]
Sure.
; Time Log:
;   Date        Start   Finish  Elapsed Activity
;   2017-09-15  18:00   18:20   20 min  Wrote four tests
;   2017-09-15  20:30   21:00   30 min  Three non-working versions
;                                       (marked as a, b, and c below).
;                                       Decided to sleep on it.
;   2017-09-16  08:00   08:10   10 min  Woke up with an idea.  Coded it.
;                                       it seems to work
;   2017-09-16  19:00   19:10   10 min  A few more tests just to make
;                                       sure.  Done!


### Problem 1

Should I worry that I’m getting strange results like (add3b -8.3) => -5.300000000000001? [Added ca. 14:30, 2017-09-16]
Nope. That’s one of the wonders of the way DrRacket approximates numbers.
That’s really strange. Can you explain more about how DrRacket approximates numbers? [Added ca. 14:30, 2017-09-16]
DrRacket uses IEEE Floating Point notation. You’ll learn more about it if you take CSC 161.
Do I have to worry about adding 3 to a list (or to each element of a list)? [Added ca. 14:30, 2017-09-16]
Nope. You’re just adding 3 to numbers.
Do we have to support complex numbers? [Added ca. 9:00 2017-09-18]
Yes.

### Problem 2

Do we have to keep the elements in the same order? [Added ca. 19:00 2017-09-16]
Certainly. We’ve updated the problem description to clarify this requirement.
Should I use substring? [Added ca. 9:00 2017-09-18]
No.
I’m not sure where to start. [Added ca. 9:00 2017-09-18]
In the lab in which we had 42,000 things to deal with, I asked you to find the middle 5. How did you do that?
Can you explain the parameters? [Added ca. 9:00 2017-09-18]
They provide the starting position (inclusive) and ending position (exclusive), which matches the parameters in substring.
Can we assume that all the elements in the list are integers? [Added ca. 14:30, 2017-09-16]
Yes. You should, however, state that requirement in your documentation, either in the “Parameters” or the “Preconditions” section.
Can we test for things that are not part of the program specification, such
as inexact integers? [Added ca. 16:30, 2017-09-16]
It is generally a bad idea to test for things that are not part of
Do we have to include the “optional message” for every check in our test
suite? [Added ca. 16:30, 2017-09-16]
No. But it would be nice if you had a few such explanations.

### Problem 5

Why isn’t there a section for examples and tests? [Added ca. 14:30, 2017-09-16]
You’ve written your tests in your answer to problem 4. There’s no reason to restate them.
I have this great alternate approach. Can I use it? [Added ca. 16:30, 2017-09-16]
We would prefer that you follow the indicated approach: Write a comparator that always says that odd numbers come first. Trust that sort will repeatedly call your comparator and therefore put the odd numbers before the even numbers.
Can I use the sublist procedure I wrote in problem 2? [Added ca. 9:00 2017-09-18]
Sure.
Can I use filter? [Added ca. 10:30 2017-09-18]
No. The problem explicitly says not to use filter. Your goal is to follow the model in the problem. Write the appropriate comparator.

### Problem 6

I’m not sure where to get started. [Added ca. 11:00, 2017-09-17]
I would suggest that you start by solving the problem without worrying about the course names. Once you’ve done that, come back and look at the next two questions and answers.
I have no idea how to join all of the course names together with “and”. [Added ca. 14:30, 2017-09-16]
Remember the mantra: “When dealing with lists, solve the problem for individual elements or pairs of elements, then use it on the whole list with map, sort, reduce, or filter.”
That prior answer was not helpful enough. Please explain more. [Added ca. 11:00, 2017-09-17]
First, think about how to combine two elements of the list of courses.
Next, decide which of map, sort, reduce, or filter will be most helpful in computing the value you want.

## Errata

Please check back periodically in case we find any new issues.

• In problem 2, “In many situation” should be “In many situations”. [AL, 1 point]
• When SamR added the examples to add3, he gave the wrong result for (add3 4+5i). [AK, 1 point]

## Citations

Some of the problems on this exam are based on (and at times copied from) problems on previous exams for the course. Those exams were written by Charlie Curtsinger, Janet Davis, Rhys Price Jones, Titus Klinge, Samuel A. Rebelsky, John David Stone, Henry Walker, and Jerod Weinman. Many were written collaboratively, or were themselves based upon prior examinations, so precise credit is difficult, if not impossible.

Some problems on this exam were inspired by conversations with our students and by correct and incorrect student solutions on a variety of problems. We thank our students for that inspiration. Usually, a combination of questions or discussions inspired a problem, so it is difficult and inappropriate to credit individual students.