Skip to main content

CSC 151.01, Class 49: Discussion of Exam 3

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Testing (with problem 2)
  • Documentation (with problem 3)
  • Recursion (with problem 3)
  • Patterns (with problem 3)
  • Preconditions (with problem 5)
  • Husk and kernel (with problem 5)
  • Deep recursion (with problem 4)
  • Vector recursion (with problem 6)

News / Etc.

  • We will use today’s class to discuss exam 3.
  • New partners!
  • Grading update
    • Robert Burns remains correct.
  • Note: I have convinced Andrew Kaufman to review your works.
  • I am frustrated that some of you found it necessary to receive (and perhaps, give) inappropriate assistance on exam 3 (and, looking back, on exam 2). In the future, please ask me for support, rather than asking others.
    • To the rest of the class: I apologize that the time it will take me to address these issues will delay my other work.

Upcoming Work

  • NO lab writeup!
  • Reading for Tuesday: Search algorithms
    • Make sure to do the self-check!
  • Projects due Tuesday night.
  • Exam 4 distributed Wednesday.
  • No quiz Friday.

Extra credit (Academic/Artistic)

  • CS Table, Tuesday, Noon: Randomness or something.
  • The Harold W. ’38 and Jean Ryan ’38 Squire Lectureship in Physics. May 2nd, 7:00 p.m., ARH 302. Astrophysicist Kartik Sheth (Grinnell class of 1993) will present From Mumbai to Grinnell to NASA HQ - Adventures in Time and Space
  • IMPORTANT: CS Extras, Thursday, May 4, 4:15 p.m.: Inclusion in CS. Science 2022. Please attend. Snacks available at 4:00 p.m.

Extra credit (Peer)

  • NEW: Grinnell Monologues is performing on this Thursday and Friday in Loose Lounge sometime in the evening.
  • Drill Team Show, Friday, May 5, at Triple V stables. The other side of the golf course. Free rides (cars, not pony) to the event!
  • GSEA (Grinnell Space Exploration Agency) is launching a weather balloon on May 7th at 5:30AM. The balloon will go to almost-space and take pictures. It’ll also tell us about its location and the temperature of the air around it as it rises. We’re inviting people to come see the launch, if they want to come (despite the fact that it’s at 5:30AM).
  • Next home baseball game: Sunday, May 7 (Senior Day)
  • Advanced Performance Performances Sunday at 7pm in the Wall
  • AAA event about gender-based violance in the APIA community, Loose, 3:00-4:30 Tuesday.

Extra credit (Misc)

  • Make Your Mark on Phase 1!, Tuesday, May 2, 10 a.m., Near the Commencement Stage. (Light refreshments provided.) Leave a hand-print on the construction wall (which will become a Commencement backdrop!) to celebrate the Class of 2017. You can also make chopsticks from wood harvested at the construction site and get your photo taken at the construction-themed photo booth.
  • UPDATED: May 4 town hall on belonging. 11am, May 4, JRC 101. It sounds like we have some important things to discuss; please come share your perspectives!

Other good things to do

Questions

