I have a question with grouping brackets together in a for loop. I use a for loop to store tournament results in array but would like to group the results by their rounds. I want to group each round. Take a look at the code below and you can compare how I would like to change the code to match the desired output:
$tournament_size = 16;
$upper_bracket_total_matches = $tournament_size - 1;
$lower_bracket_total_matches = $tournament_size - 2;
for($i = 1; $i <= $upper_bracket_total_matches; $i++)
{
$upper_brackets[]= "[0,0]";
}
$upper_bracket_results = implode(",", $upper_brackets)
/* Upper Bracket Output
[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],
[0,0],[0,0],[0,0],[0,0],
[0,0],[0,0],
[0,0]
*/
/* Desired Upper Bracket Output
[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]]
*/
for($i = 1; $i <= $lower_bracket_total_matches; $i++)
{
$lower_brackets[]= "[0,0]";
}
$lower_bracket_results = implode(",", $lower_brackets);
/* Lower Bracket Output
[0,0],[0,0],[0,0],[0,0],
[0,0],[0,0],[0,0],[0,0],
[0,0],[0,0],
[0,0],[0,0],
[0,0],
[0,0]
*/
/* Desired Lower bracket Output
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]],
[[0,0]]
*/
Hope this is clear
thanks for any help
Karim.
Try this: it generates stuff based on the size:
<?php
$tournament_size = 8;
$rounds = log($tournament_size) / log(2);
$upper_bracket_results = array();
$lower_bracket_results = array();
$curr = $tournament_size;
for ($i = 0; $i <= $rounds; $i++) {
$inner = array();
for ($i2 = 0; $i2 < $curr; $i2++)
$inner[] = array(0, 0);
$curr /= 2;
$upper_bracket_results[] = $inner;
}
$curr = $tournament_size / 2;
for ($i = 0; $i < $rounds; $i++) {
$inner = array();
for ($i2 = 0; $i2 < $curr; $i2++)
$inner[] = array(0, 0);
$lower_bracket_results[] = $inner;
$lower_bracket_results[] = $inner;
$curr /= 2;
}
echo "Upper:\n\n";
echo json_encode($upper_bracket_results);
echo "\n\nLower:\n\n";
echo json_encode($lower_bracket_results);
Output:
Upper:
[[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],[[0,0],[0,0],[0,0],[0,0]],[[0,0],[0,0]],[[0,0]]]
Lower:
[[[0,0],[0,0],[0,0],[0,0]],[[0,0],[0,0],[0,0],[0,0]],[[0,0],[0,0]],[[0,0],[0,0]],[[0,0]],[[0,0]]]
Related
Hi how can i optimize my code ?
$time_start = microtime(true);
$max = 2000;
$vx = [];
for ($i = 0; $i < $max; $i++) {
$vx[$i] = $i;
}
$vy = [];
for ($i = 0; $i < $max; $i++) {
$vy[$i] = $i;
}
$z = [];
$nb = 0;
foreach ($vx as $kx=>$x) {
foreach ($vy as $ky=>$y) {
$z[$x][$y] = cos($x*$x)-sin($y*-2);
$nb++;
}
}
$time_end = microtime(true);
echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values';
And after this
$time_start = microtime(true);
$max = 2000;
$vx = [];
$vy = [];
for ($i = 0; $i < $max; $i++) {
$vx[$i] = $i;
$vy[$i] = $i;
}
$z = [];
$nb = 0;
foreach ($vx as $kx=>$x) {
foreach ($vy as $ky=>$y) {
$z[$x][$y] = cos($x*$x)-sin($y*-2);
$nb++;
}
}
$time_end = microtime(true);
echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values';
The foreach loops are not necessary nor are the 2 arrays you build. It can all be done in 2 for loops like this
$max = 10;
$z = [];
for ($i = 0; $i < $max; $i++) {
for ($j = 0; $j < $max; $j++) {
$z[$i][$j] = cos($i*$i)-sin($j*-2);
}
}
print_r($z);
And after this
$time_start = microtime(true);
$max = 2000;
$vx = [];
$vy = [];
for ($i = 0; $i < $max; $i++) {
$vx[$i] = $i;
$vy[$i] = $i;
}
$z = [];
$nb = 0;
for ($i = 0; $i < $max; $i++) {
for ($j = 0; $j < $max; $j++) {
$z[$i][$j] = cos($i*$i)-sin($j*-2);
}
}
$time_end = microtime(true);
echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values';```
can i optimise the double foreach ?
I'm trying to look for a number with maximum divisors in a range of 1 - 10000.
I succeeded, but then I wish to verify if there exist more than two max divisors and print them out. My array is really the problem. How can I clear an array and assign a new integer to it in an if else if statement?
Here is what I have tried:
function countDivisors(){
$input = 10000;
$maxNumOfDiv = -1;
$intWMaxDivs = -1;
$curNumOfDiv = 0;
$arr = array();
for($i=1; $i <= $input; $i++) {
$curNumOfDiv = 0;
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0)
$curNumOfDiv++;
}
if($curNumOfDiv = $maxNumOfDiv){
$arr[] = $i;
$intWMaxDivs = $i;
$maxNumOfDiv = $curNumOfDiv;
} else if($curNumOfDiv > $maxNumOfDiv){
$arr = array();
$arr[] = $intWMaxDivs
$maxNumOfDiv = $curNumOfDiv;
}
}
for ($i; $i < count($arr); $i++){
echo $arr[$i]['intWMaxDivs'];
echo $arr[$i]['maxNumOfDiv'];
}
$div = [];
$maxDivKey = false;
$maxDiv = 0;
for($i = 1; $i <= 10000; $i++) {
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0){
$div[$i][] = $i.'/'.$j.'='.$i/$j;
}
if($j == $i-1){
$count = count($div[$i]);
$div[$i]['count'] = $count;
if($maxDiv < $count){
$maxDiv = $count;
$maxDivKey = $i;
}
}
}
}
echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);
I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.
<?php
$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();
$max_divs_arr=array();
for($i=$start_num;$i<=$max_num;$i++)
{
$divs=0;
$div_array=array();
for($j=$start_num;$j<=$i;$j++)
{
if($i%$j==0)
{
$divs++;
$div_array[]=$j;
}
}
if($divs==$max_divs)
$max_divs_arr[$i]=$div_array;
if($divs>$max_divs)
{
$max_divs_arr=array();
$max_divs=$divs;
$max_divs_arr[$i]=$div_array;
}
}
foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);
I have created an array dynamically like this way
$names = array();
for ($i = 0; $i < 100; $i++) {
$names[] = $i;
}
then created part
$parts = count($names) / 20;
and created a sub array then loop through the parts
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i = 0; $i < count($newarray); $i++) {
echo $i;
}
$j = $j + 20;
}
The problem is that this code displays from zero to 19 It doesn't display the other parts
Both the inner and outer loops use the same control variable $i, so just change the inner one...
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i1 = 0; $i1 < count($newarray); $i1++) {
echo $i1;
}
$j = $j + 20;
}
Using the following code I can get the DCT of an image in PHP. Then I need to convert this back in to the compressed image. How can I achieve that?
<?php
$results = array();
$image1 = "baboon.jpg";
$ima = ImageCreateFromJPEG($image1);
$N1 = imagesx($ima);
$N2 = imagesy($ima);
$rows = array();
$row = array();
for ($j = 0; $j < $N2; $j++) {
for ($i = 0; $i < $N1; $i++)
$row[$i] = imagecolorat($ima, $i, $j);
$rows[$j] = dct1D($row);
}
for ($i = 0; $i < $N1; $i++) {
for ($j = 0; $j < $N2; $j++)
$col[$j] = $rows[$j][$i];
$results[$i] = dct1D($col);
}
print_r($results);
function dct1D($in) {
$results = array();
$N = count($in);
for ($k = 0; $k < $N; $k++) {
$sum = 0;
for ($n = 0; $n < $N; $n++) {
$sum += $in[$n] * cos($k * pi() * ($n + 0.5) / ($N));
}
$sum *= sqrt(2 / $N);
if ($k == 0) {
$sum *= 1 / sqrt(2);
}
$results[$k] = $sum;
}
return $results;
}
?>
Also I need to know how can I add some extra details like another message to this image too.. (image steganography). Please help. Thanks
In this loop, I'm trying to take the value of the variable, but to save code I want to use a For Loop to print it concatenating part of the variable with a number genereted into the loop. This is my Try.
<?php
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
for ($i=0; $i < 5; $i++) {
echo '$x'.$i;
}
?>
the result that I'm geting is
$x0$x1$x2$x3$x4
I want it to end up like this:
0123
Its supposed to be:
for ($i=0; $i < 5; $i++) {
echo ${"x$i"};
}
Sidenote: You'll have to define $x4 or terminate it to < 4 so you won't get a undefined index.
Try this :
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
for ($i=0; $i < 5; $i++) {
$y='x'.$i;
if(isset($$y)){
echo $$y;
}
}
Try this Code:
<?php
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
$string = '';
for ($i=0; $i < 5; $i++) {
$string .= $i;
}
echo $string;
?>