new from koans, loops and coding
- loops can be "ON" a list, then iterate over structure, and can be BY 'cddr and such - looping over hashtables seems painful - during coding didn't think about getting debugger, couldn't figure out how to do it without BREAK, and in DO loop didn't see separate frames - again remembered MAKUNBOUND for if I try to set values during debugger
This commit is contained in:
@@ -49,11 +49,57 @@
|
||||
;;;
|
||||
;;; Your goal is to write the scoring function for Greed.
|
||||
|
||||
(defun score (&rest dice)
|
||||
____)
|
||||
(defvar *dice-evaluations* (let ((dice-evaluations-hash (make-hash-table :test #'equal :size 8)))
|
||||
(setf (gethash '(1 1 1) dice-evaluations-hash) 1000
|
||||
(gethash '(6 6 6) dice-evaluations-hash) 600
|
||||
(gethash '(5 5 5) dice-evaluations-hash) 500
|
||||
(gethash '(4 4 4) dice-evaluations-hash) 400
|
||||
(gethash '(3 3 3) dice-evaluations-hash) 300
|
||||
(gethash '(2 2 2) dice-evaluations-hash) 200
|
||||
(gethash '(1) dice-evaluations-hash) 100
|
||||
(gethash '(5) dice-evaluations-hash) 50)
|
||||
dice-evaluations-hash))
|
||||
|
||||
;; iterate over dice. (on?)
|
||||
|
||||
;; (loop for rest-dice on '(1 2 5 1 1) collect rest-dice)
|
||||
;; (subseq '(1 1 2 5) 0 2)
|
||||
;; (subseq '(1 1 2 5) 0 1)
|
||||
;; (gethash '(2) *dice-evaluations*)
|
||||
|
||||
(defun score-bad-1 (&rest dice)
|
||||
(loop for rest-dice on (sort dice #'<)
|
||||
sum (or
|
||||
(gethash (subseq rest-dice 0 (min (length rest-dice) 3)) *dice-evaluations*)
|
||||
(gethash (subseq rest-dice 0 (min (length rest-dice) 1)) *dice-evaluations*)
|
||||
0)))
|
||||
|
||||
|
||||
(defun take-n (list n)
|
||||
(butlast list (max 0 (- (length list) n))))
|
||||
|
||||
(take-n '(1 1 3 2) 5)
|
||||
;; (makunbound 'dice)
|
||||
|
||||
;; (setq test-dice '(1 1 1 2 5))
|
||||
(defun score (&rest dice) (do ((dice (sort dice #'<))
|
||||
(running-sum 0)
|
||||
(maybe-3-score (gethash (take-n dice 3) *dice-evaluations*) (gethash (take-n dice 3) *dice-evaluations*))
|
||||
(maybe-1-score (gethash (take-n dice 1) *dice-evaluations*) (gethash (take-n dice 1) *dice-evaluations*)))
|
||||
((eq nil dice) running-sum)
|
||||
;; (break "fuck")
|
||||
(if maybe-3-score (setf dice (cdddr dice)
|
||||
running-sum (+ running-sum maybe-3-score))
|
||||
(setf dice (cdr dice)
|
||||
running-sum (+ running-sum (or maybe-1-score 0))))))
|
||||
|
||||
(score 1 5 1) ; yay, i think it works
|
||||
; but not as comfortable as I imagined, or as could be?
|
||||
; not at all comfortable with debugger, some shit is shown, not iteration step
|
||||
|
||||
|
||||
(define-test score-of-an-empty-list-is-zero
|
||||
(assert-equal 0 (score)))
|
||||
(assert-equal 0 (score)))
|
||||
|
||||
(define-test score-of-a-single-roll-of-5-is-50
|
||||
(assert-equal 50 (score 5)))
|
||||
|
||||
Reference in New Issue
Block a user