CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February Long Contest
    • January CookOff
    • January Long Contest
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(easy) » Poker

Poker

Problem code: POKER

  • Submit
  • All Submissions

All submissions for this problem are available.

In poker, you have 5 cards. There are 10 kinds of poker hands (from highest to lowest):

  • royal flush - ace, king, queen, jack and ten, all in the same suit
  • straight flush - five cards of the same suit in sequence, such
    as 10,9,8,7,6 of clubs; ace can be counted both as the highest card or as the
    lowest card - A,2,3,4,5 of hearts is a straight flush. But 4,3,2,A,K of hearts is not a straight flush - it's just a flush.
  • four of a kind - four cards of the same rank, such as four kings.
  • full house - three cards of one rank plus two cards of another rank
  • flush - five cards of the same suit (but not a straight flush)
  • straight - five cards in order - just like the straight flush, but mixed suits
  • three of a kind - three cards of one rank and two other cards
  • two pairs - two cards of one rank, two cards of another rank, and one more card
  • pair - two cards of the same rank
  • high card - none of the above

Write a program that will help you play poker by telling you what kind of hand you have.

Input

The first line of input contains the number of test cases (no more than 20). Each test case consists of one line - five space separated cards. Each card is represented by a two-letter (or digit) word. The first character is the rank (A,K,Q,J,T,9,8,7,6,5,4,3 or 2), the second character is the suit (S,H,D,C standing for spades, hearts, diamonds and clubs). The cards can be in any order (but they will not repeat).

Output

For each test case output one line describing the type of a hand, exactly like in the list above.

Example

Input:
3
AH KH QH TH JH
KH 5S 3C 5C 7D
QH QD 2S QC 2C

Output:
royal flush
pair
full house

Date: 2008-12-01
Time limit: 7s
Source limit: 50000
Languages: C C99 strict C++ PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp HASK CAML CLPS PRLG WSPC BF ICK TEXT


  • Submit

Comments

  • Login or Register to post a comment.

rnoack @ 20 Jun 2009 05:59 AM

SORRY, but this site is bad. the code works. the input is correct.

if i am doing it wrong, the site isn't clear enough.

leppyr64 @ 20 Jun 2009 07:44 AM

@Robert Noack - The problem is clear and the input is correct. There are more test cases than just the sample ones. Just because you have the right answer for the three that are posted, doesn't mean you've solved it. If you have any questions about the problem you can take them to the forum, and I'd be glad to help you.

jensendp @ 4 Jul 2009 04:03 AM

I love this site. Sometimes I interpret the instructions one way, but they are meant to be interpreted another. Also, part of the fun is trying to figure out why your code fails when it looks like it should, this usually means the extreme cases. Long live the Chef!

aseem_pandey @ 9 Aug 2009 08:13 PM

someone please look at my code and tell my mistakes.
it works correct but the site shows wrong answer.

this is for the fist i'm

phanindhar @ 16 Sep 2009 10:22 PM
this is for the fist i'm using this the site i expect i can improve my self alot using this

Thats nice to know :)

admin @ 16 Sep 2009 10:25 PM
Thats nice to know :)

My first submission failed

Philanthropist @ 11 Oct 2009 06:37 AM

My first submission failed only because I output "two pair" instead of "two pairs". Be very attentive to the spelling and case of every word. Adding the s made my program correct.

I love solving one of these

hiptobecubic @ 21 Nov 2009 12:41 PM

I love solving one of these and seeing everyone else's crazy and convoluted solutions. There should be points for style. Check out my 'matching' function to determine pairs. It's godly. I love it. I'd shower in it if I could figure out how.

please some one put the

moneymachine @ 1 Mar 2010 07:19 PM

please some one put the corner cases .. please ,....... :(

I have tried all examples of

shubh09 @ 5 Mar 2010 12:53 PM

I have tried all examples of poker hands from a poker site on my prog...its working fine for all of them...still when i submit, i get 'Wrong Answer', don't know what to do...

Just one little mistake :) In

