Google Answers Logo
View Question
 
Q: DRScheme problem need quick answers to see, if I understand the way to answer ! ( Answered,   0 Comments )
Question  
Subject: DRScheme problem need quick answers to see, if I understand the way to answer !
Category: Computers > Programming
Asked by: kenobob-ga
List Price: $20.00
Posted: 19 Oct 2005 22:38 PDT
Expires: 18 Nov 2005 21:38 PST
Question ID: 582452
Please include a comment with each solution indicating the amount of
time spent solving it.

Note:  Using the two access functions of (units-digit) and
(all-but-units-digit) and/or substitution or recursion functions.

(decimal-length n)  ; returns the number of digits in the number.  
EXAMPLE:  (decimal-length 348567)  -->  6

(ith-digit n i)  ; returns digit which is the ith from the left.  
EXAMPLE:  (ith-digit 471835629 4)  --> 8

(leading-digit n)  ; returns the first digit.  
EXAMPLE:  (leading-digit 5394)  -->  5

(occurance d n)  ; returns the number of times digit d occurs in n.  
EXAMPLE:  (occurance 6 549457654987587654655478563)  -->  4

(digit-sum n)  ; returns the sum of the digits in n.  
EXAMPLE:  (digit-sum 2354)  -->  14,   (digit-sum 8)  --> 8

(digital-root n)  ; returns the result of repeatedly applying
digit-sum until the result is a single digit.
EXAMPLE:  (digital-root 341943)  -->  (via 3+4+1+9+4+3=4, then 2+4=6)

(backwards n)  ; returns the digits in reverse order.  You do not need
to handle leading or trailing zeros.  For this problem you may use
multiplication.
EXAMPLE:  (backwards 329145)  -->  541923
Answer  
Subject: Re: DRScheme problem need quick answers to see, if I understand the way to answe
Answered By: leapinglizard-ga on 20 Oct 2005 00:56 PDT
 
Dear kenobob,

Here you go.


(define (decimal-length n)      ; 4 minutes
  (if (< n 10)
    1
    (+ 1 (decimal-length (all-but-units-digit n)))))

(define (ith-digit n i)         ; 3 minutes
  (if (= i (decimal-length n))
    (units-digit n)
    (ith-digit (all-but-units-digit n) i)))

(define (leading-digit n)       ; 1 minute
  (ith-digit n 1))

(define (occurrence d n)        ; 6 minutes
  (+ (if (= (units-digit n) d)
       1
       0)
     (if (< n 10)
       0
       (occurrence d (all-but-units-digit n)))))

(define (digit-sum n)           ; 2 minutes
  (+ (units-digit n)
     (if (< n 10)
       0
       (digit-sum (all-but-units-digit n)))))

(define (digital-root n)        ; 1 minute
  (let ((x (digit-sum n)))
    (if (< x 10)
      x
      (digital-root x))))

(define (backwards n)           ; 8 minutes
  (letrec ((backward
             (lambda (n part)
               (if (= n 0)
                 part
                 (backward (all-but-units-digit n)
                           (+ (* 10 part) (units-digit n)))))))
    (backward n 0)))


If you don't already have definitions for units-digit and
all-but-units-digit, you can insert the following before all the above
definitions.


(define (units-digit x)
  (modulo x 10))

(define (all-but-units-digit x)
  (/ (- x (units-digit x)) 10))


Regards,

leapinglizard
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy