You are given an integer array ranks
and a character array suits
. You have 5
cards where the ith
card has a rank of ranks[i]
and a suit of suits[i]
.
The following are the types of poker hands you can make from best to worst:
"Flush "
: Five cards of the same suit."Three of a Kind "
: Three cards of the same rank."Pair "
: Two cards of the same rank."High Card "
: Any single card.Return a string representing the best type of poker hand you can make with the given cards.
Note that the return values are case-sensitive.
Example 1:
Input: ranks = [13,2,3,1,9], suits = [ "a ", "a ", "a ", "a ", "a "] Output: "Flush " Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush ".
Example 2:
Input: ranks = [4,4,2,4,4], suits = [ "d ", "a ", "a ", "b ", "c "] Output: "Three of a Kind " Explanation: The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind ". Note that we could also make a "Pair " hand but "Three of a Kind " is a better hand. Also note that other cards could be used to make the "Three of a Kind " hand.
Example 3:
Input: ranks = [10,10,2,12,9], suits = [ "a ", "b ", "c ", "a ", "d "] Output: "Pair " Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair ". Note that we cannot make a "Flush " or a "Three of a Kind ".
Constraints:
ranks.length == suits.length == 5
1 <= ranks[i] <= 13
'a' <= suits[i] <= 'd'
program poker_hands
implicit none
integer, parameter :: ncards = 5
integer, dimension(ncards) :: ranks
character(len=1), dimension(ncards) :: suits
call get_input(ranks, suits)
print '(A)', get_best_hand(ranks, suits)
contains
subroutine get_input(ranks, suits)
implicit none
integer, dimension(:), intent(out) :: ranks
character(len=1), dimension(:), intent(out) :: suits
integer :: i
do i = 1, ncards
read *, ranks(i), suits(i)
end do
end subroutine get_input
function get_best_hand(ranks, suits) result(best_hand)
implicit none
integer, dimension(:), intent(in) :: ranks
character(len=1), dimension(:), intent(in) :: suits
character(len=*) :: best_hand
integer :: i, j, k, l, m
integer :: flush_count, three_kind_count, pair_count
integer :: high_card
logical :: flush
flush_count = 0
three_kind_count = 0
pair_count = 0
high_card = -1
! Count the number of cards of each type
do i = 1, ncards
if (flush_count < 1 .and. all(suits(i) == suits(1))) then
flush_count = flush_count + 1
else if (three_kind_count < 1 .and. count(ranks(i) == ranks(1)) == 3) then
three_kind_count = three_kind_count + 1
else if (pair_count < 1 .and. count(ranks(i) == ranks(1)) == 2) then
pair_count = pair_count + 1
else if (high_card < 1 .or. ranks(i) > high_card) then
high_card = ranks(i)
end if
end do
! Check for flush
if (flush_count >= 1) then
best_hand = "Flush "
! Check for three of a kind
else if (three_kind_count >= 1) then
best_hand = "Three of a Kind "
! Check for pair
else if (pair_count >= 1) then
best_hand = "Pair "
! Otherwise it's a high card
else
best_hand = "High Card "
end if
end function get_best_hand
end program poker_hands
temp.f95:26:4: 26 | function get_best_hand(ranks, suits) result(best_hand) | 1 Error: Character-valued internal function ‘get_best_hand’ at (1) must not be assumed length temp.f95:44:42: 44 | if (flush_count < 1 .and. all(suits(i) == suits(1))) then | 1 Error: ‘mask’ argument of ‘all’ intrinsic at (1) must be a logical array temp.f95:46:54: 46 | else if (three_kind_count < 1 .and. count(ranks(i) == ranks(1)) == 3) then | 1 Error: ‘mask’ argument of ‘count’ intrinsic at (1) must be a logical array temp.f95:48:48: 48 | else if (pair_count < 1 .and. count(ranks(i) == ranks(1)) == 2) then | 1 Error: ‘mask’ argument of ‘count’ intrinsic at (1) must be a logical array
program poker_hands
implicit none
integer, parameter :: MAX_CARDS = 5
integer :: ranks(MAX_CARDS)
character :: suits(MAX_CARDS)
character(len=10) :: best_hand
integer :: i
read(*,*) ranks
read(*,*) suits
! Check for flush
if (all(suits == suits(1))) then
best_hand = "Flush "
! Check for three of a kind
else if (count(ranks == ranks(1)) == 3) then
best_hand = "Three of a Kind "
! Check for pair
else if (count(ranks == ranks(1)) == 2) then
best_hand = "Pair "
! Check for high card
else
best_hand = "High Card "
end if
write(*,*) best_hand
end program poker_hands
At line 13 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7c6751ee0960 in ??? #1 0x7c6751ee14d9 in ??? #2 0x7c675213517b in ??? #3 0x7c675212e684 in ??? #4 0x7c675212f2aa in ??? #5 0x7c6752134b7a in ??? #6 0x573c1125b27f in MAIN__ #7 0x573c1125b505 in main
def best_poker_hand(ranks, suits):
rank_count = {}
suit_count = {}
for rank, suit in zip(ranks, suits):
rank_count[rank] = rank_count.get(rank, 0) + 1
suit_count[suit] = suit_count.get(suit, 0) + 1
if len(suit_count) == 1:
return "Flush "
if len(rank_count) == 3:
return "Three of a Kind "
for value in rank_count.values():
if value == 2:
return "Pair "
return "High Card "
We first iterate through the `ranks` and `suits` arrays and store the count of each rank and suit in two separate hash maps (`rank_count` and `suit_count` respectively). Then we check the following to determine the best poker hand:
suit_count
has only one key, it means all cards have the same suit, so the result is a "Flush ".rank_count
has only three keys, it means there's a "Three of a Kind ", because there are either 3 of one rank and 1 each of two other ranks or 2 of one rank and 1 each of three other ranks.rank_count
, it implies there's a "Pair ".#include <map>
#include <string>
#include <vector>
std::string bestPokerHand(std::vector<int> &ranks, std::vector<char> &suits) {
std::map<int, int> rank_count;
std::map<char, int> suit_count;
for (int i = 0; i < ranks.size(); ++i) {
rank_count[ranks[i]]++;
suit_count[suits[i]]++;
}
if (suit_count.size() == 1) return "Flush ";
if (rank_count.size() == 3) return "Three of a Kind ";
for (const auto &pair : rank_count) {
if (pair.second == 2) return "Pair ";
}
return "High Card ";
}
We first iterate through the `ranks` and `suits` arrays and store the count of each rank and suit in two separate hash maps (`rank_count` and `suit_count` respectively). Then we check the following to determine the best poker hand:
suit_count
has only one key, it means all cards have the same suit, so the result is a "Flush ".rank_count
has only three keys, it means there's a "Three of a Kind ", because there are either 3 of one rank and 1 each of two other ranks or 2 of one rank and 1 each of three other ranks.rank_count
, it implies there's a "Pair ".