Skip to main content

Exam 1: Starting Scheme

Assigned
Wednesday, 13 February 2019
Due
Tuesday, 19 February 2019 by 10:30pm
Collaboration
You may not receive help from any person other than the instructor for this exam. See the exam procedures for a detailed description of the resources you can and cannot use.
Submitting
Rename the starter code file to 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.

Prologue

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).

  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 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?

Problem 1: Making large grids

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)
A two-by-two grid of circles.  The top-left and bottom-right circles are red.  The top-right and bottom-left circles are gray.
> (grid-2x2 (section star 20 'solid <>) 'purple 'cornflowerblue){:width="20px"}
A two-by-two grid of stars.  The top-left and bottom-right stars are purple.  The top-right and bottom-left stars are a light blue.

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)
A sixteen-by-sixteen grid of seven-pointed stars.  The first row starts with a red star and alternates red and black stars.  The second row starts with a black star and alternates black and red stars.  The remaining rows alternate in the same pattern.

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.

Problem 2: Computing sublists

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.

Problem 3: Whatzitdo?

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.)

Problem 4: 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).

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.

Problem 5: Markdown, simplified

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.

  • Blank lines separate paragraphs.
  • Text that begins with two underscores and ends with two underscores should be strongly emphasized, with <strong> and </strong>.
  • Text that begins with one underscore and ends with one underscore should be emphasized with <em> and </em>.
  • Lines that begin with a single octothorpe (#) and a space should be turned into level-one headings, using <h1> and </h1>.
  • Lines that begin with two octothorpes (##) and a space should be turned into level-two headings, using <h2> and </h2>.
  • Lines that begin with three octothorpes (###) and a space should be turned into level-three headings, using <h3> and </h3>.
  • Text that begins with a backtick (```) and ends with a backtick should be turned into code-like text, with <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.

Problem 6: Extracting names

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.

  • Extract all of the strings that meet the “pattern” for proper names (start with a capital letter, contain only letters).
  • Put them in alphabetical order.
  • Combine them into a string, avoiding duplicates.
  • Split that string at spaces.

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.

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

What is a general question?
A question that is about the exam in general, not a particular problem.
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.
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.
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.) In the future, email yourself a copy of the exam each time you pause.
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. Those without a date stamp were likely present when the exam was released.
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 other websites as a reference for 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). 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.
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 in solving the problem, you should cite your assignment.
How do I cite my work on an assignment?
Something like “Student 123456 and partner. Homework Assignment 3. CSC 151.01. 6 February 2019.”
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. You should remove these comments as you replace stub procedures with real implementations.
Are there any requirements for informal tests?
Sort of. They should be good tests, though they need not be exhaustive. Your informal tests only count if they are different from the examples on the exam.
Do we need to show examples of running our helper procedures?
No, but you should be checking them carefully. You don’t need to include these examples in your exam.
Do we need to explicilty verify preconditions, as we learned in Friday’s class? [2019-02-16, 7:30 p.m.]
Nope.
Do we get extra credit for finding errors in the Q&A section? [2019-02-17, 12:30 p.m.]
Nope. But you do get our appreciation.

Questions on problem 1

Can my procedure call grid-2x2? [2019-02-13, 9:30 p.m.]
Certainly.
How many procedure calls should we strive for? [2019-02-13, 9:30 p.m.]
Under a dozen would be nice. More than fifty or so is probably way too many.
Can I use map and reduce? [2019-02-13, 9:30 p.m.]
Certainly. It’s not an approach we thought about, but if you think it may be useful, you can try.
Will I get extra credit if I design the more general grid-n-by-n? [2019-02-13, 9:30 p.m.]
Perhaps. It will depend on a variety of factors.
Can I define multiple helper procedures? [2019-02-14, 9:00 a.m.]
Certainly.
Can I map a procedure that makes an image over a list? [2019-02-15, 9:30 p.m.]
Certainly. See exercise 6 on from the lab on boolean values, predicates, and conditionals.

Questions on problem 2

Can 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.])
While it is unlikely that someone would use sublist to select the whole list, your procedure should support that option.

Questions on problem 3

What name should we use in the documentation? The original ‘:/’ or the much improved name we came up with? [2019-02-13, 9:30 p.m.]
Please use the much-improved name you came up with.
What do you mean when you say that the description should not closely resemble the Scheme code? [2019-02-14, 9:00 a.m.]
Some students are tempted to detail every step. But we want a higher-level overview. For example, if the code contained (section - <> 2), you would write “subtract 2” not “section the subtraction operation using 2 as the second parameter”.
How should we reformat the code for clarity? [2019-02-16, 7:30 p.m.]
Make sure that lines are not wider than eighty characters. Make sure that all the parameters to a procedure are either on the same line or aligned vertically (with the left edge of each parameter at the same indent). Get rid of weird spaces. Make sure that it’s indented correctly, using Ctrl-I.
I’ve found a much simpler procedure that accomplishes the same result. Can I use that instead? [2019-02-17, 4:00 p.m.]
While you can provide that as an addition to your solution, you must still clean up and explain the extant code.

Questions on problem 4

What should we do with zero? [2019-02-15, 10:30 p.m.]
Zero is either an exact integer or an inexact integer.
How should we treat 1+0i and 1+0.0i? [2019-02-18, 2:30 p.m.]
Racket tells me that the first is real and the second is complex. You can do whatever makes the most sense to you. (You will likely find it easier to follow Racket’s lead.)

Questions on problem 5

What should we do with paragraphs if there are no blank lines? [2019-02-16, 7:30 p.m.]
As they example suggests, if there are no blank lines, you should still treat it as if there’s one paragraph.
Any hints on the paragraph problem? [2019-02-18, 2:30 p.m.]
A lot of you seem to be approaching this as “find the start of a paragraph all the stuff between and then the end of a paragraph; replace with <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>.
Is it okay if my procedure puts <p> tags around my headers? [2019-02-18, 2:30 p.m.]
Yes.

Questions on problem 6

Do you have any hints about how we might approach the problem of determining whether a string appears at the start of another string? [2019-02-13, 9:30 p.m.]
One approach involves regular expressions. (Note that if your regular expression contains only characters, you can use a string rather than a #px form.) Another involves a strategy like you use for ing?.
Can I use conditionals on this problem? [2019-02-15, 10:30 p.m.]
You may certainly use conditionals on this problem. The “don’t do this” section refers mostly to code you’ll find if you search for how to remove duplicates from a list. We don’t want you to use that kind of code, but rather to follow the process we’ve described.
What’s the easiest way to compare two strings for equality? [2018-02-18, 2:30 p.m.]
string=? or equals?

Errata

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

  • “equally” was misspelled on problem 1 [SM, 1 pt, Rebelsky section only]
  • Sam forgot to post the prologue and epilogue [1 pt, Rebelsky section only]
  • The sentence that describes backticks in problem 5 ends abruptly. [DL, 1 point]
  • In the problem that describes headings, the rule for level-three headings says “level-two”. [AS, 1 point]

Acknowledgements

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.