Exam 2: Expanding your Scheme skills
Assigned: Wednesday, 4 October 2017
Prologue Due: Friday, 6 October 2017 by 10:30pm
Exam Due: Tuesday, 10 October 2017 by 10:30pm
Cover Sheet Due: Wednesday, 11 October 2017 by the start of class
Epilogue Due: Wednesday, 11 October 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
exam02.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.03: Exam 2 Prologue (your name).
-
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”.
-
Which of those six problems do you expect to be the most difficult for you to solve? Why?
-
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. (Note that it is perfectly fine to just repeat what you wrote in the epilogue for the prior exam.)
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.03: Exam 2 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: Counting between
Topics: iota, lists, documentation, etc.
We have found the iota procedure useful in many situations. But
what if we want the list to start with a number other than 0? Let’s
consider a procedure, (values-between start finish), that creates
a list of integers that starts at start and finishes immediately
before finish.
> (values-between 3 5)
'(3 4)
> (values-between 7 12)
'(7 8 9 10 11)
> (values-between -3 3)
'(-3 -2 -1 0 1 2)
a. Document values-between.
b. Implement the kernel of this procedure; that is, write a procedure
(values-between-kernel start finish), that produces a list of the
values between start and finish.
c. Implement the husk of this procedure. That is, write a procedure that
checks all of the preconditions of values-between and reports an appropriate
error for each one.
Problem 2: Points in a grid
Topics: map, lists, anonymous procedures
Without using recursion, write, but do not document, a procedure,
(grid width height), that makes a list of (column row) pairs in which
column takes on all values from 0 to width-1 and row takes on all
values from 0 to height-1.
For example,
> (grid 2 2)
'((0 0) (0 1) (1 0) (1 1))
> (grid 4 1)
'((0 0) (1 0) (2 0) (3 0))
> (grid 4 3)
'((0 0) (0 1) (0 2) (1 0) (1 1) (1 2) (2 0) (2 1) (2 2) (3 0) (3 1) (3 2))
Note: If you find it easier, you can make a reasonably nested list (e.g., one list per row), rather than a single list.
> (grid 4 3)
'(((0 0) (0 1) (0 2))
((1 0) (1 1) (1 2))
((2 0) (2 1) (2 2))
((3 0) (3 1) (3 2)))
Hint: You may find it helpful to write a procedure that makes a list of all the points in one row or one column.
Note: We’ve presented the list-of-lists example for clarity, rather than how DrRacket will necessarily format it. If your screen is wide, you will likely see something closer to the following.
> (grid 4 3)
'(((0 0) (0 1) (0 2)) ((1 0) (1 1) (1 2)) ((2 0) (2 1) (2 2)) ((3 0) (3 1) (3 2)))
Problem 3: Describing numbers
Topics: types, conditionals
Write, but do not document, a procedure, (describe-number val), that
takes one parameter, a number, and returns a string that describes
the number. You should describe the basic type (integer, real, or
complex), the exactness (exact or inexact), and, when appropriate,
the sign (positive or negative)
Note that you should only use the closest type possible. Hence, if a value is an integer, a real, a rational, and complex, you should describe it as an integer.
Here are some examples.
> (describe-number 1)
"positive exact integer"
> (describe-number -2.0)
"negative inexact integer"
> (describe-number 11/3)
"positive exact real"
> (describe-number 3+4i)
"exact complex number"
> (describe-number (sqrt 2))
"positive inexact real"
Part of your goal in this problem is to be concise. In particular,
you should not have twelve different cases in one cond.
Problem 4: Finding neighboring words
Topics: strings, lists, files
Write, but do not document, a procedure, (neighboring-words file word),
that finds all of the words that appear immediately before or after the
given word in the named file.
For example, if the file contained the previous paragraph, you would see the following output.
> (neighboring-words "neighboring-words.txt" "the")
'("of" "words" "after" "given" "in" "named")
Note: If you find it necessary to name this procedure
neighbouring-words, you may.
Problem 5: Whatzitdo?
Topics: lists, local bindings, code reading, documentation
Consider the following interesting procedure definition.
(define s (lambda (l) (let ([a (iota (length l))]) (let ([a (map
even? a)]) (let ([a cadr] [b (map list l a)]) (let* ( [c (filter a b)])
(list (map car c) (map car (filter (negate cadr) b)))))))))
Determine what this procedure does and then do the following.
a. Reformat the procedure so that it follows reasonable standards. (You need not show this version; we only want to see the version in part b.)
b. Rename the procedure, parameters, and local variables so that they will make sense to the reader.
c. Write 6P-style documentation for the renamed procedure.
d. Explain how the procedure achieves its goal.
Problem 6: Categorizing words
Topics: strings, conditionals, and and or
In a recent assignment, you were asked to write a procedure to categorize words. Here’s one solution to that task.
;;; Procedure:
;;; categorize-word
;;; Parameters:
;;; word, a string
;;; Purpose:
;;; Assign one of a few categories to word.
;;; Produces:
;;; category, a string
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; category reasonably describes some characteristic of word.
(define categorize-word
(lambda (word)
(let* ([wordlen (string-length word)]
[halflen (quotient wordlen 2)])
(if (even? wordlen)
(cond
[(zero? wordlen)
"empty"]
[(string=? (substring word 0 halflen)
(substring word halflen))
"repeated"]
[else
"even length"])
(if (> wordlen 10)
"oddly long"
(if (= wordlen 1)
"one letter"
"odd length"))))))
For example,
> (categorize-word "")
"empty"
> (categorize-word "wikiwiki")
"repeated"
> (categorize-word "triskaidekaphobia")
"oddly long"
In our consideration of conditionals, you’ve learned that and and or
suffice to write any conditional. Rewrite the procedure above using
and and or rather than if and cond. In your examples and tests,
show that you’ve verified that all six different outputs are possible.
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
These questions and answers were provided in the initial release of the examination.
- 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, at least three or four per problem. 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?
- Probably not. Ask about individual procedures if you’re not sure.
- I discovered a way to define procedures without using
lambdathat 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 DrRacket to test my work?
- 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?
- 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.”
- But be sure that the other Web sites rely on the procedures you’ve learned and follow the Scheme style we use.
- 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 2” (or something similar)
- How should we write our tests and examples?
- 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.
- How many points will I lose if I don’t do the prologue?
- Somewhere between 2 and 5, depending on the mood of the grader.
- How many points will I lose if I do the prologue late?
- Somewhere between 0 and 5, depending on the mood of the grader.
- How many points will I lose if I don’t do the epilogue?
- Somewhere between 2 and 5, depending on the mood of the grader.
- How many points will I lose if I do the epilogue late?
- Somewhere between 0 and 5, depending on the mood of the grader.
- How many points will I lose if I don’t use the required template?
- Somewhere between 0 and 10, depending on the mood of the grader.
- Will I lose points if I fail to indent my code correctly with ctrl-i?
- Almost certainly.
- How many points will I lose if I fail to indent my code correctly?
- It depends on how hard your code is to read.
- Can you provide a sample time log?
- 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! - Do I have to use YYYY-MM-DD format?
- Certainly. Why would you use anything else?
- Do I get the bonus points for errors if I successfully invoke “There’s more to life”?
- Nope.
- Do I lose points for missing prologue/epilogue and other similar issues if I successfully invoke “There’s more to life”?
- Yes.
- I can’t spell. I know that you’ll be taking off points if I spell words incorrectly. What do you suggest?
- Copy and paste your exam into Microsoft Word or some other word processor.
- Run the spell check. Ignore the many Scheme-isms that aren’t problems.
- Copy and paste your exam back into DrRacket. ; Re-indent using ctrl-i. ; Re-check that your code still works. (Minimally, click Run.)
New general questions and answers
These questions and answers were added after the initial release of the examination.
- If I write a helper procedure for one of the problems that does not require me to document the primary procedure, do I need to document the helper? [2017-10-05 ca. 10:30]
- Yes. All non-local helpers should be documented with the 4P’s.
- Do I have to document local helper procedures? [2017-10-05 ca. 10:30]
- It’s nice if you write a one-line comment that explains what they do, but you certainly don’t need 4P’s.
- What’s a local helper procedure? [2017-10-05 ca. 10:30]
- One defined within the procedure, most typically with a
letorlet*. - Will you consider the clarity, efficiency, and elegance of our solution? [2017-10-07 ca. 12:00]
- _Admittedly, this question was asked more implicitly than explicitly.)
- Yes. Particularly inelegant, unclear, or inefficient solutions are likely to lose points.
Problem 1
These questions and answers were added after the initial release of the examination.
- I’m not sure how to deal with negative numbers, such as in the third example. [2017-10-05 ca. 15:00]
- The third example has six numbers. You’ll probably use
iotato generate a list of six numbers. How do you go from'(0 1 2 3 4 5)to'(-3 -2 -1 0 1 2)? - I’m not sure what you mean by “husk”. [2017-10-05 ca. 15:00]
- The reading on checking preconditions explains husk and kernel.
- Basically, you’ll start by renaming the kernel to
values-between-kernel. Then you’ll have a bigcondstatement invalues-betweenthat checks each precondition in turn and reports an error if the precondition is not met. - Since both the husk and kernel have the same parameters, purpose, product, preconditions, and postconditions (except that the husk verifies them), do I need to write two separate sets of documentation? [2017-10-06 ca. 19:00]
- No
- Should I use
takeanddropfor this problem? [2017-10-06 ca. 19:00] - Probably not.
- I’m going to use
takeanddropfor this problem. [2017-10-07 ca. 12:00] - Don’t.
- I thought I’d solved this based on your hint above, but it’s not working when I have two negative numbers. Can you provide more assistance? [2017-10-08 ca. 17:00]
- Suppose
startis -8 and finish is -3. There should be five values,'(-8 -7 -6 -5 -4). How do you build a list of five values? One way to start is with(iota 5), but that gives you'(0 1 2 3 4). How can you go from that list to the list you want?
Problem 2
- Is width the number of columns? [2017-10-06 ca. 19:00]
- Yes
- Is it okay if the format for the “list of lists” version does not quite match? [2017-10-08, ca 06:30]
- Yes. We’ve updated the problem to clarify that issue.
- I’m not sure how to get started. [2017-10-08, ca 06:30]
- Approach one: Think about the “solve it for one, then for many”
mantra, although perhaps at a different level. Write a procedure
that generates one row or column. Then
mapit to get all of the rows or columns. - Approach two: Make a list of all the columns in the order they appear in the output list. Make a list of all the rows in the order they appear in the output list. Join them together.
Problem 3
These questions and answers were added after the initial release of the examination.
- Is zero considered a positive or negative number? [2017-10-05 ca. 20:00]
- Neither.
- So what is zero’s sign? [2017-10-05 ca. 20:00]
- It doesn’t have one, just as complex numbers don’t have one.
- Do I need to consider the case in which the input is not a number? [2017-10-05 ca. 20:00]
- No.
- Just to be clear, there are only two ways to describe zero, right?
"exact integer"and"inexact integer"? [2017-10-07 ca. 12:00] - Yup.
- How concise should I be? [2017-10-07 ca. 12:00]
- One of the instructors wrote this as an
ifand two three-casecondexpressions (including the alternative), although not in that order. It would be nice if you could be that concise. - Can I nest my conditionals? [2017-10-07 ca. 12:00]
- Yes, but you can solve the problem without nesting the conditionals.
- What/how many options are there? [2017-10-07 ca. 12:00]
- Figuring out the full list is part of your job. But we count twelve.
Problem 4
These questions and answers were added after the initial release of the examination.
- Do you want the neighboring words of the input to appear in the same order as they are in the file? [2017-10-05 ca. 20:00]
- I don’t care.
Problem 5
These questions and answers were added after the initial release of the examination.
- Do I have to rename the internal variables, too? [2017-10-08, ca 06:30]
- Yes.
- It looks to me like we’re filtering using
cadr. That makes no sense to me. [2017-10-08, ca. 17:00] - Think about what filter does (discards all elements for which the procedure returns false) and what the list you’re filtering looks like.
Problem 6
These questions and answers were added after the initial release of the examination.
- I have no idea how to get started. [2017-10-05 ca. 15:00]
- Think about how you’d translate a single
ifstatement. Remember thatorevaluates each parameter in turn until it gets a non-false value or reaches the end and thatandevaluates each parameter in turn until it gets a false value or reaches the end. You’ll probably need to nest the two.
Errata
Please check back periodically in case we find any new issues.
- The example for problem 4 was not quite correct. [AA, 1 point]
- The list-of-lists example for problem 2 was unclear because it demonstrates formatting that may not occur in DrRacket unless you shift your window size. (This is not strictly an error.) [Various, 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.