triplem @ 5 Mar 2010 01:07 PM

Just one little mistake :) In one of the cases (and no I won't say which) you're missing one possibility of a different group of cards being equal.

@Stephen Merriman.. I don't

linuxfreak @ 17 Mar 2010 08:18 AM

@Stephen Merriman.. I don't think there can be a case like what you have mentioned here and is not mentioned in the test case... cause you are saying cards of different group being equal.. means... 4 cards from each suit having same rank.. bt.. the last card can be any thing... so .. means.. "four of a kind case" ... and.. if you are saying that.. five cards belonging to same suit.. means.. either.. royal, straight, or just a flash ??? now.. what you are saying??

I was referring to a specific

triplem @ 17 Mar 2010 09:53 AM

I was referring to a specific bug inside Shubham's code. What I wrote should have made sense for him; it is irrelevant to anyone else since it was specific to his style of coding.

@Stephen:: Just check my

shreydutta.28 @ 29 Mar 2010 01:03 PM

@Stephen:: Just check my code..... n please tell where m wrong, i have tried many test cases but they are giving the right answer........ but here i am getting wrong answer...... so please suggest something......

If not him then, anyone

shreydutta.28 @ 3 Apr 2010 06:54 AM

If not him then, anyone else.... please sort it out..... i m not getting the error.....!!!

nyone from DirectI...!!!!!

If not him then, anyone

shreydutta.28 @ 3 Apr 2010 06:54 AM

If not him then, anyone else.... please sort it out..... i m not getting the error.....!!!

nyone from DirectI...!!!!!

Heyy my program works fine if

pratyushag @ 4 Apr 2010 04:27 PM

Heyy my program works fine if everything is entered in uppercase and is accepted by thsite(im dng this wid asci code)

but if entered in lower case it goes wrong

i want to ask is that technically wrong?

The input section clearly

triplem @ 4 Apr 2010 04:29 PM

The input section clearly specifies the description of each card. Lowercase inputs are not mentioned as valid.

this is my code, i m getting

shreydutta.28 @ 4 Apr 2010 07:45 PM

this is my code, i m getting the wrong answer...............  nyone plz help...............

 

#include<iostream>
char *show[30]= {"royal flush","straight flush", "four of a kind",
"full house", "flush","straight","three of a kind",
"two pairs","pair","high card"} ;
char b[20];
void sort(){
int a,bc;
for(int i=0;i<13;i+=3){
for(int j = i+3;j<13;j+=3){
switch(b[i]){
case 'A': a = 1;break;
case 'T': a = 10;break;
case 'J': a = 11;break;
case 'Q': a = 12;break;
case 'K': a = 13;break;
default: a = b[i] - 48;
}
switch(b[j]){
case 'A': bc = 1;break;
case 'T': bc = 10;break;
case 'J': bc = 11;break;
case 'Q': bc = 12;break;
case 'K': bc = 13;break;
default: bc = b[j] - 48;
}            
if(a>bc){
b[i] = b[i] + b[j];      
b[i+1] = b[i+1] + b[j+1];

b[j] = b[i] - b[j];
b[j+1] = b[i+1] - b[j+1];

b[i] = b[i] - b[j];
b[i+1] = b[i+1] - b[j+1];
}
}
}
}
int ifnW_sequence(){
if(b[0]=='J'||b[0]=='Q'||b[0]=='K') return 0;
int bc,a;
bool sequence;
(b[0]=='A')?(bc = 1):((b[0]=='T')?bc=10:(bc = b[0]-48));
for(int i=3; i<13; i+=3){
switch(b[i]){
case 'A': a = 1;break;
case 'T': a = 10;break;
case 'J': a = 11;break;
case 'Q': a = 12;break;
case 'K': a = 13;break;
default: a = b[i] - 48;
}
if((i==3)&&((a-bc)==9)&&(bc=a)) continue;
else if((a-bc)!=1) return 0;
bc  = a;
}
if(b[0]=='A'&&b[3]=='T') return 2;
else return 1;
}       
bool ifSameSuit(){
char S = b[1];
for(int i=4;i<14;i+=3){
if(b[i]-S)return false;
S = b[i];
}
return true;
}
void count(int &c1, int &c2){
char S;c1 =1;c2 =1;
int cn = 0;
while(c1<2&&cn<12){
S = b[cn];
for(int i = (cn+3);i<14;i+=3){
if(S!=b[i]){cn = i;break;}
else c1++;
}
}
while(c1>1 && c1<4 && c2<2 && cn<12){
S = b[cn];
for(int i = (cn+3);i<14;i+=3){
if(S!=b[i]){cn = i;break;}
else c2++;
}
}
}
int hand(){
sort();
bool samesuit;
samesuit = ifSameSuit();
int seq = ifnW_sequence();
if(samesuit&&(seq==2)) return 0;
if(samesuit&&(seq==1)) return 1;
if(samesuit && (!seq)) return 4;
if((!samesuit)&& seq) return 5;
else{
int c1, c2;
count(c1,c2);
if(c1==4) return 2;
if((c1==3&&c2==2)||(c1==2&&c2==3)) return 3;
if(c1==3) return 6;
if(c1==2&&c2==2) return 7;
if(c1==2) return 8;
else return 9;
}
}


int main(){
int n;
std::cin>>n;
for(int i=0;i<n;i++){
std::cin.ignore();
std::cin.get(b,15);
std::cout<<show[hand()]<<'n';
}
system("PAUSE");
return 0;
}


some

shreydutta.28 @ 5 Apr 2010 12:43 PM

some one.......................?

Do not post your code in the

triplem @ 5 Apr 2010 01:34 PM

Do not post your code in the comments section.

This problem is pretty easy to debug yourself. Have you tried doing something like what Leppy suggests here? http://discuss.codechef.com/showthread.php?t=412

i m sorry for that.........

shreydutta.28 @ 5 Apr 2010 07:10 PM
i m sorry for that......... i had visited that page but did not get what he wanted to say........ it confused me......

COULD you please send me any

shreydutta.28 @ 5 Apr 2010 07:30 PM

COULD you please send me any test case which does not work with this......?

If you want FREE 50$ visit

therock18 @ 5 Apr 2010 10:29 PM

If you want FREE 50$ visit this site
http://www.pokerstrategy.com/u94MZ2 .
first you need to pass an easy quiz then you must select poker room you want to
play on and then you will get your free 50$ starting capital. Even if you don't
know much about poker, you can read some articles on this page and you will get
the answers for the quiz. Also you get nice rakeback if you are registered on
this page.

http://www.pokerstrategy.com/u94MZ2

Can anybody be kind enough to

dhruvasagar @ 17 Jun 2010 11:04 AM

Can anybody be kind enough to point me in the right direction please ? I have been working on the problem for 2 days now, I honestly think my solution is perfect, I am unable to find the flaws in it that Codechef seems to find rather easily & quickly!

I have created a thread in the forums as well!

On a different note, why can't I be shown the input codechef used on my problem during practice!? I mean isn't the whole purpose here to help us learn!?

If there's anything you need from my side, please let me know. I will port my solution to c++ in the evening & try again if that is what I need to get help around here...

According to me my solution

crazysaikat @ 17 Aug 2010 01:06 AM

According to me my solution is perfectly working I don't know what went wrong, admin please check it.. its a humble request... My solution ID is 308852..

@saikat... i have nt seen ur

calc_saransh @ 17 Aug 2010 02:14 AM

@saikat... i have nt seen ur code... but i think a common mistake can be in the case of straights.. cause.. there is an exception in that...

hi admin.. plzz consider my

devanshukashyap @ 5 Sep 2010 12:37 PM

hi admin.. plzz consider my recent post in Suggestions forum. Thanx

KD AH 2H 3H 4H what should

dabbcomputers @ 17 Sep 2010 01:39 PM

KD AH 2H 3H 4H what should the ouput for this cases......????

high card.

triplem @ 17 Sep 2010 02:14 PM

high card.

OUTPUT OF AH KH QH JH

nirajkant @ 18 Sep 2010 03:14 PM

OUTPUT OF

AH KH QH JH TH

GIVEN A CAN BE TAKEN AS HIGH ORDER ALSO IN SEQUENCE WILL IT BE A straight flush ???? or royal

Royal flush is the first item

triplem @ 18 Sep 2010 03:39 PM

Royal flush is the first item on the list in the problem statement, thus that is what you print.

Codechef roXXX!!!

SaladinII @ 8 Oct 2010 04:38 PM

Codechef roXXX!!!

Here are some more test cases

abhishek_t @ 5 Nov 2010 03:10 PM

Here are some more test cases for those who are getting the wrong answer, to test against every type of hand:

--

13
TS 3S TD AS TH
AD JD TD KD QD
JC KD JS JD JH
QS QC QD JH JD
2D 2S KH JH TH
5S 5C 5D 5H KD
4H 5H 7H 6H 3H
3S 5S AS 2S 4S
KS 7S 2H 9D 3C
KS KC 4H 4C 7C
5S JS QH 5D KS
2C 7C JC AC TC
5S 8D 4D 6H 7C

 

--

 

Correct answer is:

--

three of a kind
royal flush
four of a kind
full house
pair
four of a kind
straight flush
straight flush
high card
two pairs
pair
flush
straight

--

Dear Admin, I was working on

chiragjain1989 @ 14 Dec 2010 01:59 PM

Dear Admin,

I was working on this problem today, got it working correct in C. On submitting the solution here, it said "run time error". I devoted my 2 hours on debugging the code but in vain. I was working perfect on my machine. But somehow I just tried and changed only a single scanf( ) instruction by a cin instruction. And it got accepted. Can you please tell me what is the reason for that. Here is the code: http://pastebin.com/AZQiQRnx

I changed line no. 12 containing scanf() to cin.

Regards,

Chirag

Is T,J,Q,K,A of hearts a

jimmy valentine @ 16 Dec 2010 12:41 AM

Is T,J,Q,K,A of hearts a straight flush ?

kindly ignore the previous

jimmy valentine @ 16 Dec 2010 12:56 AM

kindly ignore the previous query...

is K,J,Q,T,9 straight flush or a flush ?

If they are all of the same

triplem @ 16 Dec 2010 02:59 AM

If they are all of the same suit, then that is a straight flush.

To Admin: What is AS 3S 4S 5S

rajneesh2k10 @ 31 Dec 2010 04:00 PM

To Admin:

What is AS 3S 4S 5S 6S ??

@admin: In 'straight flush'

rajneesh2k10 @ 31 Dec 2010 04:16 PM

@admin:

In 'straight flush' condition, what does the line "Ace can act as both highest and the lowest card" mean?

Lowest part is clear, it means A,2,3,4,5 in same suit will make a straight flush. But how can Ace act as highest card here??

AS 3S 4S 5S 6S is a

triplem @ 1 Jan 2011 02:44 AM

AS 3S 4S 5S 6S is a flush.

A2345 and TJQKA are both straights.

@Speedy: Thanks a lot! Fixed

rajneesh2k10 @ 1 Jan 2011 10:43 AM

@Speedy: Thanks a lot! Fixed that. Yet struggling. :)

