Skip to main content

CSC 151.01, Class 14: Discussion of exam 1

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Common issues
  • Problem 2
  • Problem 5
  • Problem 6
  • Problem 4

News / Etc.

  • New partners.
  • Please make sure to return your computer cards to the jar.
  • I’m glad to be back; I’m sorry that I missed classes.
  • 70 exams x 6 problems + admin x 5 min = approx 2400 minutes or 40 hours. I’ll get exams back to you next Monday.

Upcoming Work

Extra credit (Academic/Artistic)

  • CS Table, Tuesday: TBD
  • Google technical résumé talk, Tuesday at 4pm, Science 3821
  • Convocation, Thursday: Wilson Okello on “Living in the Wake of Crisis”

Extra credit (Peer)

  • Volleyball Wednesday night

Extra Credit (Misc)

None at the moment.

Other Good Things

  • GHS Homecoming - Near GHS on Thursday at 4pmish.

Questions

Common issues

  • When to ask for help. Early and often.
  • Work on the six-P style documentation. The form is important. Postconditions tell you not just type, but value.
  • What got extra credit?
    • Really nice tests.
    • The occasional good joke.
    • Some implementations that made me say “cool”
  • “There’s more to life” belongs only on your cover sheet.
  • I really do care that you use the template file.
  • Formatting. Please hit ctrl-i before submitting.

Problem 2

Can the upper bound be equal to length + 1? Let’s see … In a list of length 5, the valid indices are 0 1 2 3 4. It looks like the last legal index is 4, and the thing right after that is 5, which is (length lst).

The postconditions are a contract. If you choose postconditions that can be met without solving the real intent of the problem, they probably don’t suffice. For example, (length newlst) <= (length lst) can be achieved with (define (sublist lst lb ub) null).

;;; Procedure:
;;;   sublist
;;; Parameters:
;;;   lst, a list
;;;   lb, a non-negative integer
;;;   ub, a non-negative integer
;;; Purpose:
;;;   Select the sublist starting at lb and ending
;;;   just before ub.
;;; Produces:
;;;   newlst, a list
;;; Preconditions:
;;;   0 <= lb <= ub <= (length lst)
;;; Postconditions:
;;;   * (length newlst) = (- ub lb)
;;;   * The elements in newlst correspond to a sequence
;;;     of elements in lst, starting at pos lb.  That is,
;;;     for all reasonable indices, i, (0 <= i < (length newlst)
;;;     (list-ref newlst i) = (list-ref lst (+ i lb))
;;;     Alternately:
;;;       The value at position 0 in newlst is the
;;;         same as the value at position lb in lst
;;;       The value at position 1 in newlst is the
;;;         same as the value at position lb+1 in lst
;;;       And so on and so forth
;;; Process:
;;;   We're going to want to chop away stuff at the
;;;   start and the finish.  We have two procedures
;;;   that easily remove stuff from the list: drop
;;;   and take
(define sublist
  (lambda (lst lb ub)
    (take (drop lst lb) (- ub lb))))

(define sl
  (lambda (lst lb ub)
    (drop (take lst ub) lb)))

Problem 5

We’ll walk through one approach to solve it. There were others.

; Citations:
;   Took code and ideas from the lab on unit testing
;   available at
;   <http://www.cs.grinnell.edu/~rebelsky/Courses/CSC151/2017F/01/labs/rackunit>

; Solution:

;;; Procedure:
;;;   drop-to-first-zero
;;; Parameters:
;;;   lst, a list of numbers
;;; Purpose:
;;;   Removes all of the elements up to and including the first zero.
;;; Produces:
;;;   newlst, a list of numbers
;;; Preconditions:
;;;   The list contains at least one zero.
;;; Postconditions:
;;;   Suppose the first zero is at index z.
;;;     (length newlst) = (- (length lst) z 1)
;;;     For all i s.t. z < i < (length lst)
;;;       (list-ref newlst (- i z 1)) = (list-ref lst z)
(define drop-to-first-zero
  (lambda (lst)
    (drop lst
          (+ 1 (index-of 0 lst)))))


(define remove-odds
  (lambda (lst)
    (drop-to-first-zero (sort (append (list 0) lst)
                              (lambda (a b)
                                (odd? a))))))

We also added a test case.

; Citations:
;   Took code and ideas from the lab on unit testing
;   available at
;   <http://www.cs.grinnell.edu/~rebelsky/Courses/CSC151/2017F/01/labs/rackunit>

; Solution:

;;; Procedure:
;;;   drop-to-first-zero
;;; Parameters:
;;;   lst, a list of numbers
;;; Purpose:
;;;   Removes all of the elements up to and including the first zero.
;;; Produces:
;;;   newlst, a list of numbers
;;; Preconditions:
;;;   The list contains at least one zero.
;;; Postconditions:
;;;   Suppose the first zero is at index z.
;;;     (length newlst) = (- (length lst) z 1)
;;;     For all i s.t. z < i < (length lst)
;;;       (list-ref newlst (- i z 1)) = (list-ref lst z)
(define drop-to-first-zero
  (lambda (lst)
    (drop lst
          (+ 1 (index-of 0 lst)))))


(define remove-odds
  (lambda (lst)
    (drop-to-first-zero (sort (append (list 0) lst)
                              (lambda (a b)
                                (odd? a))))))

Problem 6

We’ll walk through one approach to solve it. There were others.

(define describe-teaching-0
  (lambda (person)
    (string-append (list-ref person 2)
                   " "
                   (list-ref person 1)
                   " "
                   (list-ref person 0)
                   " is teaching ")))

; The mantra: When confronted with a list, solve problems
; on one or two elements and then use `map`, `reduce`,
; `filter`, `sort`, ...
; * To combine a list of strings into a single string, I'll
;   use reduce.
; * reduce expects a binary procedure and a list
; * we want to join things with `and`

;;; Procedure:
;;;   join-with-and
;;; Parameters:
;;;   this, a string
;;;   that, a string
;;; Purpose:
;;;   Shove "and" between the two strings
;;; Produces:
;;;   thisandthat, a string
(define join-with-and
  (lambda (this that)
    (string-append this " and " that)))

(define describe-teaching
  (lambda (person)
    (string-append (list-ref person 2)
                   " "
                   (list-ref person 1)
                   " "
                   (list-ref person 0)
                   " is teaching "
                   (reduce join-with-and
                           (list-ref person 3)))))

Problem 4

Why is this not a sufficient postcondition?

;;; Postconditions:
;;;   (length no-odds-lst) <= (length lst)

What should you test? Here are some things.

  • Empty list.
  • List with repeated even elements.
  • List with repeated odd elements.
  • List with negative numbers.
  • List with only odd numbers
  • List with odds at the beginning.
  • List with odds in the middle.
  • List with odds at the end.
  • Very large and very small elements.
  • Really long lists

[Sam forgets that class ends at 9:50, not 9:20, and lets folks out early.]