HW5 + Solutions

posted Oct 2, 2012, 11:26 AM by Samuel Konstantinovich   [ updated Oct 8, 2012, 10:29 PM ]
Due Friday October 5th 11:59 pm on the homework server.

1.Boolean functions: (do not use if/cond and have it return #t and #f. Instead write a boolean expression for the function!)
1a. (xor a b) is true when a and b have different truth values, it is false when a and b are the same. 
Truth table for all possible combinations:
(xor #t #t) is #f
(xor #t #f) is #t
(xor #f #t) is #t
(xor #f #f) is #f

1b. (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 
 
3. Write a scheme function sumFromAtoB that takes two integers a and b, assume that b is larger than a. The function calculates the sum of the integers from a to be inclusive.

outputs:
 (sumFromAtoB 10 12) returns 33
 (sumFromAtoB 100 100) returns 100
 (sumFromAtoB 1 9) returns 45

4. Write a scheme function (sumOfPrimeDigits x) that looks at all of the digits of an integer x, and adds up the prime ones. A helper function would be useful to make your code cleaner by checking if the a one digit number is prime. (You need the lesson on Wednesday  for this)
(sumOfPrimeDigits 123) returns 5.
(sumOfPrimeDigits 223) returns 7.
(sumOfPrimeDigits 12345) returns 10.
(sumOfPrimeDigits 98762) returns 9.


;SOLUTIONS HW5:
;1a (xor a b)

;a or b, but NOT both...
(define (xor a b) 
  (and (or a b) (not (and a b))))

;using equals, they can't be the same:
(define (xor a b)
  (not (equal? a b)))

;1b

(define (leapyear y)
  (and 
   (= 0 (remainder y 4))    ;it is divisible by 4 AND...
   (or                      ;not divisible by 100 unless it is also divisible by 400
       (not (= 0 (remainder y 100))) 
       (= 0 (remainder y 400)))))

;2. 
;using remainders:
(define (militaryTime t ampm)
  (if (equal? ampm "AM")
      (remainder t 12)
      (+ 12 (remainder t 12))))

;using a cond
(define (militaryTime t ampm)
  (cond
    ( (and (= t 12) (equal? ampm "AM")) 0) ;12am is special
    ( (= t 12) 12) ;12pm is special
    ( (equal? ampm "AM") t) ;normal AM
    ( else (+ t 12))));normal PM

;3
(define (sumfromAtoB a b)
  (if (> a b)
      0
      (+ a (sumfromAtoB (+ a 1) b))))

;4
;make a function for single digit primes:

;1st way:
(define (isPrime? x)
  (or (= x 2)(= x 3)(= x 5) (= x 7)))

(define (sumOfPrimeDigits n)
  (cond
    ( (and (< n 10) (isPrime? n)) n)
    ( (< n 10) 0)
    ( (isPrime? (remainder n 10))  (+ (sumOfPrimeDigits (quotient n 10))(remainder n 10) ))
    ( else (sumOfPrimeDigits (quotient n 10)))))


;2nd way
(define (isPrime? x)
  (or (= x 2)(= x 3)(= x 5) (= x 7)))

(define (valueOfDigit x)
  (if (isPrime? x)
      x
      0))

(define (sumOfPrimeDigits n)
  (if
    (< n 10) 
    (valueOfDigit n)
    (+ (sumOfPrimeDigits (quotient n 10)) (valueOfDigit (remainder n 10)) )))
    

;Tests:
(display "xor\n")
(display "#f=")(xor #t #t)
(display "#f=")(xor #f #f)
(display "#t=")(xor #t #f)
(display "#t=")(xor #f #t)
(display "leapyear\n")
(display "#t=")(leapyear 2000)
(display "#t=")(leapyear 2004)
(display "#t=")(leapyear 2400)
(display "#f=")(leapyear 2500)
(display "#f=")(leapyear 2405)
(display "militaryTime\n")
(display "0=")(militaryTime 12 "AM")
(display "1=")(militaryTime 1 "AM")
(display "12=")(militaryTime 12 "PM")
(display "13=")(militaryTime 1 "PM")
(display "sum\n")
(display "15=")(sumfromAtoB 1 5)
(display "5=")(sumfromAtoB 5 5)
(display "45=")(sumfromAtoB 5 10)
(display "Sum\n")
(display "10=")(sumOfPrimeDigits 12345)
(display "8=")(sumOfPrimeDigits 11345)
(display "17=")(sumOfPrimeDigits 9123456789)
Comments