This class will be recorded! Its use is limited to members of the class. Please do not share with others.
Approximate overview
Attend (or watch recording) and send a one-paragraph reflection.
Why “SoLA”?
Set of Learning Assessments.
Also: Part of doremi. A mountain pass (great metaphor, isn’t it)? A plant from which we make pith helmets.
Can I at least say “Sam is evil” or something similar?
Certainly.
But couldn’t you tell from my name? The Rebel from the Sky is …? (Hmmm … How did the folks at Ellis Island feel about my people?)
Why doesn’t it work on “4331 4271 4127 4447”?
I don’t know. Why should it?
“It’s my credit card.”
We need to discuss security and privacy.
I think you got the digits backwards.
Yup. I misread the Luhn algorithm. You work from the right (* 2, add digits) rather than the left. So with an odd number of digits, the leftmost digit is also the (* 2, add digits)
I’ll accept either version.
How do I design a test? The algorithm seems complex to run on its own.
I’d choose some simple, obvious things to start with.
The check-value-computation of “1000 0000 0000 000” is ….
The check-value-computation of “0100 0000 0000 000” is ….
The check-value-computation of “0030 0000 0000 000” is ….
The check-value-computation of “0034 0000 0000 000” is ….
The check-value-computation of “0000 0000 0000 009” is ….
The check-value-computation of “7992 7398 7100 000” is ….
The check-value-computation of “0799 2739 8710 000” is ….
Can we assume that we only have to handle sixteen-digits?
Yup. That’s fifteen digits plus a check digit.
Is it okay that I used a different strategy than Sam suggested?
That’s fine. Sam’s is not the only strategy. As long as you’re doing the alternating “digit as is” and “double then add digits”.
Why use anonymous procedures?
Clean and simple.
May better match how we think about things. “Add 1 and then square.”
(o sqr (section + 1 <>))
Often, you have “one-off” procedures; naming things is a PITA.
Beautiful, small, elegant.
Why is (all char-numeric? (string->list "")) true?
What is
(string->list "")? It’s the empty list.
Are there non-numeric characters in that list? Nope.
So any characters in that list are numbers.
Similar to
(and).
Why is (any char-numeric? (string->list "")) true?
What is
(string->list "")? It’s the empty list.
Are there numeric characters in that list? Nope.
So it’s false.
Similar to
(or).
“Hammers can be dangerous.”
(define median-of-three
(lambda (x y z)
(cond
[(< x y z)
y]
[(< y x z)
x]
[(< x z y)
z]
[(< z x y)
x]
[(< y z x)
z]
[(< z y x)
y]
[else
#f])))
(median-of-three 1 2 3) 2?(when (not (= 2 (median-of-three 1 2 3))) (error "First check failed"))(define median-of-three
(lambda (x y z)
(cond
[(<= x y z)
y]
[(<= y x z)
x]
[(<= x z y)
z]
[(<= z x y)
x]
[(<= y z x)
z]
[(<= z y x)
y]
[else
#f])))
:.,.+20s/</<=/sA better strategy
(define median-of-three
(lambda (x y z)
(list-ref (sort (list x y z) <=) 2)))
We can generalize
(define median
(lambda (lst)
(list-ref (sort lst <=) ...)))
What’s the index of the middle element? Length divided by 2.
(define median
(lambda (lst)
(list-ref (sort lst <=) (/ (length lst) 2))))
Whoops. That failed.
(define median
(lambda (lst)
(list-ref (sort lst <=) (quotient (length lst) 2))))
What’s the median of the list ‘(1 2 3 4)?
Whoops. For even-length lists, we take the two middle elements and average them.
Some things you might have learned from that exercise?
Suppose you hadn’t learned the list operations and compose and section. Is there another way to write “median of three” that’s short and elegant? without conditionals!
Break into smallish groups
And our conclusion is?
(define median-of-three
(lambda (x y z)
(max (min x y) (min x z) (min y z))))
Idea: We’ve eliminated the largest value by taking all of the mins. We’ve eliminated the smallest value by taking the max. Ta dah!
Idea: Add all of them together, subtract the minimum and the maximum.
(define median-of-three
(lambda (x y z)
(- (+ x y z) (min x y z) (max x y z))))
Time to write more tests!
Think about more interesting tests we haven’t done yet.
Final set of code
#lang racket
(require 2htdp/image)
(require csc151)
(require Desktop/csc151-helper)
(define median-of-three-1
(lambda (x y z)
(cond
[(< x y z)
y]
[(< y x z)
x]
[(< x z y)
z]
[(< z x y)
x]
[(< y z x)
z]
[(< z y x)
y]
[else
#f])))
(define median-of-three-2
(lambda (x y z)
(list-ref (sort (list x y z) <=) 1)))
(define median
(lambda (lst)
(list-ref (sort lst <=) (quotient (length lst) 2))))
(define median-of-three-3
(lambda (x y z)
(max (min x y) (min x z) (min y z))))
(define median-of-three
(lambda (x y z)
(- (+ x y z) (min x y z) (max x y z))))
(when (not (equal? 2 (median-of-three 1 2 3)))
(error "First check failed"))
(when (not (equal? 1 (median-of-three 1 2 1)))
(error "Second check failed"))
(when (not (equal? 3 (median-of-three 3 2 4)))
(error "Third check failed"))
(when (not (equal? 7 (median-of-three 7 7 7)))
(error "Fourt median check failed"))
(when (not (equal? 32 (median-of-three 2 732 32)))
(error "Next median check failed"))
(when (not (equal? 0 (median-of-three 0 0 0)))
(error "Next median check failed"))
(when (not (equal? 48 (median-of-three 16 48 92)))
(error "Next median check failed"))
(when (not (equal? 6 (median-of-three 305 -23 6)))
(error "Next median check failed"))
; The following check fails on Sam's computer.
;(when (not (equal? 2 (median-of-three 1111111111111111111111111111111111111.5 2 .0000000000000000000000000000000000000032))))
; (error "Next median check failed"))
(when (not (equal? 2 (median (list 3 1 2))))
(error "First median check failed"))
(when (not (equal? 3 (median (list 1 2 3 4 5))))
(error "Second median check failed"))
(when (not (equal? 4 (median (list 7 3 9 2 4))))
(error "Third median check failed"))
;(when (not (equal? 2.5 (median (list 1 2 3 4))))
; (error "Fourth median check failed"))
(when (not (equal? 7 (median (list 7 7 7))))
(error "Fourt median check failed"))
(when (not (equal? 32 (median (list 2 732 32))))
(error "Next median check failed"))
(when (not (equal? 0 (median (list 0 0 0))))
(error "Next median check failed"))
(when (not (equal? 48 (median (list 16 48 92))))
(error "Next median check failed"))
(when (not (equal? 6 (median (list 305 -23 6))))
(error "Next median check failed"))
Nope. Sam decided that talking through the median problem was more important than today’s lab. We’ll do it tomorrow. Somehow. So no new reading for tomorrow, either.