000000.rkt
, but replace 000000
with your generated random number. Email this file as an attachment to the instructor when you are finished with your exam.
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.
The prologue for this examination will be via email, which you should send to your instructor by 10:30 p.m. on Friday evening. Your message should be titled CSC 151.01: Exam 1 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 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.
The epilogue for this examination will also be via email, which you should send to your instructor by 10:30 p.m. on the evening after the exam is due. 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?
Topics: Procedures, Image making, Style
We recently considered mechanisms for making a simple chess or checkerboard. As you may recall, such a board involves alternating two colors of squares in a grid.
Your classmates, Drew and Rachel, have decided to generalize chessboards so that the client can choose the colors and shapes that appear in the grid. Here’s how they’ve started
;;; Procedure:
;;; grid-2x2
;;; Parameters:
;;; make-image, a unary procedure
;;; color1, a color
;;; color2, a color
;;; Purpose:
;;; Make a 2x2 grid of images of alternating colors.
;;; Produces:
;;; two-by-two, an image
;;; Preconditions:
;;; make-image takes a color as a parameter and returns an image.
;;; Postconditions:
;;; two-by-two consists of four nearly identical images in a 2x2 grid.
;;; The top-left and bottom-right images are color1. The top-right
;;; and bottom-left images are color2.
(define grid-2x2
(lambda (make-image color1 color2)
(above
(beside (make-image color1) (make-image color2))
(beside (make-image color2) (make-image color1)))))
Here’s how it works (or seems to work).
> (grid-2x2 (section circle 20 'solid <>) 'red 'gray)
> (grid-2x2 (section star 20 'solid <>) 'purple 'cornflowerblue){:width="20px"}
Drew and Rachel sometimes take things to excess and have written a procedure that computes a 16x16 grid. Here’s a sketch.
(define grid-16x16
(lambda (make-image color1 color2)
(above
(beside (make-image color1) (make-image color2) (make-image color1) (make-image color2) ... (make-image color2)) ; Sixteen calls to make-image
(beside (make-image color2) (make-image color1) (make-image color2) ... make-image color1) ; Another sixteen calls
...
(beside (make-image color2) (make-image color1) (make-image color2) ... make-image color1)))) ; Sixteen rows!
Their procedure appears to work correctly.
> (grid-16x16 (section star-polygon 10 7 2 'solid <>) 'red 'black)
However, their instructor was so horrified by the use of 256 nearly
identical calls to make-image
that you could hear the screams
throughout Noyce 3rd.
Rewrite grid-16x16
so that it is more concise and therefore more
elegant.
Topics: lists
You may recall that the (substring str start finish)
extracts a
portion of a string. We should find it equally useful to be able
extract a sublist of another list: All the entries starting at
position start
and ending right before position finish
. We
have procedures that are close; take
grabs elements from the start
of a list and drop
removes elements. But neither grabs from the
middle.
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, such as take
and drop
.
Topics: code reading, documentation, sectioning and composition
Sometimes students (and professors) come up with difficult-to-read solutions to problems. What makes them difficult? Strange variable names. Awkward formatting. “Clever” approaches. Here is one such solution:
(define
:/ (o
(section reduce string-append <>)
(lambda
(
:-) (map (o string (section string-ref
:- <>) (section - (string-length
:-) <> 1)) (range (string-length
:-)
)))))
Figure out what this procedure does and then do the following:
a. Rename the procedure and parameter(s) so that their type and purpose is clear.
b. Reformat the code.
c. Explain how the procedure works in your own words. Your explanation should not closely resemble the Scheme code for this procedure. (For example, you might not mention the composition; rather, you can say what the purpose of the composed function is.)
d. Write 6P-style documentation for the code. (Do your best on the preconditions and postconditions.)
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).
You should only use the closest type possible. Hence, if a value
is an integer, a real, and complex, you should describe
it as an integer. Since DrRacket treats rational
and real
as
the same, you need not worry about differentiating the two; just
use real
.
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 ten or so different cases in one cond
.
Topics: Regular expressions, Markup languages
Many programmers find it irritating to put all the tags in an HTML document. Hence, a number of simplified markup languages have been developed, along with tools that translate from that language to HTML. One of the most popular is called markdown (a twist on “markup”). Here are some basic rules in markdown.
<strong>
and </strong>
.<em>
and </em>
.#
) and a space should be turned
into level-one headings, using <h1>
and </h1>
.##
) and a space should be turned
into level-two headings, using <h2>
and </h2>
.###
) and a space should be turned
into level-three headings, using <h3>
and </h3>
.<code>
and </code>
.a. Write, but do not document, a procedure, (markdown-code str)
, that
follows the code rule.
> (markdown-code "Use `markdown-code` to markup code.")
"Use <code>markdown-code</code> to markup code."
> (markdown-code "A single backtick, like `, should be left alone.")
"A single backtick, like `, should be left alone."
b. Write, but do not document, a procedure, (markdown-emphasize str)
, that takes a string
as input and follows the emphasis rules. You can assume that we don’t
have individual _
symbols inside a strongly emphasized section.
> (markdown-emphasize "This is a __strange__ example, _isn't it_?")
"This is a <strong>strange</strong> example, <em>isn't it</em>?"
> (markdown-emphasize "Emphasize _this_ and _that_, but not what's in between.")
"Emphasize <em>this</em> and <em>that</em>, but not what's in between."
c. Write, but do not document, a procedure, (markdown-headings
str)
, that takes a string as input and follows the heading rules.
> (define example "Testing\n# A heading\n#No space means no heading\n## Another heading\n## No end-of-line, no heading")
> (display example)
Output! Testing
Output! # A heading
Output! #No space means no heading
Output! ## Another heading
Output! ## No end-of-line, no heading
> (markdown-headings example)
"Testing\n<h1>A heading</h1>\n#No space means no heading\n<h2>Another heading</h2>\n## No end-of-line, no heading"
> (display (markdown-headings example))
Output! Testing
Output! <h1>A heading</h1>
Output! #No space means no heading
Output! <h2>Another heading</h2>
Output! ## No end-of-line, no heading
> (markdown-headings "# Heading on the first line.\n")
"<h1>Heading on the first line.</h1>\n"
d. Write, but do not document, a procedure, (markdown-paragraphs str)
,
that processes paragraphs according to the markdown rules, treating
blank lines as paragraph separators and marking paragraphs with
<p>
and </p>
tags.
> (define lines "\nHello\nworld\n\nThis is a test.\n\n\nAnd\nanother.")
> (display lines)
Output!
Output! Hello
Output! world
Output!
Output! This is a test.
Output!
Output!
Output! And
Output! another.
> (markdown-paragraphs lines)
"<p>\nHello\nworld\n</p>\n\n<p>\nThis is a test.\n</p>\n\n<p>\nAnd\nanother.\n</p>\n"
> (display (markdown-paragraphs lines))
Output! <p>
Output! Hello
Output! world
Output! </p>
Output!
Output! <p>
Output! This is a test.
Output! </p>
Output!
Output! <p>
Output! And
Output! another.
Output! </p>
We’ve chosen to put the paragraph tags on separate lines. You could also choose to put them on the same line as the text. In addition, If you’d prefer to combine lines in paragraphs, that’s fine.
> (display (markdown-paragraphs lines))
Output! <p>Hello world</p>
Output! <p>This is a test.</p>
Output! <p>And another.</p>
As in the case of headings, you should not assume that the text begins with a blank line.
> (markdown-paragraphs "Just one")
"<p>Just one</p>"
e. Write a procedure, (markdown str)
, that applies all of the
procedures above.
Topics: strings, lists, filtering, reduction, conditionals
It is often useful to be able to present a list of characters from a text. While the general problem is somewhat complicated (e.g., While capitalized words often represent proper names, that does not hold for all proper names; some people, such as bell hooks and e.e. cummings, prefer not to have their names capitalized), we can develop a simplified version.
Our goal is to take a list of strings and convert them into an alphabetized list of things that might be proper names, in alphabetical order. Here’s a possible sequence of steps.
a. Write, but do not document, a procedure, (proper-names lst)
, that
takes a list of strings as input and returns those that seem to be
proper names.
> (proper-names (list "Ginger" "and" "Fred" "and" "Dr." "Smythe" "and" "another" "Ginger"))
'("Ginger" "Fred" "Smythe" "Ginger")
b. Write, but do not document, a procedure, (add-name name names)
,
that takes two strings as parameters, one a single name and one a
set of names separated by spaces. If the name appears at the start
of the set of names, we just return the set of names. Otherwise,
we add the name to the start.
> (add-name "Jill" "")
"Jill "
> (add-name "Jill" "Jill ")
"Jill " ; Already at the front
> (add-name "Jackie" "Jill ")
"Jackie Jill "
> (add-name "Jack" "Jackie Jill ")
"Jack Jackie Jill "
> (add-name "Jack" "Jack Jackie Jill ")
"Jack Jackie Jill " ; Already at the front
> (add-name "Jill" "Jack Jackie Jill ")
"Jill Jack Jackie Jill " ; Not at the front
c. Using the procedures you wrote in parts a and b, along with
anything else you may find useful (e.g., sort
, reduce-right
,
string-split
), write a procedure, (unique-proper-names words)
,
that takes a list of strings as input and returns a list of the
proper names in the list, with no duplicates, and in alphabetical
order.
Note: If you look on the InterWeb for procedures to remove duplicates from a list, you are likely to find many solutions, often of a form something like the following.
(define remove-duplicates
(lambda (lst)
(if (null? lst)
lst
(cons (car lst) (remove-duplicates (remove-copies (car lst) (cdr lst)))))))
(define remove-copies
(lambda (val lst)
(cond
[(null? lst)
null]
[(equal? val (car lst))
(cdr lst)]
[else
(cons (car lst) (remove-copies (cdr lst)))])))
You may not use such procedures, including the one we’ve just written. Rather, you must rely on the strategy we’ve described, including combining the words into a string so that there are no duplicates in the string.
We will post answers to questions of general interest here while the exam is in progress. Please check here before emailing questions!
section
?#|
before the pasted material. Put
a |#
after the pasted material.#|
and |#
or semicolons. You might also add a note or two as to
what you were trying to do.lambda
that looks like (define (proc params) body)
. Can I use that?(random 1000000)
. If you end up with a number less than six digits, add zeros to the left side of the number until you have six digits.; STUB
on some of the procedures. What does that mean?grid-2x2
? [2019-02-13, 9:30 p.m.]map
and reduce
? [2019-02-13, 9:30 p.m.]grid-n-by-n
? [2019-02-13, 9:30 p.m.]sublist
select the whole list, as in (sublist lst 0 (length lst))
? [2019-02-14, 9:00 a.m.] (Updated [2019-02-17, 12:30 p.m.])sublist
to select the whole list, your procedure should support that option.1+0i
and 1+0.0i
? [2019-02-18, 2:30 p.m.]<p>
, the stuff between, and </p>
”. I’d recommend that you do something more like “Identify the start of each paragraph, add a <p>
. Identify the end of each paragraph. Add a </p>
.<p>
tags around my headers? [2019-02-18, 2:30 p.m.]#px
form.) Another involves a strategy like you use for ing?
.string=?
or equals?
Please check back periodically in case we find any new issues.
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, Fahmida Hamid, 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.