Permalink
Browse files

add a few problems solved.

  • Loading branch information...
1 parent 052fc1f commit 450e7509bc8489c83d160e4ca3f370fb973742c7 @Liutos committed Feb 7, 2012
Showing with 89 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 pro16.lisp
  3. +9 −0 pro17.lisp
  4. +14 −0 pro20.lisp
  5. +24 −0 pro25.lisp
  6. +14 −0 pro31.lisp
  7. +20 −0 pro40.lisp
View
@@ -0,0 +1 @@
+words.txt
View
@@ -0,0 +1,7 @@
+(defun pro16 (n)
+ (labels ((rec (num sum)
+ (if (zerop num)
+ sum
+ (rec (truncate (/ num 10))
+ (+ sum (rem num 10))))))
+ (rec n 0)))
View
@@ -0,0 +1,9 @@
+(defun pro17 (n)
+ (let ((cnt 0))
+ (loop
+ :for i from 1 upto n
+ :do (incf cnt (count-if #'(lambda (c)
+ (and (char/= #\Space c)
+ (char/= #\- c)))
+ (format nil "~R" i))))
+ cnt))
View
@@ -0,0 +1,14 @@
+(defun fact (n)
+ (labels ((rec (m prod)
+ (if (= 1 m)
+ prod
+ (rec (- m 1) (* prod m)))))
+ (rec n 1)))
+
+(defun pro20 (n)
+ (labels ((rec (num sum)
+ (if (zerop num)
+ sum
+ (rec (truncate (/ num 10))
+ (+ sum (rem num 10))))))
+ (rec n 0)))
View
@@ -0,0 +1,24 @@
+(defun fib (n)
+ (labels ((rec (a b m)
+ (if (= 2 m)
+ b
+ (rec b (+ a b) (- m 1)))))
+ (if (= 1 n)
+ 1
+ (if (= 2 n)
+ 1
+ (rec 1 1 n)))))
+
+(defun num-length (n)
+ (labels ((rec (num len)
+ (if (zerop num)
+ len
+ (rec (truncate (/ num 10)) (1+ len)))))
+ (rec n 0)))
+
+(defun pro25 (len)
+ (labels ((rec (term)
+ (if (>= (num-length (fib term)) len)
+ term
+ (rec (1+ term)))))
+ (rec 13)))
View
@@ -0,0 +1,14 @@
+(defun coins-value (n)
+ (nth (1- n) '(1 2 5 10 20 50 100)))
+
+(defun cons-coins (amount kinds-of-coins)
+ (cond ((= amount 0) 1)
+ ((or (< amount 0) (= kinds-of-coins 0)) 0)
+ (t (+ (cons-coins amount
+ (1- kinds-of-coins))
+ (cons-coins (- amount
+ (coins-value kinds-of-coins))
+ kinds-of-coins)))))
+
+(defun pro31 ()
+ (cons-coins 200 7))
View
@@ -0,0 +1,20 @@
+(defun num-length (num)
+ (labels ((rec (n len)
+ (if (<= n 0)
+ len
+ (rec (truncate (/ n 10)) (1+ len)))))
+ (rec num 0)))
+
+(defun helper (n)
+ (labels ((rec (index test-num)
+ (let ((len (num-length test-num)))
+ (if (<= index len)
+ (- (char-code (char (format nil "~D" test-num) (1- index)))
+ (char-code #\0))
+ (rec (- index len) (1+ test-num))))))
+ (rec n 1)))
+
+(defun pro40 ()
+ (apply #'* (mapcar #'helper (mapcar #'(lambda (e)
+ (expt 10 e))
+ '(0 1 2 3 4 5 6)))))

0 comments on commit 450e750

Please sign in to comment.