Skip to content

Commit

Permalink
yacht exercise: refactored an example solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimaDovakin committed May 14, 2024
1 parent eba59b3 commit 28b625f
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions exercises/practice/yacht/.meta/example.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,24 @@
(in-package :yacht)

(defun hash-values (ht)
(let ((vals '()))
(progn
(loop for v being each hash-value of ht do (setf vals (cons v vals)))
vals)))
(loop for v being each hash-value of ht collect v))

(defun frequencies (sequence)
(let ((occurences (make-hash-table)))
(reduce (lambda (acc n)
(let ((freq (gethash n acc)))
(if freq
(progn
(setf (gethash n acc) (+ freq 1))
acc
)
(progn
(setf (gethash n acc) 1))))
acc)
(incf (gethash n acc 0))
acc)
sequence
:initial-value occurences)))

(defun full-house? (scores)
(let* ((occurences (frequencies scores))
(vals (hash-values occurences)))
(defun full-house (occurences)
(let ((vals (hash-values occurences)))
(equal '(2 3) (sort vals #'<))))

(defun four-of-a-kind-dice (occurences)
(let ((pair nil))
(maphash (lambda (key val)
(if (>= val 4)
(when (>= val 4)
(setf pair key)))
occurences)
pair))
Expand All @@ -45,7 +34,7 @@
((equal category "fours") (* 4 (count 4 scores)))
((equal category "fives") (* 5 (count 5 scores)))
((equal category "sixes") (* 6 (count 6 scores)))
((equal category "full house") (if (full-house? scores)
((equal category "full house") (if (full-house (frequencies scores))
(reduce '+ scores)
0))
((equal category "four of a kind") (let ((dice (four-of-a-kind-dice (frequencies scores))))
Expand Down

0 comments on commit 28b625f

Please sign in to comment.