Optimized Logic to find high score - php

Basically this is related to a squash application where we have 2 scores. One is from winner point of view and another from loser point of view.
eg.
Score1: 11-5,11-5,11-5 (Winner point of view)
Score2: 5-11, 5-11,5-11 (Loser point of view)
Now in my logic i want to find which is the winner score and which is the loser score.
I have written my logic in the below way and it does work. But i want to know if their is any other better/optimized way of writing this.
$high1 = 0;
$high2 = 0;
$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
$score1Array = explode(",",$score1);
$size = sizeof($score1Array);
for($i = 0; $i < $size; $i++) {
$checkscore1 = explode("-",$score1Array[$i]);
if($checkscore1[0] < $checkscore1[1]) {
$high1++;
}else if($checkscore1[0] > $checkscore1[1]) {
$high2++;
}
}
if($high1 > $high2) {
$winningScore = $score2;
$losingScore = $score1;
}else{
$winningScore = $score1;
$losingScore = $score2;
}
echo $winningscore;
echo $losingscore;

What about something like this:
function is_winning($score) {
$split_scores = preg_split('/(-|,)/', $score);
$wins = $losses = 0;
for($i = 0; $i < count($split_scores) / 2; $i += 2) {
if($split_scores[$i] > $split_scores[$i + 1])
$wins++;
if($split_scores[$i] < $split_scores[$i + 1])
$losses++;
}
return $wins > $losses;
}
Assuming $score is formatted as in your question. You can then use it like this:
$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
if(is_winning($score1)) {
$winning_score = $score1;
$losing_score = $score2;
} else {
$winning_score = $score2;
$losing_score = $score1;
}
echo $winning_score;
echo $losing_score;
The idea is to split the score into an array where the even numbered indexes have the left score and the odd numbered indexes the right score. We then count the number of wins and the number of losses. If there's more wins then losses then we return true since the score was a winning score. If there's not more wins then losses we simply return false.

This should work
$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
$l = $r = 0;
$score1_sets_arr = explode(',', $score1);
foreach ($score1_sets_arr as $set_score) {
$set_score_arr = explode('-', $set_score);
if ($set_score_arr[0] > $set_score_arr[1]) {
$l++;
} else {
$r++;
}
}
if ($l > $r) {
$winning_score = $score1;
$losing_score = $score2;
} else {
$winning_score = $score2;
$losing_score = $score1;
}

you can use this :
<?php
$high1 = 0;
$high2 = 0;
$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
$explode = explode(",",$score1);
for($i=0;$i< sizeof($explode);$i++){
$explode2= explode("-", $explode[$i]);
if($explode2[0] <= $explode2[1]){
echo $explode2[0]."-";
echo $explode2[1]." ";
}
}
echo "<br />";
for($i=0;$i< sizeof($explode);$i++){
$explode2= explode("-", $explode[$i]);
if($explode2[1] >= $explode2[0]){
echo $explode2[1]."-";
echo $explode2[0]." ";
}
}
?>

for Winner point of view, all big score in left,otherwise in right. so u can just detect the first score.
$score1Array = explode(",",$score1);
$checkscore1 = explode("-",$score1Array[$i]);
if($checkscore1[0] < $checkscore1[1]) {
echo $score2;
echo $score1;
}else{
echo $score1;
echo $score2;
}
Fix: above code is wrong,try this:
$score1value = eval(str_replace(",","+",$score1));
$score2value = eval(str_replace(",","+",$score2));
if($score1value < $score2value) {
echo $score2;
echo $score1;
}else{
echo $score1;
echo $score2;
}

Related

How to not create a big if list in this part of my code?

