10-03-2013 - HW*

posted Oct 3, 2013, 5:12 AM by Samuel Konstantinovich   [ updated Oct 9, 2013, 7:55 PM ]
Do Now:
a) While you do the rest of your do now on paper, Open your lab from yesterday, run your test cases and show me your output. No typing other than logging in and running drracket. If you didn't finish the lab then focus on the do now.

b) Write two scheme functions. Format them so that they are easy to read! If you write them on one line, that is not acceptable. Answer the question after the 2nd function.

1. (biconditional a b) takes Boolean inputs a, b and returns true if both have the same truth value, false otherwise.
e.g.,
(biconditional #t #t) → true
(biconditional #t #f) → false
(biconditional #f #t) → false
(biconditional #f #f) → true

2. (XOR3 a b c) takes Boolean inputs a, b, c and returns true if exactly one input is true, false otherwise.
e.g.,
(XOR3 #t #t #t) → false
(XOR3 #t #t #f) → false
(XOR3 #t #f #f) → true

Q: What additional test cases are necessary for XOR3?





Homework: 
Submit this on the homework server by Monday October 7th 11:59pm.

You may have to research how military time works...
1. (leapyear year) returns #t when a year is a leap year, #f when it is not. The rules of leap years are:
years that are multiples of 4 are leap years, except that every year that is a multiple of 100 is not a leap year , unless it is also a multiple of 400 then it is.
outputs:
(leapyear 2000) #t
(leapyear 1900) #f
(leapyear 1904) #t 
(leapyear 2103) #f
(leapyear 2104) #t

2. Write a military time calculator: (militaryTime hour ampm) where hour is an integer from 1 to 12 inclusive, and ampm is either "AM" or "PM". Use (equal? x y) to compare non numerical things to see if they are the same, (equal? word "AMBER") will be true when the variable word stores the string "AMBER" false otherwise.  (Look at a military time wiki if you aren't sure about the rules)

outputs: 
(militaryTime 12 "PM") returns 12 
(militaryTime 8 "PM") returns 20 
(militaryTime 5 "AM") returns 5
(militaryTime 12 "AM") returns 0 

Sample Solutions:
(define (leapyear year) 
  (or
   (= (remainder year 400) 0)
   (and 
    (not (= (remainder year 100) 0)) 
    (= (remainder year 4) 0) 
    )))


(define (militaryTime h ampm)
  (cond 
    ( (and (equal? ampm "AM") (= h 12)) 0 )
    ( (and (equal? ampm "PM") (= h 12)) 12 )
    ( (equal? ampm "PM") (+ h 12))
    ( else h)))

;ALTERNATE SOLUTION!      

(define (militaryTime h ampm)
  (if (equal? ampm "AM")
      (remainder h 12)
      (+ (remainder h 12) 12)))
 
Comments