Now I know the basic logic behind finding a straight, and I assume that would include a pseudo of
function is_straight(array $cards) {
sort($cards);
if(($cards[4] - $cards[0]) == 5) {
//Code to make sure the cards in between are increment
//is straight.
}
}
would theoretically work for a 5 card check.
But how would one go for eliminating cards from the array of 7 cards to find a straight?
Would I have to individually check all 5 hand combinations within the 7 cards array?
so eliminate two cards from the $cards array and check that combination for a straight?
So I'm a little stuck on the logical side of this, rather than the code side.
In pseudo code
#filter doubles
cards = array_unique(cards)
sort(cards)
foreach cards as key, value:
if not key_exists(cards, key+4):
return false
if cards[key+4] == value + 4:
return true
longer potentially more explicit version
#filter doubles
cards = array_unique(cards)
sort(cards)
straight_counter = 1
foreach cards as key, value:
if not key_exists(cards, key+1):
return false
# is the following card an increment to the current one
if cards[key+1] == value + 1:
straight_counter++
else:
straight_counter = 1
if straight_counter == 5:
return true
function is_straight(array $array) {
$alpha = array_keys(array_count_values($array));
sort($alpha);
if (count($alpha) > 4) {
if (($alpha[4] - $alpha[0]) == 4) {
$result = $alpha[4];
return $result;
}
if (count($alpha) > 5) {
if (($alpha[5] - $alpha[1]) == 4) {
$result = $alpha[5];
return $result;
}
}
if (count($alpha) > 6) {
if (($alpha[6] - $alpha[2]) == 4) {
$result = $alpha[6];
return $result;
}
}
}
}
Assuming that $cards is an array containing cards values from 1 to 13, I think you need to evaluate with a difference of 4, not 5 :
5 - 1 = 4
6 - 2 = 4
7 - 3 = 4
etc.
You also need to add a specific logic for 10, J, Q, K, A
But for your specific question, what about :
function is_straight(array $cards) {
sort($cards);
if((($cards[4] - $cards[0]) == 4) ||
(($cards[5] - $cards[1]) == 4) ||
(($cards[6] - $cards[2]) == 4)) {
//Code to make sure the cards in between are increment
//is straight.
}
}
Related
What is the way to search the database (mysql/php code) for the following entries:
123XX
123XY
XYZ44
1X344
1Z344
Z23YY
The input letters are only X - Y - Z and the numbers from 0 to 9
They are all one number, which is (12344), so how can I show these results? The goal is to search for repeated entries.
Another example :
12XYY
X = 3,4,5,6,7,8,9,0
Y = 3,4,5,6,7,8,9,0
Provided that y is not equal to x or any apparent number (1,2)
And X is not equal to Y or any apparent number (1,2)
$number = "1XZYY";
$rnumber = str_replace(array('Y','X','Z'), "ـ", $number);
$lenNumber = strlen(5);
$duplicate = $mysqli->query("SELECT `number` FROM `listNumber` WHERE (length(`number`) = '$lenNumber' && `number` LIKE '%$rnumber%') OR (length(`number`) = '$lenNumber' && `number`LIKE '%$rnumber%')");
I tried many methods, but it was very slow in showing the results because I put the loop inside a loop to search for every number in the first loop
I understand you want to look for 12344, but some of the digits may be been redacted and replaced with a random capital letter in XYZ. For that, you can use a regular expression:
WHERE REGEXP_LIKE(foo, '^[XYZ1][XYZ2][XYZ3][XYZ4][XYZ4]$')
Demo
I would use PHP and occupy each digit into the correct position until I find a conflict. To prevent a double loop I use a dictionary helper object to hold values of X, Y and Z.
function match_str_to_number($str, $number)
{
if (strlen($number) != strlen($str)) {
return false;
}
$dict = ['X' => -1, 'Y' => -1, 'Z' => -1];
for ($i = 0; $i < strlen($number); $i++) {
if ($number[$i] != $str[$i]) {
// a number mismatch
if (!isset($dict[$str[$i]])) {
return false;
}
// a wildcard variable conflict
if ($dict[$str[$i]] != $number[$i] && $dict[$str[$i]] != -1) {
return false;
};
$dict[$str[$i]] = $number[$i];
}
}
return true;
}
echo match_str_to_number("XYZ44", "12344") ? "true" : "false";
echo match_str_to_number("XYZ4X", "12344") ? "true" : "false";
// output: truefalse
I have managed to create an algorithm to check the rank of a poker hand. It works 100% correctly, but it's very slow. I've been analysing the code, and the check straight function is one of the slowest parts of it.
So my question is, is there a better way of calculating whether a hand make a straight?
Here is some details:
7 cards, 2 from holder, 5 from board. A can be high or low.
Each card is assigned a value:
2 = 2
3 = 3
..
9 = 9
T = 10
J = 11
Q = 12
K = 13
A = 14
The script has an array of all 7 cards:
$cards = array(12,5,6,7,4,11,3);
So now I need to be able to sort this into an array where it:
discards duplicates
orders the card from lowest to highest
only returns 5 consecutive cards I.e. (3,4,5,6,7)
It needs to be fast; loops and iterations are very costly. This is what I currently use and when it tries to analyse say 15000 hands, it takes its toll on the script.
For the above, I used:
discard duplicates (use array_unique)
order cards from lowest to highest (use sort())
only return 5 consecutive cards (use a for loop to check the values of cards)
Does anyone have any examples of how I could improve on this? Maybe even in another language that I could perhaps look at and see how it's done?
Instead of working with array deduping and sorting, consider using a bitmask instead, and setting bits to 1 where the card value is set. A bitmask works like a Set datastructure and comes with additional advantages when it comes to detecting contiguous elements.
for ($i = 0; $i < count($cards); $i++) {
$card = $cards[$i];
// For each card value, set the bit
if ($card == 14) {
// If card is an ace, also set bit 1 for wheel
$cardBitmask |= 0x2;
}
$cardBitmask |= (1 << $card);
}
// To compare, you simply write a for loop checking for 5 consecutive bits
for($i = 10; $i > 0; $i--)
{
if ($cardBitmask & (0x1F << $i) == (0x1F << $i)) {
// Straight $i high was found!
}
}
Consider the Java implementation at this link. I've included it here:
public static boolean isStraight( Card[] h )
{
int i, testRank;
if ( h.length != 5 )
return(false);
sortByRank(h); // Sort the poker hand by the rank of each card
/* ===========================
Check if hand has an Ace
=========================== */
if ( h[4].rank() == 14 )
{
/* =================================
Check straight using an Ace
================================= */
boolean a = h[0].rank() == 2 && h[1].rank() == 3 &&
h[2].rank() == 4 && h[3].rank() == 5 ;
boolean b = h[0].rank() == 10 && h[1].rank() == 11 &&
h[2].rank() == 12 && h[3].rank() == 13 ;
return ( a || b );
}
else
{
/* ===========================================
General case: check for increasing values
=========================================== */
testRank = h[0].rank() + 1;
for ( i = 1; i < 5; i++ )
{
if ( h[i].rank() != testRank )
return(false); // Straight failed...
testRank++; // Next card in hand
}
return(true); // Straight found !
}
}
A quick Google search for "check for poker straight (desired_lang)" will give you other implementations.
You could just sort the cards and loop over them in an array - saving always the last card and compare them with the current one.
$cards = array(12,5,6,7,4,11,3);
sort($cards);
$last = 0;
$count = 0;
$wheel = false;
foreach ($cards as $card) {
if ($card == $last) {
continue;
} else if ($card == ++$last) {
$count++;
} else {
if ($last == 6) $wheel = true;
$count = 1;
$last = $card;
}
if ($count == 5 || ($card == 14 && $wheel)) {
echo "straight $last";
$straight = range($last - 4, $last);
break;
}
}
You may go like this, you don't need to sort or anything (assuming that 2 is 2 and 14 is ace):
$cards = [12,5,6,7,4,11,3];
function _inc(&$i) {
if ($i == 14)
$i = 2;
else
$i++;
return $i;
}
$straight = false;
for($i = 2; $i <= 14; $i++) {
$ind = $i;
if (!in_array($ind, $cards)) continue;
$s = [$ind, _inc($ind), _inc($ind), _inc($ind), _inc($ind)];
$straight = count(array_intersect($s, $cards)) == count($s);
if ($straight) break;
}
print $straight;
So, I have this requirement that I have two sets of data (stored in arrays, but can be anything). What I want to do is that I want to add these two sets together so that the final result count is 10.
The scenarios are:
Both sets can initially have more than 5 (or 10 for that matter). In which case it is easy - I just take 5 from each set and add them together and display
Either set could be less than 5. In which case, I should take whatever is available in that set. In the other set, I should take how-much-ever is required to make total 10, If the other set's count is low such that taking it does not make total to 10, then I should take it all and display whatever I got.
Based on this requirement, I am trying to write a logic which will give me the count required from each set, however the if-else-if-else is getting too complicated that I think I might be doing it incorrectly. Can anyone help me with creating a simpler logic to do what I need?
My current (incomplete and convoluted) logic is:
if($set1Count >= 5)
{
$requiredSet1Count = 5;
if($set2Count >= 5)
{
$requiredSet2Count = 5;
}
else
{
$requiredSet2Count = $set2Count;
if($requiredSet1Count > = (10 - $set2Count))
{
$requiredSet1Count = (10 - $set2Count);
}
else
{
$requiredSet1Count = $set1Count;
}
}
}
else
{
.....// I gave up by the time I reached here....
}
In above code $set1Count and $set2Count are the actual result counts in the two sets/arrays. The $requiredSet1Countand $requiredSet2Count are the dynamic counts I need which will tell me how many elements to extract from each set.
Any help is greatly appreciated!
I don't know a variant without ifs. Lets try to use one for every situation
function requiredSet($set1count, $set2count) {
// Both arrays together contain less than 10 items
if ($set1count + $set2count <= 10) {
$requiredSet1Count = $set1count; $requiredSet2Count = $set2count;
}
// 1st less than 5 elements
elseif ($set1count < 5) {
$requiredSet1Count = $set1count;
$requiredSet2Count = $set2count + $set1count > 10 ? 10 - $set1count : $set2count;
}
// 2nd - less than 5 elements
elseif ($set2count < 5) {
$requiredSet2Count = $set2count;
$requiredSet1Count = $set1count + $set2count > 10 ? 10 - $set2count : $set1count;
}
// Just take 5 elements in each
else $requiredSet1Count = $requiredSet2Count = 5;
return array($requiredSet1Count, $requiredSet2Count);
}
echo '<pre>';
var_export(requiredSet(1,2)); echo '<br>';
var_export(requiredSet(2,5)); echo '<br>';
var_export(requiredSet(2,7)); echo '<br>';
var_export(requiredSet(2,13)); echo '<br>';
var_export(requiredSet(13,11)); echo '<br>';
result
array ( 0 => 1, 1 => 2)
array ( 0 => 2, 1 => 5)
array ( 0 => 2, 1 => 7)
array ( 0 => 2, 1 => 8)
array ( 0 => 5, 1 => 5)
I have a comma delimited list of numbers which i am converting into an array and what i want to know about the list of numbers is if the numbers listed obey a natural ordering of numbers,you know,have a difference of exactly 1 between the next and the previous.
If its true the list obeys the natural ordering,i want to pick the first number of the list and if not the list obeys not the natural order,i pick the second.
This is my code.
<?php
error_reporting(0);
/**
Analyze numbers
Condition 1
if from number to the next has a difference of 1,then pick the first number in the list
Condition 2
if from one number the next,a difference of greater than 1 was found,then pick next from first
Condition 3
if list contains only one number,pick the number
*/
$number_picked = null;
$a = '5,7,8,9,10';
$b = '2,3,4,5,6,7,8,9,10';
$c = '10';
$data = explode(',', $b);
$count = count($data);
foreach($data as $index => $number)
{
/**
If array has exactly one value
*/
if($count == 1){
echo 'number is:'.$number;
exit();
}
$previous = $data[($count+$index-1) % $count];
$current = $number;
$next = $data[($index+1) % $count];
$diff = ($next - $previous);
if($diff == 1){
$number_picked = array_values($data)[0];
echo $number_picked.'correct';
}
elseif($diff > 1){
$number_picked = array_values($data)[1];
echo $number_picked.'wrong';
}
}
?>
The problem i am having is to figure out how to test the difference for all array elements.
No loops are needed, a little bit of maths will help you here. Once you have your numbers in an array:
$a = explode(',', '5,7,8,9,10');
pass them to this function:-
function isSequential(array $sequence, $diff = 1)
{
return $sequence[count($sequence) - 1] === $sequence[0] + ($diff * (count($sequence) - 1));
}
The function will return true if the numbers in the array follow a natural sequence. You should even be able to adjust it for different spacings between numbers, eg 2, 4, 6, 8, etc using the $diff parameter, although I haven't tested that thoroughly.
See it working.
Keep in mind that this will only work if your list of numbers is ordered from smallest to largest.
Try using a function to solve this... Like so:
<?php
error_reporting(0);
/**
Analyze numbers
Condition 1
if from number to the next has a difference of 1,then pick the first number in the list
Condition 2
if from one number the next,a difference of greater than 1 was found,then pick next from first
Condition 3
if list contains only one number,pick the number
*/
$number_picked = null;
$a = '5,7,8,9,10';
$b = '2,3,4,5,6,7,8,9,10';
$c = '10';
function test($string) {
$data = explode(',', $string);
if(count($data) === 1){
return 'number is:'.$number;
}
foreach($data as $index => $number)
{
$previous = $data[($count+$index-1) % $count];
$current = $number;
$next = $data[($index+1) % $count];
$diff = ($next - $previous);
if($diff == 1){
$number_picked = array_values($data)[0];
return $number_picked.'correct';
}
elseif($diff > 1){
$number_picked = array_values($data)[1];
return $number_picked.'wrong';
}
}
}
echo test($a);
echo test($b);
echo test($c);
?>
You already know how to explode the list, so I'll skip that.
You already handle a single item, so I'll skip that as well.
What is left, is checking the rest of the array. Basically; there's two possible outcome values: either the first element or the second. So we'll save those two first:
$outcome1 = $list[0];
$outcome2 = $list[1];
Next, we'll loop over the items. We'll remember the last found item, and make sure that the difference between the new and the old is 1. If it is, we continue. If it isn't, we abort and immediately return $outcome2.
If we reach the end of the list without aborting, it's naturally ordered, so we return $outcome1.
$lastNumber = null;
foreach( $items as $number ) {
if($lastNumber === null || $number - $lastNumber == 1 ) {
// continue scanning
$lastNumber = $number;
}
else {
// not ordened
return $outcome2;
}
}
return $outcome1; // scanned everything; was ordened.
(Note: code not tested)
To avoid the headache of accessing the previous or next element, and deciding whether it still is inside the array or not, use the fact that on a natural ordering the item i and the first item have a difference of i.
Also the corner case you call condition 3 is easier to handle outside the loop than inside of it. But easier still, the way we characterize a natural ordered list holds for a 1-item list :
$natural = true;
for($i=1; $i<$count && $natural; $i++)
$natural &= ($data[$i] == $data[0] + $i)
$number = $natural ? $data[0] : $data[1];
For $count == 1 the loop is never entered and thus $natural stays true : you select the first element.
I have 3 options, and they have to happen in a particular ratio.
Out of 10 times, this is the ratio:
8x Option 1
1x Option 2
1x Option 3
Right now I have the following:
if (rand(8,10)) {
option1();
} elseif (rand(1,10)) {
option2();
} elseif (rand(1,10)) {
option3();
}
But this gets option 1 way more than 8 times. Option 2 never occurs and option 3 rarely.
So, what is the correct way to achieve this?
Try with something like this.
$roll = rand(1,10);
if ($roll == 1) {
option2();
} elseif ($roll == 2) {
option3();
} else {
option1();
}
If random number is 1 (10% of the time), option 2.
If random number is 2 (10% of the time), option 3.
If random number is anything else (80% of the time), option 1.
$r = rand() / getrandmax();
if ($r <= 0.8) {
option1();
} elseif ($r <= 0.9) {
option2();
} else {
option3();
}
No, this is not the right way. You can use something like:
<?php
// Store in DB use of options 1, 2 and 3
// field value
// -----------------
// options1 0
// options2 0
// options3 0
// If $options1 = 8, $options2= 1 and $options3 = 1, then clear all values from DB
$random = rand(1,3);
if ($random == 1) {
if ($options1 < 9) { option1(); } else { // tryagain; }
} elseif (
...
// update DB with new value for used option
Sorry the code is slightly messed up (the if part) but I guess you can understand. Let me know if you need the whole code.