I don't want to create a big if list for all the story chapters of my website.
if $commentformname[$a] is equal to the number of the current chapter, then I make an array $commentonchapter1[$a] saying that a comment was inserted in the chapter 1, and chapter 2, and so on.
So if a comment was posted in the chapter 2, a array called commentonchapter2 will be created.
if ($sitecomments == true) {
$c = 0;
$i = 0;
$z = 0;
#defines the comment form name, name of who commented, the comment text and time (to be converted, example: 2020-04-09T15:53:07.580Za > 12:53 09/04/2020)
while ($c <= $commentsnumb) {
$commentformname[$z] = $comments[$i];
$i++;
$commenttername[$z] = $comments[$i];
$i++;
$commenttext[$z] = $comments[$i];
$i++;
$commenttime[$z] = $comments[$i];
$i++;
$c++;
$z++;
}
#shows the text of the comments tab
echo '<hr class="'.$sitehr2.'" />'."\n";
echo $divzoomanim.'<'.$n.'><p></p><br /><b>'.$tabnames[7].': '.$orangespan.$commentsnormalnumb.$spanc.' '.$icons[12].'</b><br /><br /><p></p></'.$n.'>'.$divc.'<hr class="'.$sitehr.'" />'."\n";
$i = 0;
$z = 0;
#converts the comment date and time
#example
#from 2020-04-09T15:53:07.580Za
#to 12:53 09/04/2020
while ($z <= $commentsnumb) {
$commenttime[$z] = substr($commenttime[$z], 0, -1);
$commenttime[$z] = date("H:i d/m/Y", strtotime($commenttime[$z]));
$z++;
}
$a = 0;
$z = 0;
$i = 0;
#styles the form name
#example
#from "pequenata-comment-7"
#to '<b>Commented on form "Pequenata-comment-7"</b>'
while ($a <= $commentsnumb) {
if ($lang == $langs[0] or $lang == $langs[1]) {
$commentformname[$i] = '<b>'.$commenttxt3.' '.$commenttxt5.' '.strtolower($formtxt).':</b> "'.ucwords($commentformname[$i]).'"';
}
if ($lang == $langs[2]) {
$commentformname[$i] = '<b>'.$commenttxt3.' '.$commenttxt4.' '.strtolower($formtxt).':</b> "'.ucwords($commentformname[$i]).'"';
}
$a++;
$z++;
$i++;
}
$a = 0;
$a2 = 1;
$i = 0;
$z = 0;
$c = 0;
$v = 0;
$b = 0;
$commentschapternumb = 0;
#Generates the array of the website comments
while ($c <= $commentsnumb) {
#Generates the comments of the website if the strlen of $commentformname[$a] is equal to 45, String: <b>Commented on form "Pequenata-comment-7"</b>
if (strlen($commentformname[$a]) == 45) {
$cmntsgeral[$b] = '<'.$m.' class="'.$textstyle2.'" style="text-align:left;border-width:3px;border-color:'.$bordercolor.';border-style:solid;"><div style="margin-left:5%;margin-right:5%;">'.'<br /><b>'.$a2.' - '.$commenttername[$i].'</b> - '.$commenttime[$i].'<br />'.$commentformname[$i].' '.'<hr class="'.$sitehr3.'" />'.$commenttext[$i].'<br /><br /><br /><br /><br />'.$divc.'</'.$m.'>'."<br />"."\n";
#Dispalyer disabled because of showing the wrong array (using $z, so it was tring to print a number on the array that didn't existed ($cmntschapter was incrementing on $z)
#echo $cmntsgeral[$z];
$a2++;
$b++;
}
#Generates the array of the chapter comments
if (strlen($commentformname[$a]) == 47) {
$cmntschapter[$v] = '<'.$m.' class="'.$textstyle2.'" style="text-align:left;border-width:3px;border-color:'.$bordercolor.';border-style:solid;"><div style="margin-left:5%;margin-right:5%;">'.'<br /><b>'.$a2.' - '.$commenttername[$i].'</b> - '.$commenttime[$i].'<br />'.$commentformname[$i].' '.'<hr class="'.$sitehr3.'" />'.$commenttext[$i].'<br /><br /><br /><br /><br />'.$divc.'</'.$m.'>'."<br />"."\n";
#Doesn't show the chapter comments because they shouldn't be showed in the website comments tab
#echo $cmntschapter[$z];
$v++;
$commentschapternumb++;
}
$a++;
$i++;
$z++;
$c++;
}
#Shows the website comments on the comments tab of the site
$z = 0;
while ($z <= $commentsnormalnumbtowrite) {
echo $cmntsgeral[$z];
$z++;
}
}

Decimals For negative PHP values