Have a look at it, if you can figure out some wrong outputs: http://www.codechef.com/viewsolution/409940

Happy New Year to you and your family!! Have a prosperous 2011. :)

@Admin: Don't know which

rajneesh2k10 @ 1 Jan 2011 07:37 PM

@Admin:

Don't know which condition I am missing. Please provide an exhaustive test suite. Give some test cases which is satisfied by more than one hand.

If possible please give me a test case which fails my solution.

http://www.codechef.com/viewsolution/410372

Is there any I/O issue with

rajneesh2k10 @ 1 Jan 2011 08:05 PM

Is there any I/O issue with my solution? I have checked it thoroughly for many test cases. I couldn't come across Wrong Answer condition.

It is your job to provide an

triplem @ 2 Jan 2011 07:52 AM

It is your job to provide an extensive test suite - that is one of the key steps in solving any problem. Nobody else is going to do all the hard work for you :P You could also do something like run your program on all possible hands and count how many of each type you print - you can then compare that with the theoretical values which you can calculate by hand.

@Stephen: Thanks a lot! Even

rajneesh2k10 @ 7 Jan 2011 07:48 PM

@Stephen:

Thanks a lot! Even if you don't hint me it motivates me to help myself! Done!

Try out the test cases given

KK123 @ 19 Jan 2011 06:47 PM

