Skip to main content

Exam 1: Starting Scheme

Assigned: Wednesday, 7 February 2018

Prologue Due: Friday, 9 February 2018 by 10:30pm

Exam Due: Tuesday, 13 February 2018 by 10:30pm

Cover Sheet Due: Wednesday, 14 February 2018 by the start of class

Epilogue Due: Wednesday, 14 February 2018 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 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: Quadrupling values

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

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, quadruple, that takes one parameter, a number, and multiplies that number by four.

> (quadruple 1)
4
> (quadruple 7.5)
30.0
> (quadruple -1/2)
-2
> (quadruple 4+5i)
16+20i

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

b. Implement quadruple using function composition. To avoid name overlap, you can call this quadruple-b. You may not use sectioning, lambda, or other procedures you define in implementing quadruple-b. You may, however, use the double procedure which is part of the latest csc151 package.

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

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

Problem 2: Sorting by substrings

Topics: sorting, lists, strings, generalizing code

In a recent assignment, you sorted a list of strings that represent course information. In particular, you sorted by course info (characters 6 to 15, inclusive, which you can extract with (substring <> 6 16)) and by course level (character 10, which you can extract with (substring <> 10 11)).

When software developers find themselves solving similar problems (and coming up with similar solutions), they look for ways to generalize the code. In this problem, you will generalize the problem of sorting by substrings.

Document and write a procedure, (sort-by-substring list-of-strings start finish), that sorts list-of-strings, using the substring of each string taken from positions start to finish, inclusive, to determine sorting order.

For example,

> (define classes
    (list "01 CSC-151" "02 CSC-321" "03 ENG-295" "04 SST-195" "05 CSC-161"
          "06 ALS-100" "07 ART-395" "08 BCM-262" "09 MUS-120" "10 PHE-101"))
> (sort-by-substring classes 3 5)
'("06 ALS-100"
  "07 ART-395"
  "08 BCM-262"
  "01 CSC-151"
  "02 CSC-321"
  "05 CSC-161"
  "03 ENG-295"
  "09 MUS-120"
  "10 PHE-101"
  "04 SST-195")
> (sort-by-substring classes 7 9)
'("06 ALS-100"
  "10 PHE-101"
  "09 MUS-120"
  "01 CSC-151"
  "05 CSC-161"
  "04 SST-195"
  "08 BCM-262"
  "03 ENG-295"
  "02 CSC-321"
  "07 ART-395")
> (sort-by-substring (list "alpha" "beta" "gamma" "delta") 2 2)
'("delta" "gamma" "alpha" "beta")
; Sorted as "l" "m" "p" "t"
> (sort-by-substring (list "alpha" "beta" "gamma" "delta") 1 2)
'("gamma" "delta" "beta" "alpha")
; Sorted as "am" "el" "et" "lp"
> (sort-by-substring (list "alpha" "beta" "gamma" "delta") 1 1)
'("gamma" "beta" "delta" "alpha")
; Sorted as "a" "e" "e" "l"
> (sort-by-substring (list "alpha" "beta" "gamma" "delta") 3 6)
. . substring: ending index is out of range
  ending index: 7
  starting index: 3
  valid range: [0, 5]
  string: "alpha"

Hint: You will likely find it useful to develop helper procedures that do things like add a substring to the front of a string or remove that added substring. Make sure to document those helpers with the 4P’s.

Problem 3: Whatzitdo?

Topics: arithmetic operators, code reading, documentation

Sometimes students (and professors) come up with difficult-to-read solutions to simple problems, like this one below:

(define z (lambda 
(w x y) (- 
        (+ w y (* 2 x)) 
    (min w x y) x (max w x y))))

a. Clean up this procedure. You should reformat the code (add carriage returns and indent to format clearly); rename the procedure, parameters, and variables; and simplify any unnecessarily complicated code.

b. Write 6P-style documentation for the code.

c. Explain how the code achieves its purpose.

Problem 4: Preparing to modify strings

Topics: strings, testing, documentation

As you may have noted, one of the things we need to do as we process data is transform that data. We’re going to consider a somewhat strange transformation, removing all copies of a character (or sequence of characters) from a string. In particular, we will write a procedure, (string-remove source pattern), that removes all copies of pattern from source

> (string-remove "alphabet" "a")
"lphbet"
> (string-remove "he took his vorpal sword in hand" "or")
"he took his vpal swd in hand"
> (string-remove "he took his vorpal sword in hand" " h")
"he tookis vorpal sword inand"

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

a. Write 6P-style documentation for a procedure, string-remove, that functions as described above.

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

(define string-remove
  (lambda (source pattern)
    ""))

Here’s the start of a test suite.

(define tests-string-remove
  (test-suite
   "Tests of the string-remove procedure"
   (test-case
    "Empty string"
    (check-equal? (string-remove "" "a") ""))
   (test-case
    "Source equals pattern, singleton"
    (check-equal? (string-remove "a" "a") ""))
   (test-case
    "Source equals pattern, longer"
    (check-equal? (string-remove "alpha" "alpha") ""))))

Note: If removing a substring creates another instance of the substring, you need not remove that new instance. Here’s an example to illustrate the point.

> (string-remove "alphabet and abab and aabb" "ab")
"alphet and  and ab"