I am writing a bit of php code to output a random value
$max_mal = (3 - $oray);
$oray = 1;
$max = 100;
$total = 0;
for ($i = 0; $i < $max_mal; $i++){
$goli = mt_rand(3, 8);
$total += $goli;
$golis[] = $goli;
}
and for each loop goes here
foreach($golis as &$goli) {
$goli = floor(($goli / $total) * $max);
if ($goli == 0) {
$goli = 1;
}
}
$result = array_pad($golis, 3, -1);
shuffle($result);
$myresult = $result[0];
I am looking to get decimal values upto 5 numbers, but once a negative value comes it results out as 0.000-1 instead of -0.00001
$myresultb = str_pad($mario, 5, '0', STR_PAD_LEFT);
$myresultf = '0.'.$myresultb.'<br/>';
$total_score = 300;
echo $myresultf;
Secondly I am new to php learning so am I doing this PHP correct or it needs improvement
I have a div to show total score like this
<div id="total_score"></div>
and another div to show current score which value comes as echo $myresultf;
<div id="current_score"></div>
I want to update total score in real time with jquery wheneven button is clicked and <?php echo $myresultf ?> is refreshed in real time also
$("#play").click(function() {
var currentscore = $("#current_score").val();
var totalscore = $("#total_score").val();
how to do this.....
});
Try this:
$max = 100;
$oray = 1;
$max_mal = (3 - $oray);
$total = 0;
for ($i = 0; $i < $max_mal; $i++){
$goli = mt_rand(3, 8);
$total += $goli;
$golis[] = $goli;
}
foreach($golis as &$goli) {
$goli = floor(($goli / $total) * $max);
if ($goli == 0) {
$goli = 1;
}
}
$result = array_pad($golis, 3, -1);
shuffle($result);
$myresult = $result[0];
$negative_var=false;
if($myresult < 0)
{
$negative_var=true;
$myresult = 0-$myresult;
}
$myresultb = str_pad($myresult, 5, '0', STR_PAD_LEFT);
$myresultf = '0.'.$myresultb.'<br/>';
if($negative_var)
$myresultf="-".$myresultf;
$total_score = 300;
echo $myresultf;
simple use as follow:
$myresultb =str_replace('-','',$myresultb);
if($myresult == -1) {
$myresultf = '-0.'.$myresultb.'<br/>';
}
else {
$myresultf = '0.' . $myresultb . '<br/>';
}

PHP generate football schedule

I have 38 days and 20 clubs (EPL).
How can I generate not repeated matches for this clubs in this days (schedule)?
For example:
Day 1:
club1 - club2
club3 - club4
...
club19 - club 20
Day 2:
club1 - club3
club2 - club4
...
club20 - club18
Each club plays with other two games (home and away). Respectively do not play with himself.
My thinks:
$clubs1 = array();
$clubs2 = array();
$days = range(1, 38);
$calendar = array();
$pars = array();
$rows = Yii::app()->db->createCommand()
->select('id')
->from('clubs')
->queryAll();
foreach ($rows as $item) {
$clubs1[] = $item['id'];
$clubs2[] = $item['id'];
}
shuffle($clubs1);
shuffle($clubs2);
$total = (count($clubs1) * 2) - 2;
for ($j = 1; $j <= $total; $j ++) {
$day = $days[$j];
for ($i = 0; $i < count($clubs1); $i++) {
WHAT I SHOULD DO IN THIS BODY?
}
}
You need only one clubs array
1) remove $clubs2
2) rename $clubs1 to $clubs
3) remove whole for structure
//for testing: $clubs=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$countofteams=count($clubs);
$c=1;
for($j=0;$j<2;$j++) //home/away
for($i=1;$i<$countofteams;$i++){ //move teams
echo '----DAY '.$c++.'----<br>';
for($a=0;$a<$countofteams;$a++) //all teams are playing
echo 'Match '.$clubs[$a].' vs '.$clubs[($a+$i)%$countofteams].'<br>';
}
$team = array();
$pars = array();
$rows = Yii::app()->db->createCommand()
->select('id')
->from('clubs')
->queryAll();
foreach ($rows as $k => $item) {
$team[$k+1] = $item['id'];
}
$all_team = count($team);
$k = $all_team/2;
$days = range(7, 100, 2); // first halh of season
$days2 = range(55, 100, 2); // second half
// 1 tour
for ($i=1; $i <= $k; $i++) {
$pars[] = $days[0].'|'.$team[$i].'|'.$team[($all_team-$i+1)];
$pars[] = $days2[0].'|'.$team[($all_team-$i+1)].'|'.$team[$i];
}
// Next tours
for($i=2; $i<$all_team; $i++)
{
$team2 = $team[2];
for($y=2;$y<$all_team;$y++)
{
$team[$y] = $team[$y+1];
}
$team[$all_team] = $team2;
for($j=1;$j<=$k;$j++)
{
$pars[] = $days[$i - 1].'|'.$team[$j].'|'.$team[($all_team-$j+1)];
$pars[] = $days2[$i - 1].'|'.$team[($all_team-$j+1)].'|'.$team[$j];
}
}
Here's my solution, replace the $clubs array with your result set of club IDs from database. The only slight inaccuracy with real-world EPL is that the second half of the season will mirror the first half exactly.
See if this adaption of http://board.phpbuilder.com/showthread.php?10300945-Round-Robin-Generator is any better :) - just for first half of the season.
$clubs = array(
'che', 'swa', 'ast', 'manc', 'liv', 'tot', 'ars', 'sot', 'hul', 'stok', 'wham', 'qpr', 'sun', 'mutd', 'lei', 'new', 'eve', 'wba', 'cry', 'bur',
);
shuffle($clubs);
$num_players = count($clubs) - 1;
// Set the return value
$ret = '';
// Generate the pairings for each round.
for ($round = 0; $round < $num_players; $round++) {
$ret .= '<h3>' . ($round + 1) . '</h3>';
$players_done = array();
// Pair each player except the last.
for ($player = 1; $player < $num_players; $player++) {
if (!in_array($player, $players_done)) {
// Select opponent.
$opponent = $round - $player;
$opponent += ($opponent < 0) ? $num_players : 1;
$playerName = $clubs[$player];
$opponentName = $clubs[$opponent];
// Ensure opponent is not the current player.
if ($opponent != $player) {
// Choose colours.
if (($player + $opponent) % 2 == 0 xor $player < $opponent) {
// Player plays white.
$ret .= "$playerName - $opponentName $br";
} else {
// Player plays black.
$ret .= "$opponentName - $playerName $br";
}
// This pair of players are done for this round.
$players_done[] = $player;
$players_done[] = $opponent;
}
}
}
// Pair the last player.
if ($round % 2 == 0) {
$playerName = $clubs[$num_players];
$opponent = ($round + $num_players) / 2;
$opponentName = $clubs[$opponent];
// Last player plays white.
$ret .= "$playerName - $opponentName $br";
} else {
$opponent = ($round + 1) / 2;
// Last player plays black.
$ret .= "$opponentName - $playerName $br";
}
}
echo $ret;