How does academic dishonesty make you feel?
Mostly sad that students hit a point that they feel that they have to cheat to succeed.
Also frustrated, because writing up cases uses time that I could be spending on more productive issues.
How does academic dishonesty affect your relationships with students?
It generally doesn’t. I have had advisees who were convicted of academic dishonesty in my classes. I also remain close to some other students who have been convicted.
On the other hand, I am not sure how academic dishonesty affects students’ relationship with me.
I want to put a blend in the middle of an existing image. How do I
do so?
Select the portion and then use (image-recompute! image (lambda (col row) ...))
I’d like to do a transformation based on not only the color, but also the
position. Is that possible?
Yes, we provide (image-redo! image (lambda (col row color) ...))
Can you include those examples in the eboard?
Yes.
; Note: 4 refers to an existing image
> (image-recompute! 4 (lambda (col row) (irgb col 0 row)))
4
> (image-redo! (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg"))
               (lambda (x y color)
                 (if (< x y)
                     color
                     (irgb-complement color))))
5
> (image-redo! (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg"))
               (lambda (x y color)
                 (irgb-average color
                               (irgb x 0 y))))
6
Does our actual project have to bear any resemblance whatsoever to our
proposal?
No.
But you do have to meet the requirements.

Testing (with problem 2)

Why do we write tests?

  • Sam makes us do it!
  • To make sure that our code actually works. (Tests are easier to rerun and recheck then just manually looking at the output on different inputs.)
  • As a way of documenting expectations for our client.
  • As a way of helping ourselves think through expectations.

How do we write tests?

  • test-suite
  • test-case
  • (check-equals? EXPRESSION EXPECTED OPTIONAL-MESSAGE)
  • Plus variants thereof.

If I am testing index-of-substring, should index-of-substring appear in the expression?

  • Yes. It is pointless to have tests that do not actually include the procedure you want to test.

What should we do if a test fails?

  • Reread the test to make sure that it makes sense.
  • Fix the code that you were fixing.

What are “edge cases” for strings?

  • Empty strings.
  • Different sizes: one character, a few, and a lot

What other issues do you expect to be challenging for index-of-substring?

  • Does it do zero-based indexing correctly?
  • On the “edges” of the string (front end and back end)
  • Partial matches (e.g., “aaab” and we try to match “aab”)
  • Go beyond the end (e.g., “aaab” and we try to match “abc”)

Are there other testing strategies you employ?

  • Be moderately comprehensive. E.g., check every position in the string, possibly with every length substring.

Do you think your procedure will pass this suite?

(define ios-tests
  (test-suite
   "Tests of string-contains"
   (test-case "start-of-string"
              (check-equal? (index-of-substring "banana" "ban")
                            0))
   (test-case "middle of string"
              (check-equal? (index-of-substring "banana" "nan")
                            2))
   (test-case "incorrect capitalization"
              (check-false (index-of-substring "banana" "NAN")))
   (test-case "mismatched fruit"
              (check-false (index-of-substring "banana" "orange")))
   (test-case "end of string"
              (check-equal? (index-of-substring "banana" "nana")
                            2))
   (test-case "duplicate of string"
              (check-equal? (index-of-substring "banana" "banana")
                            0))
   (test-case "search in empty string"
              (check-false (index-of-substring "" "hello")))
   (test-case "search in smaller string"
              (check-false (index-of-substring "nan" "nana")))
   (test-case "singleton pattern, position 0"
              (check-equal? (index-of-substring "abcde" "a")
                            0))
   (test-case "singleton pattern, position 1"
              (check-equal? (index-of-substring "abcde" "b")
                            1))
   (test-case "singleton pattern, position 2"
              (check-equal? (index-of-substring "abcde" "c")
                            2))
   (test-case "singleton pattern, position 3"
              (check-equal? (index-of-substring "abcde" "d")
                            3))
   (test-case "singleton pattern, position 4"
              (check-equal? (index-of-substring "abcde" "e")
                            4))
   (test-case "beyond the end"
              (check-false (index-of-substring "abcde" "def")))
   (test-case "partial match at position 0, real match at 1"
              (check-equal? (index-of-substring "aaab" "aab")
                            1))))

Documentation (with problem 3)

What are the six P’s?

  • An annoying style of documentation that Sam makes us write.
  • Procedure, Parameters, Purpose, Produces, Preconditions, Postconditions
  • Plus a few other optional P’s, such as the Props, Practicum, Process, Philosophy

What should the Produces section have?

  • The name of the result, a comma, and its type

What should the Preconditions section have?

  • It lets you control the inputs. (So does the parameters section.)
  • It also lets you control the broader world. (E.g., “A valid brush is selected”, “the foreground color is not white”)

What should the Postconditions section have?

  • A “mathy” description of the product.
  • Where “mathy” == “relatively precise”

Where should you describe how your procedure works?

  • In the Process section.
  • NOT in the Postconditions section.

For closest-to-zero

;;; Procedure:
;;;   closest-to-zero
;;; Parameters:
;;;   lst, a list of numbers
;;; Purpose:
;;;   To find the element of lst closest to zero
;;; Produces:
;;;   result, a number
;;; Preconditions:
;;;   [no additional]
;;; Postconditions:
;;;   

At least three errors appear above. What are they?

  • We should specify “real numbers” in either the parameters or the preconditions.
  • We should specify that result is a real number.
  • We may want to indicate “find and return”
  • We should require that lst is nonempty.

What are the “mathy” things you want to say in the postconditions?

  • result is an element of lst
    • If you don’t say anything else, it could be any element of the list. We could implement it with car
  • For all x in lst, (<= (abs result) (abs x))
  • If multiple values are equally close to zero, it may return any of them.
  • If zero is in the list returns zero.
  • The result may be inexact or exact, depending on other characteristics of the input.

Recursion (with problem 3)

Is the following procedure correct?

(define closest-to-zero-z
  (lambda (values)
    (cond
      [(null? (cdr values))
       (car values)]
      [(< (abs (car values)) (abs (closest-to-zero-z (cdr values))))
       (car values)]
      [else
       (closest-to-zero-z (cdr values))])))

Yes. It is correct.

What is the point of the following code?

(define ctz-counter (counter-new "closest-to-zero"))
(define ctz-experiment
  (lambda (values)
    (counter-reset! ctz-counter)
    (let ([result (closest-to-zero-z values)])
      (counter-print! ctz-counter)
      result)))

Assume I annotate closest-to-zero-z to increment the counter …

What do you expect to see as output for (ctz-experiment (list 1 2 3 4))?

  • closest-to-zero: 4
  • 1

What do you expect to see as output for (ctz-experiment (list 1 2 3 4 5))?

  • closest-to-zero: 5
  • 1

What do you expect to see as output for (ctz-experiment (list 4 3 2 1))?

  • closeest-to-zero: 4
  • 1

What do you expect to see as output for (ctz-experiment (list 5 4 3 2 1))?

To think about: Why?

Patterns (with problem 3)

How would you generalze the following procedure?

(define largest
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (max (car lst) (largest (cdr lst))))))
(define largest
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (max (car lst) (largest (cdr lst))))))

How does that help us find the closest to zero?

Preconditions (with problem 5)

(define sierpinski-carpet-region!
  (lambda (image left top width height small-enough)
     ...
        (image-select-rectangle! image REPLACE left1 top1 w/3 h/3)
     ...))

What are important preconditions of image-select-rectangle!?

What types should the parameters of `sierpinksi-carpet-region! be?

Husk and kernel (with problem 5)

What is husk-and-kernel programming?

What form does it normal take?

What should we test for sierpinski-carpet-region!?

Deep recursion (with problem 4)

When should we (or should we not) draw the trunk?

After drawing the trunk, what should we do?

Is there a procedure that we can use to draw a branch?

How do the parameters differ for our call to that procedure?

Suppose we had to undo everything. How do you undo a move forward?

Suppose we had to undo everything. How do you undo a turn?

When should we undo our move forward?

When should we undo our turn?

Vector recursion (with problem 6)

What pattern do you know for vector recursion?

How does that apply to this problem?