Skip to main content

CSC 151 2019S, Class 11: Testing your procedures

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Some notes from Friday’s class
  • About testing
  • Lab

Preliminaries

News / Etc.

  • Welcome to any prospective students visiting class today!
  • Mentor sessions Wednesday 8-9 p.m., Thursday 8-9 p.m., Sunday 5-6 p.m.
  • I’ve distributed the class code of conduct. Please sign and return by Wednesday.
  • Thanks for all the good questions on the exam.
  • I distributed Friday’s quiz as an FYI. There’s no need to do it, but you may want to read through it to see what kinds of things we care about.
  • Congrats to Men’s BBall for another victory on Saturday. Good luck this coming weekend!
  • Remember: Book office hours at https://rebelsky.as.me/schedule.php
  • Remember: I do take questions via email, and writing up questions can often help you think through a problem.
  • Welcome to Anna and Maddie (or vice versa) who are here to talk to you about CPUS. “Computing Peers for Understanding and Support”
    • CPUS builds community in the department.
    • Once upon a time, long long ago, the department was small and it was easy to get to meet people.
    • CS is hard. But people survive and even thrive and get jobs.
    • Program provides you with someone to talk to about how much you hate 151 and how Sam can’t estimate how long tests take and how you are struggling (or not).
    • Helps overcome the mythology about our classes.
    • Helps you think through all the stuff at Grinnell - How do you balance classwork and socialization and job and …?
    • The opportunity to talk to someone older without having to take (too much) initiative.
    • You will get an email to ask you for information.
    • You can express identity issues (e.g., as someone who identifies as female/first gen/international/low SES, I’d like to talk to someone else who identifies as female/first gen/international/low SES.
    • Plus you get free food when you meet with your CPUS peer.

Upcoming work

  • Reading due before class Wednesday
  • Exam 1 due Tuesday night.
  • Flash cards due Wednesday at 8:00 p.m.
    • Covers Wednesday/Friday/Monday classes
  • Lab writeup due before class Wednesday.
    • Exercise: 7
    • Subject: CSC 151.01 Writeup for Class 11 (YOUR NAMES)
    • To: csc151-01-grader@grinnell.edu
  • Quiz Friday: Conditionals, Preconditions and Postconditions, Precondition checking, and Testing.

Extra Credit

Extra credit (Academic/Artistic)

  • CS Table, Tuesday, Noon, JRC 224B, Who Owns 3D Scans of Historic Sites? Readings available at the back of the room.
  • CS Extras, Thursday, 4:15 p.m. Science 3821: Sam talks about course design. (Snacks at 4pm in the CS Commons.)

Extra credit (Peer)

  • Grinnell Symphony, Wednesday, 7:30 p.m., in Sebring-Lewis
  • Men’s BBall, Friday, 5:00 p.m. vs. LFC at St. Norbert. (Hopefuly a Saturday game, too.)
  • Indoor Track and Field, Friday and Saturday, at Monmouth.

Extra credit (Wellness)

  • HIIT training, 4:30 pm, Tuesday, Dance Studio, Bear. (Cap of two EC units.)
  • HIIT training, 10:00 am, Saturday, Dance Studio, Bear
  • Hatha Yoga, 7:00 pm, Tuesday, Dance Studo, Bear. (Cap of two EC units.)
  • Brazilian Jiu-Itsu, Wednesday and Friday, 6:30, Dance Studio (cap of two EC units.)
  • Boxing 2pm Dance Studio Saturday. (Cap of two EC units.)

Extra credit (Misc)

Other good things

Questions

How did you write merge-lines?

There are multiple approaches. The simplest that I could come up with was drop newlines that follow a character, which gives us each “paragraph” as a single line, then convert each newline to two newlines (to get a blank space), then convert each sequence of more than two newlines to just two newlines.

(define merge-lines-stage-1
  (lambda (str)
    (regexp-replace* #px"([^\n])\n" str "\\1 ")))

(define merge-lines-stage-2
  (lambda (str)
    (regexp-replace* #px"([^\n])\n" str "\\1\n\n")))

(define merge-lines-stage-3
  (lambda (str)
    (regexp-replace* #px"\n\n+" str "\n\n")))

(define merge-lines
  (lambda (infile outfile)
    (string->file (merge-lines-stage-3
                   (merge-lines-stage-2
                    (merge-lines-stage-1
                     (file->string infile))))
                  outfile)))

There’s a bit more cleanup we could do beforehand (e.g., removing spaces at the end of a line) or afterwards (e.g., removing blank lines at the beginning or end), but this suffices.

Moral: Do things step-by-step and then combine.

Do the prospies have any questions for the students?

Nope.

Some notes from Friday’s class

All of exercise 3 from Friday’s class about the maxim “Solve for one or two, then solve for a list. That is, the process should be that you come up with an appropriate procedure and use it with one of the “list combining” procedures. Some of you missed that idea, so we’re going to reinforce it a bit here.

To combine words with a space in between

Suppose we’ve figured out how to combine two strings with a space in between (combine-with-space), who do we combine a list of strings?

  • Possibly with map. But map gives us back a list.
  • Possibly with reduce, which tends to turn lists into single values.

First, combine two words with a space.

(define combine-with-space
  (lambda (str1 str2)
    (string-append str1 " " str2)))
; or
(define combine-with-space (section string-append <> " " <>))

Then, use reduce to bring all the words together.

> (reduce combine-with-space (list "a" "b" "c"))
> (reduce (section string-append <> " " <>) (list "a" "b" "c"))

To “double” all the strings in a list

E.g., from '("a" "beta" "c") to '("aa" "betabeta" "cc")

Suppose we can double one string, how do we double all of them?

  • Doing something to every element of a list is a case for map.

First, write a procedure that duplicates a string.

(define duplicate-string
  (lambda (str)
    (string-append str str)))
; or
(define duplicate-string
  (lambda (str)
    (regexp-replace* #px"(.*)" str "\\1\\1")))
; or
(define duplicate-string
  (section regexp-replace* #px"(.*)" <> "\\1\\1"))

Then, use map to to double all of the words.

> (map duplicate-string (list "a" "b" "c"))
> (map (lambda (x) (string-append x x)) (list "a" "b" "c"))

To sort a list of strings by length.

First, write a procedure that compares two strings for length

(define shorter?
  (lambda (str1 str2)
    (< (string-length str1) (string-length str2))))

Then, use that with sort to sort the list.

> (sort (list "twas" "brillig" "and" "the" "slithy") shorter?)

To determine if a list contains only odd integers

First, write a procedure that returns true only for odd integers.

(define odd-integer?
  (lambda (val)
    (and (integer? val) (odd? val))))
; or
(define odd-integer? (conjoin integer? odd?))

Then, use it with all to check all of the list.

> (all odd-integer? (list 1 3 5 3 -1))
> (all odd-integer? (list 7 5 4 3 1))
> (all odd-integer? (list 3+3i 4))

About testing

Today is the day we pretend to be “scientists”.

  • Computer scientists design and analyze *algorithms”

Today we apply the scientific method. How?

  • Hypothesis: My algorithm is correct.
  • Experiment: Run it on some inputs, see if you get the expected outputs.
  • Note: Useful to try “edge cases” (also “corner cases”)
  • Lab: Practice thinking about these issues.

Lab

Sam and Fahmida clearly did not test their code. (It’s Sam’s fault, as usual.) The parameters to index-of are backwards. We’ll trust you to fix them.

Note: max and min return inexact numbers if any of their parameters are inexact. (The rounding that happens with an inexact number can lead it to be smaller or bigger than it should be, which means that we can’t be absolutely sure that our comparison is correct. Isn’t that sad?)

Note: check-equal? returns nothing if the check succeeds. That’s so that the failures don’t get buried in a lot of “OK”. See the silly story at the end of the reading.

Note: If you are comparing numbers, please use check-= rather than check-equal?.

Writeup is exercise 7. You can describe the tests in English.

(Sam is Sad. No one listens when he tries to make end-of-class announcements.)