Name that Poker hand - Tiny Code Challenge

?

[quote]AC 8C QA JH TC: high card
AC 8C QA JH TC: one pair
Exception in thread “main” java.lang.IllegalStateException
at Poker.main(Poker.java:39)
[/quote]

[quote=“richierich,post:60,topic:47415”]

Cant you omit the “=0” when declaring the int “n”? n gets defined anyways at the beginning of the for loop.

[/quote]
Lol, OK actually in my test data I “fixed” the invalid card QA! Not sure why you got an IllegalStateException though - when I run it it just reports the QA hand as a pair because of 2 'A’s (which is wrong of course).

Do we actually have to handle invalid inputs, or just that particular odd one? My solution definitely has other bugs if it has to handle anything.

Yep looks like it - nice one!

Changed that ‘QA’ (lol, can’t believe we didn’t see that) to a ‘QS’:

Passed the initial battery, but the permutation generator is the real test, which it failed quickly:

[quote]AH 3H 4H 5H 6H
Riven: flush
Richy: straight flush
[/quote]

Made a more “extensible” test framework using Riven’s permutation gen with our 3 submissions:

http://pastebin.java-gaming.org/0a2155d9e0717

EDIT: fixed duplicate test

Ah cool - didn’t see the permutation tester :slight_smile:

This version gets much further, and is a few characters longer, 325 or 330 I think.


int h(char[]g) {
  int i=0,f=0,r=0,s=0,p=0,t=0,q=0,n;
  for(;i<18;i++){n=0;for(char x:g)if(x=="A23456789TJQKACDHS".charAt(i))n++;
  if(i<14){if(n==1){s++;if(i==13&&s==5)r=1;}else if(s<5)s=0;
  if(i<13){if(n==2)p++;if(n==3)t++;if(n==4)q++;}}
  if(n==5)f=1;}s=(s<5)?0:1;for(i=0;new int[]{f&s&r,f&s,q,t&p,f,s,t,p-1,p,1}[i]<=0;i++);
  return 99-i;
}

Reaches this:

2/52, 3/52
Exception in thread "main" 2H 3H 4H 5H 6H
	Riven: royal flush
	Richy: straight flush

Which seems like a good time to quit for the night :slight_smile:
Will try out the new test framework tomorrow…

I don’t know which version of Riven’s you’re testing against, but that’s definitely a straight flush, and your new code verifies successfully on the tester. Congratz!

EDIT: golf’d it a bit, was 325, now 304. No algorithmic changes.

int h(char[]g){
	int i=-1,f=0,r=0,s=0,p=0,t=0,q=0,n;for(;i++<17;){n=0;
	for(int x:g)if(x=="A23456789TJQKACDHS".charAt(i))n++;
	s=i<14&n>0?s==4&i==13?s+(r=1):s+1:s<5?0:s;
	if(i<13){if(n==2)p++;if(n==3)t++;if(n==4)q++;}f=n==5?1:f;}
	s=s<5?0:1;for(i=0;new int[]{f&s&r,f&s,q,t&p,f,s,t,p-1,p,1}[i]<=0;i++);
	return 99-i;
}

Yay i also achived 7.0.

I would be interested in seeing how our implementations differ…

my solution:


class S{public static void main(String[]a){for(int i=0;i<100;)System.out.println((++i%3<1?"Fizz":"")+(i%5>0?i%3>0?i:"":"Buzz"));}}


-   new int[]{...}[i]<=0
+   new int[]{...}[i]<1

-   s=s<5?0:1;for(i=0;new int[]{f&s&r,f&s,q,t&p,f,s,t,p-1,p,1}[i]<1;i++);
+   s=s<5?0:1;for(i=0;new int[]{f&s&r,f&s,q,t&p,f,s,t,p-1,p,1}[i++]<1;);

I don’t think it would take many changes to get something which can be posted to http://codegolf.stackexchange.com/questions/6494/name-the-poker-hand

@moogie
You’re just going to give away the answer like that?

I actually feel kind of dumb because I had that solution (a thing or two moved around) for the longest time (the 6.3 pt solution), but simply didn’t realize the class didn’t have to be named ‘Solution’ like many of the other challenges. :emo:

yep :slight_smile:

it frustrated me that I could not look at other submissions to see if there is any other “tricks” I could utilise! I try to share my insights to help others avoid similar frustration.

[quote=“moogie,post:73,topic:47415”]
That’s kind of the point :stuck_out_tongue:

Pretty sure that configuration is optimal, a few things can be switched, e.g. where i is incremented, etc.

Ah, if that is the case then I think I may not have the “correct” competition psyche… I personally like arriving at a solution in a collaborative manner.

If it offends I can remove the solution.

I used pretty much the same solution as moogie, except with a few of the ternary operators switched around.

What I find funny is that we all called the class ‘S’.

I wonder if a recursive solution might be smaller.

For fizzbuzz? No, because the method declaration would be too large. It’s such a tiny challenge there is no room for any fanciness.
The only one I used a recursive solution for was the sudoku solver.

Lol - nice one! I thought I’d golf’d it down pretty hard from the pre-obfuscated version.

[quote]I wonder if a recursive solution might be smaller.
[/quote]
For fizzbuzz it would surely have to recurse on main() if anything, for the reason BurntPizza said. But is there any way to get anything useful into and then out of the String[] parameter without lots of syntax? Null or not null could be used I suppose…

For poker (recurse once per card or something?) gut feeling is it would either need global variables or so many arguments the function signature would be unreasonably long.

Quick attempt, not going to get much smaller, and 27 chars more than the iterative:

class R{public static void main(String[]a){int i=a.length;if(i<101){System.out.println((i%3<1?"Fizz":"")+(i%5<1?"Buzz":i%3<1?"":i));main(new String[i+1]);}}}