From 2a83e6f323c5422c9b74b57acf9b3462e3c4e50e Mon Sep 17 00:00:00 2001 From: Prashant Sinha Date: Sat, 20 Nov 2021 02:09:56 +0530 Subject: [PATCH] Solves pokerface problems --- src/p_p_p_pokerface.clj | 45 +++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 5ea80094..e0e8d9e9 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -1,34 +1,53 @@ (ns p-p-p-pokerface) -(defn rank [card] - nil) +(def replacements {\T 10 \J 11 \Q 12 \K 13 \A 14}) -(defn suit [card] - nil) +(defn rank [[rank _]] + (if (Character/isDigit rank) + (Integer/valueOf (str rank)) + (get replacements rank))) + +(defn suit [[_ suit]] + (str suit)) (defn pair? [hand] - nil) + (< 0 (count (filter #(= 2 %) (vals (frequencies (map rank hand))))))) (defn three-of-a-kind? [hand] - nil) + (< 0 (count (filter #(= 3 %) (vals (frequencies (map rank hand))))))) (defn four-of-a-kind? [hand] - nil) + (< 0 (count (filter #(= 4 %) (vals (frequencies (map rank hand))))))) (defn flush? [hand] - nil) + (= 1 (count (keys (frequencies (map suit hand)))))) (defn full-house? [hand] - nil) + (= [2 3] (sort (vals (frequencies (map rank hand)))))) (defn two-pairs? [hand] - nil) + (= [1 2 2] (sort (vals (frequencies (map rank hand)))))) (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) + (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))))))