Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
phoe committed May 9, 2020
1 parent f3ed44b commit 30dd961
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 154 deletions.
85 changes: 46 additions & 39 deletions koans-solved/clos.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@
(setf (slot-value car-1 'speed) 220)
(setf (slot-value car-2 'color) :blue)
(setf (slot-value car-2 'speed) 240)
(assert-equal ____ (slot-value car-1 'color))
(assert-equal ____ (slot-value car-2 'speed))))
(assert-equal :red (slot-value car-1 'color))
(assert-equal 240 (slot-value car-2 'speed))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Common Lisp predefines the symbol SPEED in the COMMON-LISP package, which
;;; means that we cannot define a function named after it. The function SHADOW
;;; creates a new symbol with the same name in the current package and shadows
;;; the predefined one within the current package.

(shadow 'speed)

(defclass spaceship ()
;; It is possible to define reader, writer, and accessor functions for slots.
((color :reader color :writer (setf color))
(speed :accessor color)))
(speed :accessor speed)))

;;; Specifying a reader function named COLOR is equivalent to
;;; (DEFMETHOD COLOR ((OBJECT SPACECSHIP)) ...)
Expand All @@ -47,20 +54,20 @@
(let ((ship (make-instance 'spaceship)))
(setf (color ship) :orange
(speed ship) 1000)
(assert-equal ____ (color ship))
(assert-equal ____ (speed ship))))
(assert-equal :orange (color ship))
(assert-equal 1000 (speed ship))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defclass bike ()
;; It is also possible to define initial arguments for slots.
((color :reader color :initarg :color)
(speed :reader color :initarg :color)))
(speed :reader speed :initarg :speed)))

(define-test initargs
(let ((bike (make-instance 'bike :color :blue :speed 30)))
(assert-equal ____ (color bike))
(assert-equal ____ (speed bike))))
(assert-equal :blue (color bike))
(assert-equal 30 (speed bike))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand All @@ -84,15 +91,15 @@
:favorite-lisp-implementation :sbcl))
(adam (make-instance 'c-programmer
:name :adam
:favorite-c-compiler :llvm)))
(assert-equal ____ (person-name jack))
(assert-equal ____ (person-name bob))
(assert-equal ____ (favorite-lisp-implementation bob))
(assert-equal ____ (person-name adam))
(assert-equal ____ (favorite-c-compiler adam))
(true-or-false? ____ (typep bob 'person))
(true-or-false? ____ (typep bob 'lisp-programmer))
(true-or-false? ____ (typep bob 'c-programmer))))
:favorite-c-compiler :clang)))
(assert-equal :jack (person-name jack))
(assert-equal :bob (person-name bob))
(assert-equal :sbcl (favorite-lisp-implementation bob))
(assert-equal :adam (person-name adam))
(assert-equal :clang (favorite-c-compiler adam))
(true-or-false? t (typep bob 'person))
(true-or-false? t (typep bob 'lisp-programmer))
(true-or-false? nil (typep bob 'c-programmer))))

;;; This includes multiple inheritance.

Expand All @@ -103,13 +110,13 @@
:name :zenon
:favorite-lisp-implementation :clisp
:favorite-c-compiler :gcc)))
(assert-equal ____ (person-name zenon))
(assert-equal ____ (favorite-lisp-implementation zenon))
(assert-equal ____ (favorite-c-compiler zenon))
(true-or-false? ____ (typep zenon 'person))
(true-or-false? ____ (typep zenon 'lisp-programmer))
(true-or-false? ____ (typep zenon 'c-programmer))
(true-or-false? ____ (typep zenon 'embeddable-common-lisp-programmer))))
(assert-equal :zenon (person-name zenon))
(assert-equal :clisp (favorite-lisp-implementation zenon))
(assert-equal :gcc (favorite-c-compiler zenon))
(true-or-false? t (typep zenon 'person))
(true-or-false? t (typep zenon 'lisp-programmer))
(true-or-false? t (typep zenon 'c-programmer))
(true-or-false? t (typep zenon 'clisp-programmer))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand All @@ -133,17 +140,17 @@

(define-test greeting-chatbot ()
(let ((chatbot (make-instance 'greeting-chatbot :version "1.0.0")))
(true-or-false? ____ (typep chatbot 'greeting-mixin))
(true-or-false? ____ (typep chatbot 'chatbot))
(true-or-false? ____ (typep chatbot 'greeting-chatbot))
(assert-equal ____ (greet chatbot "Tom"))
(assert-equal ____ (greeted-people chatbot))
(assert-equal ____ (greet chatbot "Sue"))
(assert-equal ____ (greet chatbot "Mark"))
(assert-equal ____ (greet chatbot "Kate"))
(assert-equal ____ (greet chatbot "Mark"))
(assert-equal ____ (greeted-people chatbot))
(assert-equal ____ (version chatbot))))
(true-or-false? t (typep chatbot 'greeting-mixin))
(true-or-false? t (typep chatbot 'chatbot))
(true-or-false? t (typep chatbot 'greeting-chatbot))
(assert-equal "Hello, Tom." (greet chatbot "Tom"))
(assert-equal '("Tom") (greeted-people chatbot))
(assert-equal "Hello, Sue." (greet chatbot "Sue"))
(assert-equal "Hello, Mark." (greet chatbot "Mark"))
(assert-equal "Hello, Kate." (greet chatbot "Kate"))
(assert-equal "Hello, Mark." (greet chatbot "Mark"))
(assert-equal '("Kate" "Mark" "Sue" "Tom") (greeted-people chatbot))
(assert-equal "1.0.0" (version chatbot))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand All @@ -168,7 +175,7 @@
(antonio (make-instance 'italian))
(roy (make-instance 'stereotypical-person))
(mary (make-instance 'another-stereotypical-person)))
(assert-equal ____ (stereotypical-food james))
(assert-equal ____ (stereotypical-food antonio))
(assert-equal ____ (stereotypical-food roy))
(assert-equal ____ (stereotypical-food mary))))
(assert-equal :burger (stereotypical-food james))
(assert-equal :pasta (stereotypical-food antonio))
(assert-equal :burger (stereotypical-food roy))
(assert-equal :pasta (stereotypical-food mary))))
89 changes: 57 additions & 32 deletions koans-solved/format.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -22,63 +22,88 @@
(define-test format-basic
;; If there are no format directives in the string, FORMAT will return
;; a string that is STRING= to its format control.
(assert-equal ____ (format nil "Lorem ipsum dolor sit amet")))
(assert-equal "Lorem ipsum dolor sit amet"
(format nil "Lorem ipsum dolor sit amet")))

(define-test format-aesthetic
;; The ~A format directive creates aesthetic output.
(assert-equal ____ (format nil "This is the number ~A" 42))
(assert-equal ____ (format nil "This is the keyword ~A" :foo))
(assert-equal ____ (format nil "~A evaluates to ~A"
'(/ 24 (- 3 (/ 8 3)))
(/ 24 (- 3 (/ 8 3)))))
(assert-equal ____ (format nil "This is the character ~A" #\C))
(assert-equal ____ (format nil "In a ~A" "galaxy far far away")))
(assert-equal "This is the number 42"
(format nil "This is the number ~A" 42))
(assert-equal "This is the keyword FOO"
(format nil "This is the keyword ~A" :foo))
(assert-equal "(/ 24 (- 3 (/ 8 3))) evaluates to 72"
(format nil "~A evaluates to ~A"
'(/ 24 (- 3 (/ 8 3)))
(/ 24 (- 3 (/ 8 3)))))
(assert-equal "This is the character C"
(format nil "This is the character ~A" #\C))
(assert-equal "In a galaxy far far away"
(format nil "In a ~A" "galaxy far far away")))

(define-test format-standard
;; The ~S format directive prints objects with escape characters.
;; Not all Lisp objects require to be escaped.
(assert-equal ____ (format nil "This is the number ~S" 42))
(assert-equal ____ (format nil "~S evaluates to ~S"
'(/ 24 (- 3 (/ 8 3)))
(/ 24 (- 3 (/ 8 3)))))
(assert-equal "This is the number 42" (format nil "This is the number ~S" 42))
(assert-equal "(/ 24 (- 3 (/ 8 3))) evaluates to 72"
(format nil "~S evaluates to ~S"
'(/ 24 (- 3 (/ 8 3)))
(/ 24 (- 3 (/ 8 3)))))
;; Keywords are printed with their leading colon.
(assert-equal ____ (format nil "This is the keyword ~S" :foo))
(assert-equal "This is the keyword :FOO"
(format nil "This is the keyword ~S" :foo))
;; Characters are printed in their #\X form. The backslash will need to be
;; escaped inside the printed string, just like in "#\\X".
(assert-equal ____ (format nil "This is the character ~S" #\C))
(assert-equal "This is the character #\\C"
(format nil "This is the character ~S" #\C))
;; Strings include quote characters, which must be escaped:
;; such a string might look in code like "foo \"bar\"".
(assert-equal ____ (format nil "In a ~S" "galaxy far far away")))
(assert-equal "In a \"galaxy far far away\""
(format nil "In a ~S" "galaxy far far away")))

(define-test format-radix
;; The ~B, ~O, ~D, and ~X radices print numbers in binary, octal, decimal, and
;; hexadecimal notation.
(assert-equal ____ (format nil "This is the number ~B" 42))
(assert-equal ____ (format nil "This is the number ~O" 42))
(assert-equal ____ (format nil "This is the number ~D" 42))
(assert-equal ____ (format nil "This is the number ~X" 42))
(assert-equal "This is the number 101010"
(format nil "This is the number ~B" 42))
(assert-equal "This is the number 52"
(format nil "This is the number ~O" 42))
(assert-equal "This is the number 42"
(format nil "This is the number ~D" 42))
(assert-equal "This is the number 2A"
(format nil "This is the number ~X" 42))
;; We can specify a custom radix by using the ~R directive.
(assert-equal ____ (format nil "This is the number ~3R" 42))
(assert-equal "This is the number 1120"
(format nil "This is the number ~3R" 42))
;; It is possible to print whole forms this way.
(let ((form '(/ 24 (- 3 (/ 8 3))))
(result (/ 24 (- 3 (/ 8 3)))))
(assert-equal ____ (format nil "~B evaluates to ~B" form result))
(assert-equal ____ (format nil "~O evaluates to ~O" form result))
(assert-equal ____ (format nil "~D evaluates to ~D" form result))
(assert-equal ____ (format nil "~X evaluates to ~X" form result))
(assert-equal ____ (format nil "~3R evaluates to ~3R" form result))))
(assert-equal "(/ 11000 (- 11 (/ 1000 11))) evaluates to 1001000"
(format nil "~B evaluates to ~B" form result))
(assert-equal "(/ 30 (- 3 (/ 10 3))) evaluates to 110"
(format nil "~O evaluates to ~O" form result))
(assert-equal "(/ 24 (- 3 (/ 8 3))) evaluates to 72"
(format nil "~D evaluates to ~D" form result))
(assert-equal "(/ 18 (- 3 (/ 8 3))) evaluates to 48"
(format nil "~X evaluates to ~X" form result))
(assert-equal "(/ 220 (- 10 (/ 22 10))) evaluates to 2200"
(format nil "~3R evaluates to ~3R" form result))))

(define-test format-iteration
;; The ~{ and ~} directives iterate over a list.
(assert-equal ____ (format nil "~{[~A]~}" '(1 2 3 4 5 6)))
(assert-equal ____ (format nil "~{[~A ~A]~}" '(1 2 3 4 5 6)))
(assert-equal "[1][2][3][4][5][6]" (format nil "~{[~A]~}" '(1 2 3 4 5 6)))
(assert-equal "[1 2][3 4][5 6]" (format nil "~{[~A ~A]~}" '(1 2 3 4 5 6)))
;; The directive ~^ aborts iteration when no more elements remain.
(assert-equal ____ (format nil "~{[~A]~^, ~}" '(1 2 3 4 5 6))))
(assert-equal "[1], [2], [3], [4], [5], [6]"
(format nil "~{[~A]~^, ~}" '(1 2 3 4 5 6))))

(define-test format-case
;; The ~( and ~) directives adjust the string case.
(assert-equal ____ (format nil "~(~A~)" "The QuIcK BROWN fox"))
(assert-equal "the quick brown fox"
(format nil "~(~A~)" "The QuIcK BROWN fox"))
;; Some FORMAT directives can be further adjusted with the : and @ modifiers.
(assert-equal ____ (format nil "~:(~A~)" "The QuIcK BROWN fox"))
(assert-equal ____ (format nil "~@(~A~)" "The QuIcK BROWN fox"))
(assert-equal ____ (format nil "~:@(~A~)" "The QuIcK BROWN fox")))
(assert-equal "The Quick Brown Fox"
(format nil "~:(~A~)" "The QuIcK BROWN fox"))
(assert-equal "The quick brown fox"
(format nil "~@(~A~)" "The QuIcK BROWN fox"))
(assert-equal "THE QUICK BROWN FOX"
(format nil "~:@(~A~)" "The QuIcK BROWN fox")))
17 changes: 16 additions & 1 deletion koans-solved/scoring-project.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@
;;;
;;; Your goal is to write the scoring function for Greed.

(defun score-once (&rest dice)
(let ((sorted (sort (copy-list dice) #'<)))
(cond ((search '(1 1 1) sorted) (list 1000 (remove 1 sorted :count 3)))
((search '(2 2 2) sorted) (list 200 (remove 2 sorted :count 3)))
((search '(3 3 3) sorted) (list 300 (remove 3 sorted :count 3)))
((search '(4 4 4) sorted) (list 400 (remove 4 sorted :count 3)))
((search '(5 5 5) sorted) (list 500 (remove 5 sorted :count 3)))
((search '(6 6 6) sorted) (list 600 (remove 6 sorted :count 3)))
((find 5 sorted) (list 50 (remove 5 sorted :count 1)))
((find 1 sorted) (list 100 (remove 1 sorted :count 1)))
(t (list 0 '())))))

(defun score (&rest dice)
____)
(loop for current-dice = dice then remaining-dice
for (score remaining-dice) = (apply #'score-once current-dice)
sum score
while remaining-dice))

(define-test score-of-an-empty-list-is-zero
(assert-equal 0 (score)))
Expand Down
Loading

0 comments on commit 30dd961

Please sign in to comment.