Try out the test cases given by amritpal and abhishek.... bcoz i think they cover almost all the cases...

FRIENDS KINDLY HELP IN MY

sukusuku @ 24 Mar 2011 10:21 AM

FRIENDS KINDLY HELP IN MY CODING IS THERE ANY WRONG .ITS JUST WORKING IN MY SYSTEM FOR ALL KIND OF INPUT BUT IT IS SHOWING ERROR IN CODECHEFF.HERE IS MY CODE THANKS IN ADVANCE

CODE:

 

#include<stdio.h>

#include<string.h>

char const suit[]={'S','C','D','H'};

char const cards[]={'A','2','3','4','5','6','7','8','9','T','J','Q','K'};

char s[15];

int c[4]={0},d[13]={0};

void checkfrequency()

{

int i,j;

for(i=1;i<14;i=i+3){

for(j=0;j<4;j++)

{

if(suit[j]==s[i])

c[j]+=1;

 

}

for(j=0;j<13;j++)

{

if(cards[j]==s[i-1])

d[j]+=1;

 

}

 

}

}

 

int checkSameSuit()

{

int i;

for(i=1;i<11;i=i+3){

if(s[i]!=s[i+3])

return 0;

}

 

return 1;

 

}

int sumofcards()

{

char p[5];

int k;

p[0]=s[0];

p[1]=s[3];

p[2]=s[6];

p[3]=s[9];

p[4]=s[12];

k=p[0]+p[1]+p[2]+p[3]+p[4];

return k;

}

