[ create a new paste ] login | about

Link: http://codepad.org/G17lhMD3    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on May 12:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
; three interview questions

(define (subtract-first-half xs)
  (let loop ((front xs) (back (reverse xs)) (hare xs) (out (list)))
    (if (or (null? hare) (null? (cdr hare)))
        (append (reverse out) front)
        (loop (cdr front) (cdr back) (cddr front)
              (cons (- (car front) (car back)) out)))))

(display (subtract-first-half '(5 4 3 2 1))) (newline)
(display (subtract-first-half '(4 3 2 1))) (newline)

(define (trailing-zeros n)
  (let loop ((k 5) (z 0))
    (if (< n k) z
      (loop (* k 5) (+ z (quotient n k))))))

(display (trailing-zeros 487)) (newline)


Output:
1
2
3
(4 2 0 2 1)
(3 1 2 1)
119


Create a new paste based on this one


Comments: