-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler17.lisp
72 lines (65 loc) · 2.09 KB
/
euler17.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;;;; Problem 17 - Number letter counts
;;; If the numbers 1 to 5 are written out in words: one, two, three, four,
;;; five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
;;;
;;; If all the numbers from 1 to 1000 (one thousand) inclusive were
;;; written out in words, how many letters would be used?
;;;
;;; NOTE: Do not count spaces or hyphens. For example, 342 (three hundred
;;; and forty-two) contains 23 letters and 115 (one hundred and fifteen)
;;; contains 20 letters. The use of "and" when writing out numbers is in
;;; compliance with British usage.
(defun numberwords (limit)
(let ((tmp ))
(loop for i from 1 to limit
do (push (format nil "~R" i) tmp))
tmp))
(defun sum-string (s)
"sum the amount of chars, minus whitespace"
(let ((tmp )
(total 0))
(setf tmp (remove #\- s))
(setf tmp (remove #\Space tmp))
(setf total (length tmp))
(if (search "hundred" tmp)
(setf total (+ total 3)))
total))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; This is a new attempt
(defun string-with-and (p)
(let* ((number-string (format nil "~R" p))
(start-value 0)
(end-value (length number-string))
(space-value (position #\Space (format nil "~R" p) :from-end t))
(combined-string
(concatenate 'string
(subseq number-string start-value space-value)
" and"
(subseq number-string space-value end-value))))
combined-string))
(defun number-of-words (limit)
"Creating a list of words, adding in \"and\" when needed"
(let ((tmp ))
(loop :for i :from 1 :to limit
:do (cond
((= 0 (mod i 100))
(push (format nil "~R" i) tmp))
((> i 100)
(push (string-with-and i) tmp))
(t
(push (format nil "~R" i) tmp))))
tmp))
;;; also used in recursive attempt
(defun sum-words (w)
"Recurse over a list of words, summing all chars."
(let ((tmp 0))
(labels ((summer (n)
;; end of list?
(if (null n)
nil
;; if not, add the sum of chars and recurse to next
(progn
(setf tmp (+ tmp (sum-string (car n))))
(summer (cdr n))))))
(summer w))
tmp))