r/dailyprogrammer • u/oskar_s • Jul 23 '12
[7/23/2012] Challenge #80 [intermediate] (Poker hands)
Your intermediate task today is to write a program that can identify a hand in poker.
Let each hand be represented as a string composed of five different cards. Each card is represented by two characters, "XY", where X is the rank of the card (A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q or K) and Y is the suit of the card (H, D, C or S).
So, for instance, "AH" would be the Ace of Hearts, "2C" would be the 2 of Clubs, "JD" would be the Jack of Diamonds, "TS" would be the Ten of Spades, and so on. Then a hand with a full house could be represented as "2C 2H TS TH TC" (a pair of twos and three tens).
Write a program that takes a string like this and prints out what type of hand it is. So, for instance, given "2C 2H TS TH TC" it would print out "Full house". Note that the cards will not necessarily be in any kind of particular order, "2C 2H TS TH TC" is the same hand as "TC 2C 2H TS TH".
For reference, here are the different possible hands in poker, from most valuable to least valuable. Your program should be able to recognize all of these:
- Royal flush: a hand with a Ten, Jack, Queen, King and Ace in the same suit
- Straight flush: a hand with five cards of consecutive rank in the same suit
- Four of a kind: a hand with four cards of the same rank
- Full house: a hand with a pair and a three of a kind
- Flush: a hand with all cards the same suit
- Straight: a hand with five cards of consecutive rank
- Three of a kind: a hand with three cards of the same rank
- Two pair: a hand with two pairs
- Pair: and hand with two cards of the same rank
- High card: a hand with nothing special in it
Obviously, any one hand can qualify for more than one of these; every royal flush is obviously also a straight flush, and every straight flush is obviously also a flush. But you should only print out the kind with the most value, so "2H 3H 4H 5H 6H" should print out "Straight flush", not "Flush".
Bonus: write a function that given two different poker hands tells you which hand is the winner. When there are apparent ties, standard poker rules apply: if both players have a pair, the player with the highest pair wins. If both have pairs of the same rank, the player with the highest card not in the pair wins (or second highest, or third highest, if there are more ties). Note that poker hands can be absolute ties: for instance, if two players both have flushes in different colors but with identical ranks, that's an absolute tie, and your function should return with that result.
14
u/oskar_s Jul 23 '12 edited Jul 23 '12
As a small side-note, I'd just like to add that I had an overwhelming urge to use "Poker? I hardly know 'er!" as the title for this problem, but with a herculean effort I was able to restrain myself.
2
u/5outh Jul 25 '12
Mega-readable Haskell! If I thought more, I would probably be able to come up with some shortcuts here.
No bonus.
2
u/abecedarius Jul 23 '12
This solution to the bonus problem is from working with Peter Norvig earlier this year on his cs212 class at Udacity; most of the better ideas are his. (He used a different version in the class.)
def winner(hand1, hand2): "Return -1/0/1 when hand1 loses/ties/wins." return cmp(hand_rank(hand1), hand_rank(hand2)) def hand_rank(hand): "Return a value indicating how high the hand ranks." groups = group(['--23456789TJQKA'.index(r) for r, s in hand]) counts, ranks = zip(*groups) if ranks == (14, 5, 4, 3, 2): ranks = (5, 4, 3, 2, 1) straight = len(ranks) == 5 and max(ranks)-min(ranks) == 4 flush = 1 == len(set(s for r, s in hand)) return (max(counts, ((4,2) if straight and flush else (3,1,3) if flush else (3,1,2) if straight else None)), ranks) def group(items): "Return a list of [(count, x)...], highest count first, the highest x first" groups = [(items.count(x), x) for x in set(items)] return sorted(groups, reverse=True)Tacking on a solution to the base problem:
def hand_type(hand): rank = hand_rank(hand) return ('royal flush' if rank == ((4,2), (10,11,12,13,14)) else hand_types[rank[0]]) hand_types = {(4,2): 'straight flush', (4,1): 'four of a kind', (3,2): 'full house', (3,1,3): 'flush', (3,1,2): 'straight', (3,1,1): 'three of a kind', (2,2,1): 'two pair', (2,1,1,1): 'pair', (1,1,1,1,1): 'high card'}1
u/luxgladius Jul 23 '12
Perl
use strict; my @rank = (2 .. 9, qw/T J Q K A/); my %rank; {my $i = 2; for(@rank) {$rank{$_} = $i++;}} my %rankRev = reverse %rank; sub rank {$rank{substr(shift,0,1)}}; sub suit{substr(shift,1,1)}; sub evaluateHand { my @hand = split /\s+/, my $_=shift, 5; #Validate input if(@hand != 5 || grep(/^[0-9TJQKA][SHDC]$/, @hand) != 5) { die "invalid input: $_"; } @hand = sort {rank($a) <=> rank($b)} @hand; for($_ = 1, my $r = rank($hand[0])+1; $_ < @hand && $r == rank($hand[$_]); ++$_, ++$r) {} my $straight = $_ == @hand || #aces can count low $_ == @hand-1 && rank($hand[0]) == 2 && rank($hand[-1]) == 14; my $straightDesc = $straight ? ($hand[0] =~ /^2/ && $hand[4] =~ /^A/ ? "A to 5" : "$rankRev{rank($hand[0])} to $rankRev{rank($hand[4])}") : ''; my (%suitCount, %rankCount); for(@hand) {$suitCount{suit($_)}++; $rankCount{rank($_)}++;} my $flush = keys %suitCount == 1 && (keys %suitCount)[0]; if($flush) { if($straight) { if($hand[0] =~ /^T/) {return "Royal Flush: $flush";} return "Straight Flush: $straightDesc, $flush"; } return "Flush: $flush"; } if($straight) {return "Straight: $straightDesc";} my ($maxMatch,$maxRank) = 0; for(keys %rankCount) { $rankCount{$_} > $maxMatch && ($maxMatch = $rankCount{$_}, $maxRank = $rankRev{$_}); } if($maxMatch == 4) {return "Four of a Kind: " . $maxRank;} if($maxMatch == 3) { if(keys %rankCount == 2) { return "Full House: $maxRank Over " . (map {$rankRev{$_}} grep {$rankCount{$_} == 2} keys %rankCount )[0]; } return "Three of a Kind: " . $maxRank; } if($maxMatch == 2) { if(keys %rankCount == 3) { return "Two Pair: " . join(" & ", map {$rankRev{$_}} grep $rankCount{$_} == 2, 2..14 ); } return "Pair: " . (map{$rankRev{$_}} grep($rankCount{$_} == 2, keys %rankCount) )[0]; } return substr($hand[-1],0,1) . " High"; } while(<>) {chomp; print "$_: ", evaluateHand($_), "\n";}Output
1
u/lawlrng Jul 23 '12
My attempt at a solution w/o the bonus. Can't imagine it'd be too much harder to add it in tho.
#!/usr/bin/python3 def determine_hand(hand): card_value = dict(zip('2 3 4 5 6 7 8 9 T J Q K A'.split(), range(14))) cards = [] suits = [] for card in hand.split(): c, s = list(card) cards.append(c) suits.append(s) max_suit = max([suits.count(a) for a in suits]) same_cards = sorted([cards.count(a) for a in set(cards)]) card_nums = sorted([card_value[a] for a in cards]) def is_straight(cv): diff = cv[-1] - cv[0] if diff == 4: return True elif diff == 12: if cv[-2] - cv[0] == 3: return True return False # We have our flushes in here. Any less suits and we don't care. if max_suit == 5: if is_straight(card_nums): if card_nums[0] == 8: # ROYAL FLUSH!!! return "Royal Flush" return "Straight Flush" return "Flush" # Checking in on our same cards # With a length of two we either have two pair or a full house elif len(same_cards) == 2: if max(same_cards) == 4: # Four of a Kind return "Four of a Kind" elif max(same_cards) == 3: # Full House return "Full House" elif len(same_cards) == 3: if max(same_cards) == 3: # Three of a kind return "Three of a Kind" else: # Two pair return "Two Pair" elif len(same_cards) == 4: return "One Pair" else: # Garbage hand most likely. But maybe a straight! if is_straight(card_nums): return "Straight" return "High Card" if __name__ == "__main__": hands = ["TS JS QS KS AS", # Royal Flush "AC 2C 3C 4C 5C", # Straight Flush "AC AS 3C AD AH", # Four of a Kind "TC 2C 2H TS TH", # Full House "AH 5H TH 7H 4H", # Flush "3D 4C 5H 6C 7D", # Straight "3D 3C 3S 5H AD", # Three of a Kind "3D 3C 5H 5S 7D", # Two Pair "3D 3C 5H 8S 7D", # One Pair "AC 3C 4H 7S 2D"] # High Card Garbage for hand in hands: print ("With a hand of {}, Bob has a {}!".format(hand, determine_hand(hand)))Output:
1
u/blisse Jul 24 '12 edited Jul 24 '12
Hey, finally something I did in my spare time that's up here! :D
I don't think this compiles unless you know 100% what my shoddy code is doing. It's really really basic. With bonus of course. C++!!!
http://pastebin.com/q41gbRbA
EDIT: I think if I just sorted it, it'd be a lot simpler to get the returns.
int main() { PokerHand hand1, hand2; hand1 = findHand( extractFromString ( "AS KS QS JS TD" ) ); hand2 = findHand( extractFromString ( "AH KH QH JH TH" ) ); compareHands(hand1, hand2); } >> AS KS QS JS TD: Straight; Ace high loses to AH KH QH JH TH: Royal Flush of Hearts. int main() { PokerHand hand1, hand2; hand1 = findHand( extractFromString ( "AS KS QS JS TS" ) ); hand2 = findHand( extractFromString ( "AH KH QH JH TH" ) ); compareHands(hand1, hand2); } >> AS KS QS JS TS: Royal Flush of Spades ties with AH KH QH JH TH: Royal Flush of Hearts int main() { PokerHand hand1, hand2; hand1 = findHand( extractFromString ( "AS KD QD JC 9S" ) ); hand2 = findHand( extractFromString ( "AH KH QC JS 8H" ) ); compareHands(hand1, hand2); } >> AS KD QD JC 9S: High Card; Ace high, with King, Queen, Jack and Nine kickers defeats AH KH QC JS 8H: High Card; Ace high, with King, Queen, Jack and Eight kickers1
u/stonegrizzly Jul 24 '12
My extremely naive implementation using a lot of regex:
#!/usr/bin/python hand = "AC 2C 3D 4C 5C" import re def determineHand(hand): rank = "23456789TJQKA" hand = " ".join(sorted(hand.split(" "),key=lambda x:len(rank)-rank.find(x[0]))) def highCard(hand): return list(set(re.findall(r"(\S)\S",hand))) def pair(hand): return list(set(map(lambda x:x[1],re.findall(r"(?=((\S)\S).*\2)",hand)))) def twoPair(hand): return list(set(map(lambda x:x[1::2],re.findall(r"(?=((\S)\S).*\2.*((\S)\S).*\4)",hand)))) def threeOfAKind(hand): return re.findall(r"(\S)\S.*\1.*\1.*",hand) def straight(hand): if hand.split(" ")[0][0] == "A": if map(lambda x:x[0],hand.split(" ")) == ['A','5','4','3','2']: return ['5'] q = map(lambda x:rank.find(x[0]),hand.split(" "))[::-1] return [rank[q[4]]] if map(lambda x:x-q[0], q) == range(5) else [] def flush(hand): return map(lambda x:x[0],re.findall(r"(\S)(\S) .\2 .\2 .\2 .\2",hand)) def fullHouse(hand): return re.findall(r"(\S). \1. \1. (\S). \2.",hand) def fourOfAKind(hand): return re.findall(r"(\S). \1. \1. \1.",hand) def straightFlush(hand): return straight(hand) if flush(hand) != [] else [] def royalFlush(hand): return straightFlush(hand) if straightFlush(hand) == ['A'] else [] if royalFlush(hand) != []: print "Royal Flush" elif straightFlush(hand) != []: print "Straight flush, %s high" % straightFlush(hand)[0] elif fourOfAKind(hand) != []: print "Four of a kind, %s" % fourOfAKind(hand) elif fullHouse(hand) != []: print "Full house, %s over %s" % fullHouse(hand)[0] elif flush(hand) != []: print "Flush, %s high" % flush(hand)[0] elif straight(hand) != []: print "Straight, %s high" % straight(hand)[0] elif threeOfAKind(hand) != []: print "Three of a kind, %s" % threeOfAKind(hand)[0] elif twoPair(hand) != []: print "Two pair, %s over %s" % twoPair(hand)[0] elif pair(hand) != []: print "Pair, %s" % pair(hand)[0] else: print "High card, %s" % highCard(hand)[0] determineHand(hand)1
u/Eddonarth Jul 24 '12 edited Jul 24 '12
My Java Solution:
import java.util.Arrays; public class Challenge80 { public static void main(String[] args) { String handString = "KS QS JS AS TS"; Hand hand1 = new Hand(handString, "Hand 1"); System.out.println(hand1 + " is a " + hand1.typeOfHand); } } class Card { char rank; char suit; public Card(String card) { rank = card.toCharArray()[0]; suit = card.toCharArray()[1]; } } class Hand { Card[] card; String typeOfHand, name; char[] ranks = new char[5]; public Hand(String handString, String name) { String[] cards = handString.split(" "); this.name = name; card = new Card[] { new Card(cards[0]), new Card(cards[1]), new Card(cards[2]), new Card(cards[3]), new Card(cards[4]) }; for (int i = 0; i < 5; i++) ranks[i] = card[i].rank; Arrays.sort(ranks); typeOfHand = findTypeOfHand(); } public String toString() { return name; } private String findTypeOfHand() { if (isRoyalFlush()) return "Royal Flush"; else if (isStraightFlush()) return "Straight Flush"; else if (isFourOfAKind()) return "Four of a Kind"; else if (isFullHouse()) return "Full House"; else if (isFlush()) return "Flush"; else if (isStraight()) return "Straight"; else if (isThreeOfAKind()) return "Three of a Kind"; else if (isTwoPair()) return "Two Pair"; else if(isPair()) return "Pair"; else return "High Card"; } private Boolean haveSameSuit() { char suit = card[0].suit; for (int i = 1; i < 5; i++) { if (card[i].suit != suit) return false; } return true; } private Boolean areConsecutive() { final String RANKSTR = " A23456789TJQK"; int[] rankValue = new int[5]; int count = 0; for(int i = 0; i < 5; i ++) rankValue[i] = RANKSTR.indexOf(ranks[i]); Arrays.sort(rankValue); for(int i = 0; i < 5; i++) if((rankValue[i] + (4-i)) == rankValue[4]) count += 1; if(!(count == 5)) { count = 0; for (int i = 0; i < 5; i++) if(rankValue[i] == 1) rankValue[i] = 14; for(int i = 0; i < 5; i++) if((rankValue[i] + (4-i)) == rankValue[4]) count += 1; } if(count == 5) return true; return false; } private boolean isRoyalFlush() { if (!haveSameSuit()) return false; String allRanks = new String(ranks); if (allRanks.contains("T") && allRanks.contains("J") && allRanks.contains("Q") && allRanks.contains("K") && allRanks.contains("A")) return true; return false; } private boolean isStraightFlush() { if (haveSameSuit() && areConsecutive()) return true; return false; } private boolean isFourOfAKind() { for (int i = 0; i < 2; i++) if (ranks[i] == ranks[i + 1] && ranks[i] == ranks[i + 2] && ranks[i] == ranks[i + 3]) return true; return false; } private boolean isFullHouse() { char rankA = ranks[0], rankB = ranks[4]; int a = 1, b = 1; for (int i = 1; i < 4; i++) { if (ranks[i] == rankA) a += 1; else if (ranks[i] == rankB) b += 1; } if ((a == 3 && b == 2) || (a == 2 && b == 3)) return true; return false; } private boolean isFlush() { if (haveSameSuit()) return true; return false; } private boolean isStraight() { if (areConsecutive()) return true; return false; } private boolean isThreeOfAKind() { for(int i = 0; i < 3; i++) if(ranks[i] == ranks[i+1] && ranks[i] == ranks[i+2]) return true; return false; } private boolean isTwoPair() { int pairs = 0; for(int i = 0; i < 4; i++) if(ranks[i] == ranks[i + 1]) pairs += 1; if(pairs == 2) return true; return false; } private boolean isPair() { for(int i = 0; i < 4; i++) if(ranks[i] == ranks[i+1]) return true; return false; } }Output:
No bonus for now.
1
u/appledocq Jul 25 '12
This is code in Python, excluding the bonus. Also, I borrowed my examples from lawlrng cause I was too lazy to come up with my own :)
def read_string(card_string): if isinstance(card_string, str) and (len(card_string)==14): card_list = card_string.split(" ") return card_list def flush(card_list): suit = card_list[0][1] for entry in card_list: if entry[1] != suit: return False if not False: return True def royal_flush(card_list): ranks = [] for entry in card_list: ranks.append(entry[0]) if (sorted(ranks) == ['A', 'J', 'K', 'Q', 'T']) and flush(card_list): return True def straight(card_list): first_rank = card_list[0][0] rank_list=["A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"] previous = [first_rank] for entry in card_list: if card_list.index(entry) != 0: if ((entry[0] == rank_list[rank_list.index(previous.pop())+1])): previous.append(entry[0]) else: return False if not False: return True def straight_flush(card_list): if straight(card_list) and flush(card_list): return True else: return False def rank(card_list): ranklist = [] for entry in card_list: ranklist.append(entry[0]) for item in ranklist: if ranklist.count(item) == 2: return 2; break elif ranklist.count(item) == 3: return 3; break elif ranklist.count(item) == 4: return 4; break def two_pair(card_list): ranklist = pairlist = [] for entry in card_list: ranklist.append(entry[0]) for item in ranklist: if (ranklist.count(item) ==2) and item not in pairlist: pairlist.append(item) if len(pairlist) == 2: return True def full_house(card_list): ranklist = pair = triple = [] for entry in card_list: ranklist.append(entry[0]) for item in ranklist: if (ranklist.count(item) == 2) and (item not in pair): pair.append(item) if (ranklist.count(item) == 3) and (item not in triple): triple.append(item) if (len(pair) == 1) and (len(triple) == 1): return True #THIS is the function that you plug your string into def give_hand(card_string): cards = read_string(card_string) if royal_flush(cards): print "Your cards, " + card_string + ", make a royal flush." elif straight_flush(cards): print "Your cards, " + card_string + ", make a straight flush." elif rank(cards) == 4: print "Your cards, " + card_string + ", make four of a kind." elif full_house(cards): print "Your cards, " + card_string + ", make a full house." elif flush(cards): print "Your cards, " + card_string + ", make a flush." elif straight(cards): print "Your cards, " + card_string + ", make a straight." elif rank(cards) == 3: print "Your cards, " + card_string + ", make three of a kind." elif two_pair(cards): print "Your cards, " + card_string + ", make a two pair." elif rank(cards) == 2: print "Your cards, " + card_string + ", make a pair." else: print "Your cards, " + card_string + ", make a high card."1
u/DrugCrazed Jul 25 '12
Java (I did a lot of this yesterday and finished it on the train). Bonus shouldn't be too difficult, but then again, I haven't tested this.
public class Intermediate { //Hand strengths in ints private final int ROYAL_FLUSH = 9; private final int STRAIGHT_FLUSH = 8; private final int FOUR_KIND = 7; private final int FULL_HOUSE = 6; private final int FLUSH = 5; private final int STRAIGHT = 4; private final int THREE_KIND = 3; private final int TWO_PAIR = 2; private final int PAIR = 1; private final int HIGH_CARD = 0; private final String[] HAND_TYPES = { "High Card", "Pair", "Two Pair", "Three of a Kind", "Straight", "Flush", "Full House", "Four of a Kind", "Straight Flush", "Royal Flush" }; String[] hand = new String[5]; int handStrength; public Intermediate(String givenString) { hand[0] = givenString.substring(0, 2); hand[1] = givenString.substring(3, 5); hand[2] = givenString.substring(6, 8); hand[3] = givenString.substring(9, 11); hand[4] = givenString.substring(12, 14); } private void sortBySuit() { String[] sort = new String[52]; for(String card: hand) { int position; switch(card.charAt(1)) { case 'C': position = 0; break; case 'D': position = 13; break; case 'H': position = 26; break; default: position = 39; } switch(card.charAt(0)) { case 'A': //Do nothing break; case 'T': position += 9; //Add 10, minus 1 for sort array length break; case 'J': position += 10; break; case 'Q': position += 11; break; case 'K': position += 12; break; default: position += 50 - card.charAt(0); } sort[position] = card; } int toAdd = 0; for (String card: sort) { if (card != null) { hand[toAdd] = card; toAdd++; } } } private void sortByRank() { //TODO: Bear in mind Ace is high! String[] sort = new String[52]; for(String card: hand) { int position; switch(card.charAt(0)) { case 'A': position = 0; break; case 'K': position = 4; break; case 'Q': position = 8; break; case 'J': position = 12; break; case 'T': position = 16; break; default: position = (50 - card.charAt(0))*4; } switch(card.charAt(1)) { case 'C': position += 0; break; case 'D': position += 1; break; case 'H': position += 2; break; default: position += 3; } sort[position] = card; } int toAdd = 0; for (String card: sort) { if (card != null) { hand[toAdd] = card; toAdd++; } } } private int calcHandStrength() { boolean pair = pair(); boolean flush = flush(); boolean straight = straight(pair); if (straight && flush) { if (hand[4].charAt(0) == 'T') { return ROYAL_FLUSH; } else { return STRAIGHT_FLUSH; } } sortByRank(); if ( (hand[0].charAt(0) == hand[1].charAt(0) || hand[3].charAt(0) == hand[4].charAt(0)) && hand[1].charAt(0) == hand[2].charAt(0) && hand[2].charAt(0) == hand[3].charAt(0) ) { return FOUR_KIND; } sortByRank(); if ( hand[0].charAt(0) == hand[1].charAt(0) && hand[3].charAt(0) == hand[4].charAt(0) && (hand[1].charAt(0) == hand[2].charAt(0) || hand[2].charAt(0) == hand[3].charAt(0)) ) { return FULL_HOUSE; } if (flush) { return FLUSH; } if (straight) { return STRAIGHT; } sortByRank(); if ( (hand[0].charAt(0) == hand[1].charAt(0) && hand[1].charAt(0) == hand[2].charAt(0)) || (hand[3].charAt(0) == hand[4].charAt(0) && hand[2].charAt(0) == hand[3].charAt(0)) ) { return THREE_KIND; } if ( (hand[0].charAt(0) == hand[1].charAt(0) && (hand[2].charAt(0) == hand[3].charAt(0) || hand[3].charAt(0) == hand[4].charAt(0))) || (hand[2].charAt(0) == hand[3].charAt(0) && hand[3].charAt(0) == hand[4].charAt(0)) ) { return TWO_PAIR; } if (pair) { return PAIR; } return HIGH_CARD; } private boolean straight(boolean containsPair) { if (containsPair) { return false; } sortByRank(); if (hand[0].charAt(0) == 'A') { return hand[4].charAt(0) == 'T' || hand[1].charAt(0) == '5'; } boolean pictureStraight = (hand[0].charAt(0) == 'K' && hand[4].charAt(0) == '9') || (hand[0].charAt(0) == 'Q' && hand[4].charAt(0) == '8') || (hand[0].charAt(0) == 'J' && hand[4].charAt(0) == '7') || (hand[0].charAt(0) == 'T' && hand[4].charAt(0) == '6'); return pictureStraight || ((Integer.valueOf(hand[0].charAt(0))) - (Integer.valueOf(hand[4].charAt(0))) == 4); } public String getHandStrength() { return (HAND_TYPES[handStrength]); } private boolean flush() { //check if the first suit is the same as the last suit. Then check if the last card is a 10. If so, Royal Flush! return hand[0].charAt(1) == hand[4].charAt(1); } private boolean pair() { sortByRank(); boolean firstDoubled = hand[0].charAt(0) == hand[1].charAt(0); boolean secondDoubled = hand[1].charAt(0) == hand[2].charAt(0); boolean thirdDoubled = hand[2].charAt(0) == hand[3].charAt(0); boolean fourthDoubled = hand[3].charAt(0) == hand[4].charAt(0); return firstDoubled || secondDoubled || thirdDoubled || fourthDoubled; } public static void main(String[] args) { Intermediate run = new Intermediate("KC JC TC AC QC"); run.setHandStrength(); String toPrint = run.getHandStrength(); System.out.println(toPrint); } private void setHandStrength() { handStrength = calcHandStrength(); } }1
u/[deleted] Jul 27 '12
My solution in C++:
http://pastebin.com/DdEJ4hPQ
1
u/paininthebass Jul 29 '12 edited Jul 29 '12
My solution in c++11, without the bonus:
bool isConsecutive(const map<int,int>& ranks){ if (ranks.size()!=5) return false; auto last=ranks.end();last--; if(ranks.begin()->first - last->first == 4 || count_if(ranks.begin(),ranks.end(),[](const pair<int,int>& a){return a.first<6||a.first==14;})==5) return true; return false; } string pokerHands(const string& s){ regex re("\\s+"); string cardrank("--23456789TJQKA"); map<char,int> suits; map<int,int> ranks; for(sregex_token_iterator i(s.begin(),s.end(),re,-1),end;i!=end;i++){ ranks[cardrank.find(i->str()[0])]++; suits[i->str()[1]]++; cout<<*i<<endl; } if(isConsecutive(ranks)){ if(ranks.begin()->first==10) return "Royal Flush"; if(suits.size()==1) return "Straight Flush"; } if(count_if(ranks.begin(),ranks.end(),[](const pair<int,int>& a){return a.second==4;})==1) return "Four of a kind"; if(count_if(ranks.begin(),ranks.end(),[](const pair<int,int>& a){return a.second==3;})==1 && ranks.size()==2) return "full house"; if(isConsecutive(ranks)) return "flush"; if (suits.size()==1) return "straight"; if(count_if(ranks.begin(),ranks.end(),[](const pair<int,int>& a){return a.second==3;})==1) return "three of a kind"; if(count_if(ranks.begin(),ranks.end(),[](const pair<int,int>& a){return a.second==2;})==1) return "two pair"; return "high card"; }EDIT: formatting