From 9280394619170b27bed6d81cd9fa2e770c046643 Mon Sep 17 00:00:00 2001 From: Binod Sarkar Date: Sat, 25 May 2024 08:39:29 +0100 Subject: [PATCH] ppp solved --- src/p_p_p_pokerface.clj | 54 ++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 5ea80094..f17a2fd5 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -1,34 +1,66 @@ (ns p-p-p-pokerface) +(def replacements {\T 10, \J 11 \Q 12 \K 13 \A 14}) + (defn rank [card] - nil) + (let [[fst _] card] + (if (Character/isDigit fst) + (Integer/valueOf (str fst)) + (get replacements fst)))) (defn suit [card] - nil) + (let [[_ snd] card] + (str snd))) (defn pair? [hand] - nil) + (let [ranks (map rank hand) + freq (vals (frequencies ranks))] + (>= (apply max freq) 2))) (defn three-of-a-kind? [hand] - nil) + (let [ranks (map rank hand) + freq (vals (frequencies ranks))] + (>= (apply max freq) 3))) (defn four-of-a-kind? [hand] - nil) + (let [ranks (map rank hand) + freq (vals (frequencies ranks))] + (>= (apply max freq) 4))) (defn flush? [hand] - nil) + (let [suits (map suit hand)] + (apply = suits))) (defn full-house? [hand] - nil) + (let [ranks (map rank hand) + freq (vals (frequencies ranks))] + (every? #(or (= % 2) (= % 3)) freq))) (defn two-pairs? [hand] - nil) + (let [ranks (map rank hand) + freq (vals (frequencies ranks))] + (= '(1 2 2) (sort freq)))) (defn straight? [hand] - nil) + (let [ranks (map rank hand) + sorted-ranks (sort ranks) + inv-sorted-ranks (sort (replace {14 1} ranks))] + (or (= (take 5 (iterate inc (first sorted-ranks))) sorted-ranks) + (= (take 5 (iterate inc (first inv-sorted-ranks))) inv-sorted-ranks)))) (defn straight-flush? [hand] - nil) + (let [ranks (map rank hand)] + (and (straight? hand) (flush? hand)))) + +(defn high-card? [hand] + true) (defn value [hand] - nil) + (let [checkers #{[high-card? 0] [pair? 1] + [two-pairs? 2] [three-of-a-kind? 3] + [straight? 4] [flush? 5] + [full-house? 6] [four-of-a-kind? 7] + [straight-flush? 8]}] + (apply max (map second (filter (fn [[bool _]] bool) + (map (fn [[f score]] [(f hand) score]) checkers)))) + ))