PokerProblem code: POKER |
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 |
Comments
SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:
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.

Fetching successful submissions

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.
@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.
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!
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
Thats nice to know :)
My first submission failed
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
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
please some one put the corner cases .. please ,....... :(
I have tried all examples of
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
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
@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
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
@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
If not him then, anyone else.... please sort it out..... i m not getting the error.....!!!
nyone from DirectI...!!!!!
If not him then, anyone
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
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
The input section clearly specifies the description of each card. Lowercase inputs are not mentioned as valid.
this is my code, i m getting
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
some one.......................?
Do not post your code in the
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.........
COULD you please send me any
COULD you please send me any test case which does not work with this......?
If you want FREE 50$ visit
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
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
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
@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
hi admin.. plzz consider my recent post in Suggestions forum. Thanx
KD AH 2H 3H 4H what should
KD AH 2H 3H 4H what should the ouput for this cases......????
high card.
high card.
OUTPUT OF AH KH QH JH
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
Royal flush is the first item on the list in the problem statement, thus that is what you print.
Codechef roXXX!!!
Codechef roXXX!!!
Here are some more test cases
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
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
Is T,J,Q,K,A of hearts a straight flush ?
kindly ignore the previous
kindly ignore the previous query...
is K,J,Q,T,9 straight flush or a flush ?
If they are all of the same
If they are all of the same suit, then that is a straight flush.
To Admin: What is AS 3S 4S 5S
To Admin:
What is AS 3S 4S 5S 6S ??
@admin: In 'straight flush'
@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
AS 3S 4S 5S 6S is a flush.
A2345 and TJQKA are both straights.
@Speedy: Thanks a lot! Fixed
@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
@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
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
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
@Stephen:
Thanks a lot! Even if you don't hint me it motivates me to help myself! Done!
Try out the test cases given
Try out the test cases given by amritpal and abhishek.... bcoz i think they cover almost all the cases...
FRIENDS KINDLY HELP IN MY
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;
}