Observe that even though we removed ab from the middle of aabb, the sequence ab remains in the newly constructed string. This behavior is permissible, but not required. You may decide whether or not to remove the newly created instance. (It will be harder to implement if you choose to remove the newly created instance.)

Note: This postcondition is likely to be difficult to write if you permit created strings to include the pattern. Do the best that you can.

Problem 5: Removing substrings

Topics: strings, lists, map, reduce

Implement string-remove.

Hint: You might find the (string-split source pattern) procedure useful. You can determine by experimentation what that procedure does.

Problem 6: Finding courses of a certain size

Topics: strings, lists, filtering

In a recent assignment, you worked with data that look like "80631 CSC-151-02 4.00 00 32 Functional Prob Solving w/lab" or "80495 THD-245-01 4.00 04 12 Lighting for the Stage".

Suppose we have a list of courses in that form. Write, but do not document, a procedure, (extract-courses-by-size list-of-courses smallest largest), that extracts all of the courses whose size is between smallest and largest, inclusive, and orders them by course capacity.

You may assume that every class has a size of less than one-hundred.

Note: Although you are extracting by size, you are sorting by capacity.

Note: You will likely need to solve this problem in multiple steps. For example, you might first put the size at the front of each entry. Make sure to document any helper procedures you write with 4P-style or 6P-style documentation.

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!

Initial questions and answers

Are we allowed to talk to others about general issues from the first few weeks 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. Put a #| before the pasted material. put a |# after the pasted material.
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 using #| and |# or semicolons. You might also add a note or two as to what you were trying to do.
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?
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.
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?
No.
Is extra credit available on this exam?
No explicit extra credit is available. However, we may find that you’ve come up with such wonderful 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?
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?
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.
Yes. The title and URL should suffice.
Can I use some other websites (different from ours websites) to solve problems in the exam?
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?
No.
How do I generate a random six digit number?
(random 1000000)
How do I submit my exam?
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)
Some of the problems are based on a recent homework assignment. Should I cite my work on that assignment?
If you refer to your work on that assignment, you should cite it.
How do I cite my work on an assignment?
Something like “Student 123456 and partner. Homework Assignment 3. CSC 151.. 6 February 2018.”
I see the comment ; STUB on some of the procedures. What does that mean?
We use the term “STUB” for a procedure that gives some default value, rather than the correct one. Stub procedures are useful when you need the procedure to exist during development, but have not yet had time to implement it.

Time logs

What should I record for “Elapsed” in the time log?
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?
What you spent the time on. For example, you might have been writing test cases, or failed solutions, or ….
Can you provide a sample?
Sure.
; Time Log:
;   Date        Start   Finish  Elapsed Activity
;   2018-02-08  18:00   18:20   20 min  Wrote four tests
;   2018-02-08  20:30   21:00   30 min  Three non-working versions
;                                       (marked as a, b, and c below).
;                                       Decided to sleep on it.
;   2018-02-09  08:00   08:10   10 min  Woke up with an idea.  Coded it.
;                                       it seems to work
;   2018-02-09  19:00   19:10   10 min  A few more tests just to make
;                                       sure.  Done!

New general questions

Why don’t we get extra credit for errors in the Q&A, errata, or code? [2018-02-08; 10:00 a.m.]
We hope to get those answers out quickly and, in doing so, we may make the occasional mistake.
May I use string-join on the exam? [2018-02-08, 21:00]
No. We have not covered it.
How many examples are enough for each procedure? [2018-02-09, 18:45]
Three or four are enough.
Do I have to write examples for the helpers? [2018-02-09, 18:45]
No. (It wouldn’t hurt, but it’s not required.)

Problem 2

All of the examples given have the specified substrings containing only letters. Should we assume that this will be true for all of the lists we use with the procedure? If not, how should we prioritize letters, numbers, characters, etc.? [2018-02-08, 9:00 a.m.]
It shouldn’t matter, but you can assume just letters, spaces, and punctuation. However, you will mostly rely on the ordering that string<=? chooses.
I see that you identified "l" as the sort key for "alpha" in (sort-by-substring ... 1 1). But (substring "alpha" 1 1) is "". [2018-02-08, 10:00 a.m.]
You are correct. However, we did say that, unlike substring, sort-by-substring includes both lower bound and upper bound. You will need to translate the parameters appropriately.
I feel like we did something similar on assignment 3. How is this different? [2018-02-08, 8:45 p.m.]
You dealt with a particular arrangement of data in the assignment and sorted by particular sections. This problem generalizes what you did on the assignment.
Can we assume that all the strings are the same length? [2018-02-09, 6:45 p.m.]
You could make that a precondition. I’d prefer a slightly more general one, but if that’s the best you can come up with, so be it.

Problem 3

Should we include examples? [2018-02-09, 6:45 p.m.]
Yes
What’s a carriage return? [2018-02-10, 9:00 a.m.]
The thing that divides your code into separate lines.
What you get when you hit the Enter key.

Problem 4

I’m not sure I understand the relationship of problems 4 and 5. Don’t we need to write a procedure before we write the tests? [2018-02-08, 9:00 a.m.]
A central idea of testing is that you write the tests before you write the code. So we provide you with a non-working version for you to write the tests around. That’s problem 4. Once you have good tests, you are ready to write the procedure. That’s problem 5.

Errata

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

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.