For loop problem takes the third data as undefined

Controller
for ($x = 1; $x <= $numb; $x++)
{
echo $quanoutput = $this->input->post('quanoutput');
$barcodeoutput = $this->input->post('barcodeoutput');
$productsoutput = $this->input->post('productsoutput');
$outward_date=$this->input->post('outward_date');
$stock=$this->input->post('stock');
$warehouse_id =$this->input->post('warehouse_id');
$request_id =$this->input->post('request_id');
$warehouse=$this->input->post('warehouse');
$buyprice = $this->input->post('buyprice');
if ($productsoutput=='undefined'){
//$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
} else {
$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
}
}
Try starting your for loops at 0. (ie. j=0) and change the <= to just <.

Pseudo-code for shelf-stacking

Suppose I have some serially numbered items that are 1-n units wide, that need to be displayed in rows. Each row is m units wide. I need some pseudo-code that will output the rows, for me, so that the m-width limit is kept. This is not a knapsack problem, as the items must remain in serial number order - empty spaces at the end of rows are fine.
I've been chasing my tail over this, partly because I need it in both PHP and jQuery/javascript, hence the request for pseudo-code....
while (!items.isEmpty()) {
rowRemain = m;
rowContents = [];
while (!items.isEmpty() && rowRemain > items[0].width) {
i = items.shift();
rowRemain -= i.width
rowContents.push(i);
}
rows.push(rowContents);
}
Running time is Θ(number of items)
Modulus is your friend. I would do something like:
$items = array(/* Your list of stuff */);
$count = 0;
$maxUnitsPerRow = 4; // Your "m" above
while ($item = $items[$count]) {
if ($count % $maxUnitsPerRow == 0) {
$row = new row();
}
$row->addItemToRow($item);
$count++;
}
Here is an alternative php code ...
function arrayMaxWidthString($items, $maxWidth) {
$out = array();
if (empty($items)) {
return $out;
}
$row = $maxWidth;
$i = 0;
$item = array_shift($items);
$row -= strlen($item);
$out[0] = $item;
foreach ($items as $item) {
$l = strlen($item);
$tmp = ($l + 1);
if ($row >= $tmp) {
$row -= $tmp;
$out[$i] = (($row !== $maxWidth) ? $out[$i] . ' ' : '') . $item;
} elseif ($row === $maxWidth) {
$out[$i] = $item;
++$i;
} else {
++$i;
$row = $maxWidth - $l;
$out[$i] = $item;
}
}
return $out;
}
For what it's worth, I think I have what I was looking for, for PHP - but not sure if there is a simpler way...
<?php
// working with just a simple array of widths...
$items = array(1,1,1,2,1,1,2,1);
$row_width = 0;
$max_width = 2;
echo "Begin\n"; // begin first row
foreach($items as $item=>$item_width) {
// can we add item_width to row without going over?
$row_width += $item_width;
if($row_width < $max_width) {
echo "$item_width ";
} else if($row_width == $max_width) {
echo "$item_width";
echo "\nEnd\nBegin\n"; // end last row, begin new row
$row_width = 0;
} else if($row_width == 2* $max_width) {
echo "\nEnd\nBegin\n"; // end last row, begin new row
echo "$item_width";
echo "\nEnd\n"; // end new row
$row_width = 0;
if($item < count($items)) echo "Begin\n"; // new row
} else if($row_width > $max_width) {
echo "\nEnd\nBegin\n"; // end last row, begin new row
echo "$item_width";
$row_width = $item_width;
}
}
echo "\nEnd\n"; // end last row
?>

Categories