Im looking for someone to point me in the right direction to coding some statistical comparisons. Currently I query my database, and will get data back as follows:
main data set :
3,4,7,10,5,8,1,3,7
sets to compare with could be like this, and can have multiple sets.
4,5,6,9,10,2,3,4,6
Now i need to work out the difference between these two sets of data - for example, difference between 3-4 is 1. I then need to choose the biggest difference, the most agreed upon, and the lowest scoring.
How would you tackle coding this?
I would recommend to use the function array_walk(), passing the first array as the first parameter and the second array as the third, optional parameter. It would look like this:
<?php
$array_1 = array(3,4,7,10,5,8,1,3,7);
$array_2 = array(4,5,6,9,10,2,3,4,6);
// making a copy, because the callback function
// works on the actual value
$array_1_copy = $array_1;
echo '<pre>';
array_walk($array_1_copy, 'difference', $array_2);
echo "\nThe maximum difference is: ". max($array_1_copy);
echo "\nThe minimum difference is: ". min($array_1_copy);
echo '</pre>';
// the callback function, takes the 1st param as reference
function difference(&$value_1, $index_1, $array_2) {
$difference = abs($value_1 - $array_2[$index_1]);
echo "Difference between $value_1 and {$array_2[$index_1]} is $difference\n";
$value_1 = $difference;
}
?>
And the output for this code is:
Difference between 3 and 4 is 1
Difference between 4 and 5 is 1
Difference between 7 and 6 is 1
Difference between 10 and 9 is 1
Difference between 5 and 10 is 5
Difference between 8 and 2 is 6
Difference between 1 and 3 is 2
Difference between 3 and 4 is 1
Difference between 7 and 6 is 1
The maximum difference is: 6
The minimum difference is: 1
$max_diff = 0;
for ($i = 0; $i < (min(count($array1), count($array2));$i++){
if (($array1[$i]-$array2[$i]) > $max_diff ) $max_diff = $array1[$i]-$array2[$i];
}
echo $max_diff;
Something like that...Didn't actually tested it, but that's the idea.
Related
I have a PHP problem.
I need to write a number from a sets of digits 0-9. Each set has 10 digits, each digit once.
I need to count the number of sets that I have to use to write the number.
For example number 10 is written from one set, but number 300 uses 2 sets because it has two zeros.
But, the problem is that 6 and 9 are considered the same. They can be rotated by 180 degrees.
Number 266 will be using one set, 369 also is using one set, but 5666 is using 2 sets.
I would be very grateful if you could somehow help me.
Here is how I have started and stuck up, have no more clue how to loop through it. Tried many things, nothing successful.
<?php
function countSet($num) {
$array = str_split($num);
$statarr = [0,1,2,3,4,5,6,7,8,9];
$a1 = $array; $a2 = $statarr;
$result = array_intersect($a1,$a2);
$count = array_count_values($result); }
?>
If you just need to know how many sets you need for a number, you can solve by counting the numbers. For the case of the 9, just replace every 9 by a 6 and divide the number of 6 by two. Something like this (sorry if there's any syntax error, I'm on mobile):
function countSet($input) {
// Convert the input into a string and replace every 9 by a 6. Then convert it to array
$numbers = str_split(str_replace("9", "6", strval($input)));
// Count occurrences for each number
$usedNumbers = array_count_values($numbers);
// If we have a 6, must divide it by 2 (6 represents 6 and 9
if (array_key_exists("6", $usedNumbers)) {
$usedNumbers["6"] = ceil($usedNumbers["6"] / 2);
}
// Now we just need to know the max number of occurrences for a single number as our result.
return max($usedNumbers);
}
See an online demo here
So, I,been trying to write a code for a "Dice generator" and I want it to generate random numbers (from 1-6) a x numbers of times.
The x number of times is given as a parameter by the user through the terminal, with this "$argc argv" function, but this is not really important.
What I want to know is: how do I generate random values x number of times?
FOR EXAMPLE:
User input: 4
Output: 5 3 6 8
User input: 3
Output: 5 1 2
This is what I am trying to write. I used the array_rand function with the array as a parameter, and the number of times I want as the second parameter, but It does not work! What am I not getting here?
<?php
if ($argc < 2) {
print "You have to write at least one parameter" .PHP_EOL;
exit(1);
}
//This is the variable that corresponds to the number of times the user will give on the Terminal as a parameter.
//$randoms = $argv[1];
$num = 3;
$diceNumbers = [1, 2, 3, 4, 5, 6];
$keys = array_rand($diceNumbers, $num);
print $diceNumbers[$keys[0]]." ".$diceNumbers[$keys[1]] .PHP_EOL;
?>
Given your use case is for a 'dice roll' I wonder if you would be better off using a cryptographically secure random number generation function like random_int() rather than array_rand(), which is not.
You can then use a for loop for your output as you know in advance how many times you want the loop to run.
$num = 3;
for($i = 0; $i < $num; $i++){
print random_int(1,6) . PHP_EOL;
}
The array_rand(arry, n) function returns an array with length n, composed of elements from arry. It looks like you did everything right, however when you print it you only ask for the first 2 random numbers. If you want to print all of the numbers, you will need a for/foreach loop.
foreach($keys as $key) {
print $key . " ";
}
I want to get my output like this
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
I am trying like this:
<?php
for($a=1; $a<=16; $a++)
{
for($b=$a; $b>=1; $b--)
{
echo "$b";
}
echo "<br>";
}
?>
The above code gives me the wrong output.
Let's debug.
You are starting from 1 in your outer loop and in your inner loop, you are going from $a till 1 times.
This doesn't comply with your requirements because we have to print an increasing sequence in each row.
You can also notice that every number in a row differs by 4.
So, logic would be like below:
Pseudocode:
rows = 4
starting_number = 1
loop from 1 to rows
number = starting_number
loop from 1 to 4 // since each row has 4 numbers
print number
number += 4
print new_line
starting_number++
Demo: https://3v4l.org/9YjIP
i am using this code
<?php
function random()
{
return rand(1111111111,9999999999);
};
for ($x = "1";$x <= "5";$x++)
{
echo $x." : ".random()."<br>";
};
echo "<hr>";
?>
some outputs :
1 : 1303960718
2 : 1308203081
3 : 1280148745
4 : 1263151923
5 : 1124814399
i tried generating more numbers and all of it starts with 1
i tried to used rand() directly and the same thing happend
Run this code and you will get your answer yourself
return rand(2147483647,9999999999);
Then try running
echo getrandmax();
Depending upon your system you might get something like 2147483647
That means your upper limit is pretty much useless beyond that number. And on certain systems that max can even be lower than that. You also have to research about integer overflow.
Now if you were to go easy on your system and remove 1 digit from your number and make the new range
return rand(111111111,999999999);
Then your code would work just fine, because there are no overflows.
///PROBLEM SOLVED. SEE MY ANSWER BELLOW.///
I have an array that randomly generates TRUE values throughout it. I
specified the number of TRUE values i want and it works like a charm.
I was toying around with it`s values and generating certain actions
based on wether or not the value is TRUE. Problem is i need to do this
again, this time using the TRUE values to form an array from which i
need a certain number of TRUE values.
I.E. I wanted 3 TRUE values out of 5. The code gave me the 3 TRUE
values on random iterations:
[1]=>1; [1]=>;
[2]=>1; [2]=>1;
[3]=>; [3]=>1;
[4]=>; [4]=>;
[5]=>1; [5]=>1;
This is all fine and dandy. Now i need TRUE values on 2 out of the 3
previous values. Consider i am taking 5 bites out of an apple. On 3
occasions i choke on it, from which in 2 cases i lose 2,3 teeth.
I want to print the outcome.
"Took a bite"
"Took a bite and choked"
"Took a bite"
"Took a bite , choked , lost 1 tooth and got 9 left" (i have a total of 10 teeth)
"Took a bite , choked , lost 2 teeth and got 7 left"
This is what i need to see printed after the 3/5 and 2/3 random
calculations occured. If i took a bite on the [2],[4] and [5]
iterations, i must lose tooth 2 times randomly on those exact
iterations.
Sorry for this example.
///PROBLEM SOLVED. SEE MY ANSWER BELLOW.///
2) Lastly, how can i store what happened after the code ran? Like "After you ate that apple, you choked 3 times lost 3 teeth and still got 7 left".
The way i did it does not work and returns "me loosing 1 tooth and having 9 left despite the fact that my calculated value is 7", not stacking the values of my ordeal.
I know these questions are silly, but i searched everywhere for information, read manuals and stuff and cannot put the pieces together...
The method used in the " random 3/5" case:
$spots = array();
while (count($spots) < number) {
$rand = rand(1,36);
if (!isset($spots[$rand])) {
$spots[$rand] = TRUE;
}
}
$spots = $spots + array_fill(1, number, FALSE);
ksort($spots); /// credits to Nick J.
Then did a foreach($spots as $k => $v)
Then did my code statements, starting with if ($v == 1), do code...
And gave me a random 3/5 list like the one i posted first.
Thank you in advance,
Vlad
Ok, i edited my answer as i have found the solution to problem number 1.
The catch was to scan the array of elements that are FALSE, unset them, work with that further on as it will randomly select elements from the TRUE elements of the original array.
Hope this comes in handy. I will post the code here:
$spots = array();
while (count($spots) < number) {
$rand = rand(1,constant);
if (!isset($spots[$rand])) {
$spots[$rand] = TRUE;
}
}
$spots = $spots + array_fill(1, constant, FALSE);
ksort($spots);
print_r($spots) ;
foreach($spots as $v)
{
if($v != 1)
{
unset($spots[array_search($v,$spots)]);
}
}
print_r($spots);
If example array:
$spots = array("1","2","3","4","5");
And i want 3 random numbers to be picked (TRUE = 1 , FALSE = nothing)
then the output is:
[1]=>1;
[2]=>1;
[3]=>;
[4]=>;
[5]=>1;
If i want 2 random numbers out of the 3 just got, then the final output is:
[1]=>1;
[2]=>1;
[5]=>1;