2015-10-20

posted Oct 20, 2015, 4:53 AM by Samuel Konstantinovich   [ updated Oct 20, 2015, 8:51 AM ]
Goal: Recursive splitting of numbers.

Quiz Thursday *Does not include recurison*

Do now: 
1. How can you take an integer X and calculate the last digit? The number can be any integer.
927 -> 7
532 -> 2
29 -> 9
etc.
2. How can you take an integer X and calculate everything except the last digit?
921 -> 92
532 -> 53
29 -> 2

Do in class together:
;(numDigits n) - count the digits of the integer
;(numDigits 3) -> 1
;(numDigits 43) -> 2
;(numDigits 300) -> 3
;What is a good base case? 
;How can we handle the numbers that are bigger than the base case?

(define numDigits(lambda (n)
                   (cond 
                     ( (< n 0) (numDigits (- n)))
                     ( (< n 10) 1 )
                     ( else (+ 1 (numDigits (quotient n 10)))))))

;Lab Recursion1

;(sumDigits n) - add the value of each of the digits of the integer
;(sumDigits 3) -> 3
;(sumDigits 43) -> 7
;(sumDigits 352) -> 10
;What is a good base case? 
;*Careful here:
;What else needs to be done now that we didn't need to do in the last problem?

;(countEvenDigits n) - how many of the digits of the number are even?
;(countEvenDigits 3) -> 0
;(countEvenDigits 43) -> 1
;(countEvenDigits 352) -> 1
;(countEvenDigits 23452) -> 3
;How is the base case different now?
;*Careful here:
;How do you check if something is even?
;This problem requires looking at each digit and deciding what to do...
Comments