int checkSequence()

{

int k=sumofcards();

if(k==271||k==260||k==265||k==270||k==275||k==306||k==326||k==352||k==371)

return 1;

else

return 0;

}

int checkTopCards()

{

int k=sumofcards();

if(k==379)

return 1;

else

return 0;

}

int checkForeRank()

{

int i;

 

for(i=0;i<13;i++){

if(d[i]==4)

return 1;

}

return 0;

}

int checkThreeRank()

{

int i;

 

for(i=0;i<13;i++){

if(d[i]==3)

return 1;

}

 

return 0;

}

 

int checkTwoRank()

{

int i,count=0;

for(i=0;i<13;i++){

if(d[i]==2)

count++;

}

 

return count;

}

void flush(){

int i;

for(i=0;i<13;i++){d[i]=0;}

for(i=0;i<4;i++){c[i]=0;}

 

}

int main()

{

int b[6]={0},test;

scanf("%d",&test);

while(test--)

{

fflush(stdin);flush();

gets(s);

checkfrequency();

b[0]=checkSameSuit();

b[1]=checkSequence();

b[2]=checkTwoRank();

b[3]=checkThreeRank();

b[4]=checkForeRank();

b[5]=checkTopCards();

if(b[0]&&b[5])

printf("nroyal flush");

else if(b[0]&&b[1])

printf("nstraight flush");

else if(b[4])

printf("nfour of a kind");

else if((b[2]==1)&&b[3])

printf("nfull house");

else if(b[0])

printf("nflush");

else if(b[1])

printf("nstraight");

else if(b[3])

printf("nthree of a kind");

else if(b[2]==2)

printf("ntwo pairs");

else if(b[2]==1)

printf("npair");

else

printf("nhigh card");

}

return 0;

}

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com