Thread: sequential number test View Single Post
2012-02-08, 11:54   #4
axn

Jun 2003

23×19×31 Posts

Quote:
 Originally Posted by Bundu Ok that is a completely new one on me I will do some research on this area of javascript and get back to you! will a bitwise approach handle potential repeated numbers in the sequence? for example [2,3,3,3,4,5,6] is a straight but does not have 5 sequential numbers?
It will detect it correctly as a straight. The repeated application of "|" (or) operator on the same bit will not cause any problem.

EDIT:- Sample code
Code:
function is_straight(v)
{
// v is a vector of card values

var x = 0;
var i;
for(i=0; i < 7; i++)
{
x |= (1 << v[i]); // v[i] is assumed to be between 1 and 13, where 1 = 'A' and 13 = 'K'

if (v[i] == 1) // special check for Ace
x |= (1 << 14);

}
x &= (x << 1) & (x << 2) & (x << 3) & (x << 4);

return (x > 0); // return result
}
call with "is_straight([1,2,3,4,5,6,7])"

Last fiddled with by axn on 2012-02-08 at 12:04 Reason: Stupid loop bug