Skip to content

Commit

Permalink
Best Poker Hand
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 17, 2023
1 parent eb180a1 commit a908daa
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ add_task(backspace-string-compare)
add_task(balanced-binary-tree)
add_task(basic-calculator)
add_task(basic-calculator-ii)
add_task(best-poker-hand)
add_task(best-sightseeing-pair)
add_task(best-time-to-buy-and-sell-stock)
add_task(best-time-to-buy-and-sell-stock-ii)
Expand Down
1 change: 1 addition & 0 deletions solutions/best-poker-hand/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_best_poker_hand test.cpp)
29 changes: 29 additions & 0 deletions solutions/best-poker-hand/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <map>
#include <set>
#include <string>
#include <vector>

class Solution {
public:
static std::string bestHand(const std::vector<int> &ranks,
const std::vector<char> &suits) {
if (std::set(suits.begin(), suits.end()).size() == 1) {
return "Flush";
}

std::map<int, int> count;
int max = 0;
for (auto r : ranks) {
max = std::max(max, ++count[r]);
}
if (max > 2) {
return "Three of a Kind";
}
if (max > 1) {
return "Pair";
}
return "High Card";
}
};
34 changes: 34 additions & 0 deletions solutions/best-poker-hand/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::vector<int> ranks;
std::vector<char> suits;
std::string expected;
};

std::vector<TestCase> test_cases{
{
.ranks{13, 2, 3, 1, 9},
.suits{'a', 'a', 'a', 'a', 'a'},
.expected = "Flush",
},
{
.ranks{4, 4, 2, 4, 4},
.suits{'d', 'a', 'a', 'b', 'c'},
.expected = "Three of a Kind",
},
{
.ranks{10, 10, 2, 12, 9},
.suits{'a', 'b', 'c', 'a', 'd'},
.expected = "Pair",
},
};

for (const auto &[ranks, suits, expected] : test_cases) {
const auto actual = Solution::bestHand(ranks, suits);
REQUIRE(expected == actual);
}
}

0 comments on commit a908daa

Please sign in to comment.