Golf league payout to six finishing positions - php

I have a golf league of 40 individuals. We all throw money in a pot and pay out the first 6 places based on final score.
If there were no ties the pay out would be simple but often we have, for example, 2 people tied for first place, 3 people tied for second, 1 person alone in third, etc. The variations seem endless.
I've been trying to automate the calculated payouts for each place using PHP but have not been successful. Any suggestions, help, or pointing in the right direction would be much appreciated. I noticed that someone else tried to ask a similar question on this site but was not successful in framing the question. I'll try to do a better job.
Here is some data I've been playing around with:
$playerNumber=40;
$totalPoints=100;
Payouts:
$pointsFirst=0.6*$totalPoints;
$pointsSecond=0.2*$totalPoints;
$pointsThird=0.15*$totalPoints;
$pointsFourth=0.03*$totalPoints;
$pointsFifth=0.02*$totalPoints;
$pointsSixth=0.01*$totalPoints;
For the example given above and to pay out six places, we would calculate the payouts as follows:
If two people are tied for first place, we add first and second place points and divide by two.
If three people are tied for second place, we add third, fourth and fifth place points and divide by three.
If one person is alone in third, this person would win sixth place points.
I can count the number of players who are in or tied for a certain place.
$countFirst=2;
$countSecond=3;
$countThird=1;
$countFourth=2;
$countFifth=1;
$countSixth=2;
In this example the player scores would be 72, 72, 73, 73, 73, 74, 75, 75, 76, 77, 77.
At first I thought this was an application for nested arrays. Then I thought perhaps using arrays, array slice, etc, may be a way to go. Each time I end up in the woods. I'm not seeing the logic.
I have used conditional statements for paying out three places but to pay out six places with this method puts me deep in the woods.
Example of payout to three places using conditional statements:
$pointsFirst=0.5*$totalPoints;
$pointsSecond=0.3*$totalPoints;
$pointsThird=0.2*$totalPoints;
if($countFirst>2) {
$ptsA=round($totalPoints/$countFirst,2);
}
elseif($countFirst==2) {
$ptsA=round(($pointsFirst+$pointsSecond)/2,2);
if($countSecond>1) {
$ptsB=round($pointsThird/$countSecond,2);
}
elseif($countSecond==1) {
$ptsB=round($pointsThird,2);
}
}
elseif($countFirst==1) {
$ptsA=round($pointsFirst,2);
if($countSecond>1) {
$ptsB=round(($pointsSecond+$pointsThird)/2,2);
}
elseif($countSecond==1) {
$ptsB=round($pointsSecond,2);
if($countThird>1) {
$ptsC=round($pointsThird/$countThird,2);
}
elseif($countThird==1) {
$ptsC=round($pointsThird,2);
}
}
}
I hope I have been clear in my request. I'll be glad to clarify anything. If anyone has any ideas on how to efficiently automate a payout calculation to six places I will be eternally grateful. Thank-you! Mike
Per request:
$scores=array();
$scores[0]=72;
$scores[1]=72;
$scores[2]=73;
$scores[3]=73;
$scores[4]=73;
$scores[5]=74;
$scores[6]=75;
$scores[7]=75;
$scores[8]=76;
$scores[9]=77;
$scores[10]=77;
$payout=array();
$payout[0]=0.6*$totalPoints;
$payout[1]=0.2*$totalPoints;
$payout[2]=0.15*$totalPoints;
$payout[3]=0.03*$totalPoints;
$payout[4]=0.02*$totalPoints;
$payout[5]=0.01*$totalPoints;
$countScores=array();
$countScores[0]=$countFirst;
$countScores[1]=$countSecond;
$countScores[2]=$countThird;
$countScores[3]=$countFourth;
$countScores[4]=$countFifth;
$countScores[5]=$countSixth;

First, there is a problem with your Payouts. If you add them up you get 1.01 not 1
0.6 (1st) + 0.2 (2nd ) + 0.15 (3rd) + 0.03 (4th) + 0.02 (5th) + 0.01 (6th) = 1.01
Second, it is easier if you make your Payouts and Counts into arrays -
change these -
$pointsFirst=0.6*$totalPoints;
$pointsSecond=0.2*$totalPoints;
$pointsThird=0.15*$totalPoints;
$pointsFourth=0.03*$totalPoints;
$pointsFifth=0.02*$totalPoints;
$pointsSixth=0.01*$totalPoints;
$countFirst=2;
$countSecond=3;
$countThird=1;
$countFourth=2;
$countFifth=1;
$countSixth=2;
to these
$payout=array();
$payout[0]=0.6*$totalPoints;
$payout[1]=0.2*$totalPoints;
$payout[2]=0.15*$totalPoints;
$payout[3]=0.03*$totalPoints;
$payout[4]=0.02*$totalPoints;
$payout[5]=0.01*$totalPoints;
$count=array();
$count[0]=2;
$count[1]=3;
$count[2]=1;
$count[3]=2;
$count[4]=1;
$count[5]=2;
Here is the start of one way to do it. Although I would eventually change this into a function so that I can use it again with different payouts, and number of places (see phpfiddle examples below)
I see this in 4 steps-
Step 1
// Add together the payments if there are ties - ie. 2 tied for first $payout[0]+$payout[1], etc
$payout_groups = array(); // start a payout array
$payout_groups_key = 0; // array key count
$payout_groups_count = 0; // array counter, use to match the $count array values
for($w=0;$w<count($payout);$w++){ //
if(array_key_exists($payout_groups_key,$payout_groups)){
$payout_groups[$payout_groups_key] += $payout[$w]; // if there are ties, add them together
}
else{
$payout_groups[$payout_groups_key] = $payout[$w]; // else set a new payout level
}
$payout_groups_count++; // increase the counter
if($payout_groups_count == $count[$payout_groups_key]){ // if we merged all the ties, set a new array key and restart the counter
$payout_groups_key++;
$payout_groups_count = 0;
}
}
Step 2
// basic counter to get how many placers/winners. This makes it possible to have ties for 6th (last) place
$x = 0;
$y = 0;
while($y < count($payout)){
$y += $count[$x]; // the $count array values until we reach the amount of places/payouts
$x++;
}
Step 3
// Create array for winnings per placing
$winnings = array(); // start an array
$placings_count = 0; //
$placings_counter = 0;
for($z=0;$z<$y;$z++){
$winnings[$z] = $payout_groups[$placings_count]/$count[$placings_count];
$placings_counter++;
if($placings_counter == $count[$placings_count]){
$placings_count++;
$placings_counter = 0;
}
}
Step 4
// Assign winnings to scorecard
$scoreboard = array();
for($t=0;$t<count($winnings);$t++){
$scoreboard[$t]['score'] = $scores[$t];
$scoreboard[$t]['payout'] = $winnings[$t];
}
You can see this using your defined values at - http://phpfiddle.org/main/code/a1g-qu0
Using the same code above, I changed the payout amounts, and increased it to 7th places - http://phpfiddle.org/main/code/uxi-qgt

Related

Selecting one number from the range

I have a trivial question for you but I can not deal with the matches myself.
more precisely - I have a problem with writing a condition that will only be fulfilled when specific parameters occur.
say, I have three thresholds: 2000, 15325, 500000
each of them has an appropriate level
user points are, for example: 16,000
I want it to search for the appropriate level and assign it to the user after reaching the threshold
I unfortunately have a problem with the condition of fulfilling the activity, because the condition
user points> = point threshold
shows me all levels, but
user points <= point threshold
it does not show anything, it does not fulfill the assumption of the script
function chooseLevel($dbh) {
$userPoints = getPoints($dbh, 3);
$levels = getLevels($dbh);
foreach($levels as $required) {
if($userPoints >= $required["points"]) {
$level = $required["level"];
}
}
return $level;
}

Challenge: display php array with aggregated result

I am about to bleed with this challenge....anybody can help me with understanding how to solve please. I am missing the logic. I will spare you the details and give you the input variables to work with, which are dynamically generated. The below records are Male, Female, Male, Female....
$data=array(1,5,10,4,3,4,1,1,1,'2);
$indexOfSubTotalColumns=array(3,6,9,12,15,18,21);
//This means on the 3, 6th ...column of the HTML output, a summary is to happen. That is 1+5 will be 6 on column 3. 10 +4 will be 14 on column 6.
$groupTotalColumns=array(9,21);
//the 9th column will display sum of all columns before it. and 21 from col 10 on words, excluding the subtotals calculated or if you want.
With the following code,I have gotten to calculate the subtotals of each but can't put the dynamic grand total. First, add dumb data to the data array so it becomes equal to the number of HTML columns.
$previousValues=array();
foreach($data as $value){
if(in_array($i,$indexOfSubTotalColumns)){
$rows.="<td>".array_sum($previousValues)."</td>";
$rows.="<td>$value</td>";
$previousValues=array();
array_push($previousValues,$value);
$i++;
//
}
else{//not a subtotal
$rows.="<td>$value</td>";
array_push($previousValues,$value);
}
$i++;
//if $i reaches number of columns i.e. 21, close current row and open new
}
echo $rows;
The above code outputs me this layout:
I am failing to calculate the Totals.

PHP code reaching execution time limit

I need to go through an array containing points in a map and check their distance from one another. I need to count how many nodes are within 200m and 50m of each one. It works fine for smaller amounts of values. However when I tried to run more values through it (around 4000 for scalability testing) an error occurs saying that I have reached the maximum execution time of 300 seconds. It needs to be able to handle at least this much within 300 seconds if possible.
I have read around and found out that there is a way to disable/change this limit, but I would like to know if there is a simpler way of executing the following code so that the time it takes to run it will decrease.
for($i=0;$i<=count($data)-1;$i++)
{
$amount200a=0;
$amount200p=0;
$amount50a=0;
$amount50p=0;
$distance;
for($_i=0;$_i<=count($data)-1;$_i++)
{
$distance=0;
if($data[$i][0]===$data[$_i][0])
{
}
else
{
//echo "Comparing ".$data[$i][0]." and ".$data[$_i][0]." ";
$lat_a = $data[$i][1] * PI()/180;
$lat_b = $data[$_i][1] * PI()/180;
$long_a = $data[$i][2] * PI()/180;
$long_b = $data[$_i][2] * PI()/180;
$distance =
acos(
sin($lat_a ) * sin($lat_b) +
cos($lat_a) * cos($lat_b) * cos($long_b - $long_a)
) * 6371;
$distance*=1000;
if ($distance<=50)
{
$amount50a++;
$amount200a++;
}
else if ($distance<=200)
{
$amount200a++;
}
}
}
$amount200p=100*number_format($amount200a/count($data),2,'.','');
$amount50p=100*number_format($amount50a/count($data),2,'.','');
/*
$dist[$i][0]=$data[$i][0];
$dist[$i][1]=$amount200a;
$dist[$i][2]=$amount200p;
$dist[$i][3]=$amount50a;
$dist[$i][4]=$amount50p;
//*/
$dist.=$data[$i][0]."&&".$amount200a."&&".$amount200p."&&".$amount50a."&&".$amount50p."%%";
}
Index 0 contains the unique ID of each node, 1 contains the latitude of each node and
index 2 contains the longitude of each node.
The error occurs at the second for loop inside the first loop. This loop is the one comparing the selected map node to other nodes. I am also using the Haversine Formula.
first of all, you are performing in big O notation: O(data^2), which is gonna be slow as hell , and really, either there are 2 possible solutions. Find a proven algorithm that solves the same problem in a better time. Or if you cant, start moving stuff out of the innner for loop, and mathmatically prove if you can convert the inner for loop to mostly simple calculations, which is often something you can do.
after some rewriting, I see some possiblities:
If $data is not a SPLFixedArray (which has a FAR Better access time, ) then make it. since you are accessing that data so many times (4000^2)*2.
secound, write cleaner code. although the optizmier will do its best, if you dont try either to minize the code (which only makes it more readable), then it might not be able to do it as well as possible.
and move intermediate results out of the loops, also something like the size of the array.
Currently you're checking all points against all other points, where in fact you only need to check the current point against all remaining points. The distance from A to B is the same as the distance from B to A, so why calculate it twice?
I would probably make an adjacent array that counts how many nodes are within range of each other, and increment pairs of entries in that array after I've calculated that two nodes are within range of each other.
You should probably come up with a very fast approximation of the distance that can be used to disregard as many nodes as possible before calculating the real distance (which is never going to be super fast).
Generally speaking, beyond algorithmic optimisations, the basic rules of optimisation are:
Don't any processing that you don't have to do: Like not multiplying $distance by 1000. Just change the values you're testing against from 20 and 50 to 0.02 and 0.05, respectively.
Don't call any function more often than you have to: You only need to call count($data) once before any processing starts.
Don't calculate constant values more than once: PI()/180, for example.
Move all possible processing outside of loops. I.e. precalculate as much as possible.
Another minor point which will make your code a little easier to read:
for( $i = 0; $i <= count( $data ) - 1; $i++ ) is the same as:
for( $i = 0; $i < count( $data ); $i++ )
Try this:
$max = count($data);
$CONST_PI = PI() / 180;
for($i=0;$i<$max;$i++)
{
$amount200a=0;
$amount50a=0;
$long_a = $data[$i][2] * $CONST_PI;
$lat_a = $data[$i][1] * $CONST_PI;
for($_i=0;$_i<=$max;$_i++)
//or use for($_i=($i+1);$_i<=$max;$_i++) if you did not need to calculate already calculated in other direction
{
$distance=0;
if($data[$i][0]===$data[$_i][0]) continue;
$lat_b = $data[$_i][1] * $CONST_PI;
$long_b = $data[$_i][2] * $CONST_PI;
$distance =
acos(
sin($lat_a ) * sin($lat_b) +
cos($lat_a) * cos($lat_b) * cos($long_b - $long_a)
) * 6371;
if ($distance<=0.2)
{
$amount200a++;
if ($distance<=0.05)
{
$amount50a++;
}
}
} // for %_i
$amount200p=100*number_format($amount200a/$max,2,'.','');
$amount50p=100*number_format($amount50a/$max,2,'.','');
$dist.=$data[$i][0]."&&".$amount200a."&&".$amount200p."&&".$amount50a."&&".$amount50p."%%";
} // for $i
It will be better to read I think and if you change the commented out line of the for $_i it will be faster at all :)

Add all instance of a variable together in a PHP foreach loop?

I am trying to do something that's quite complex (for me anyway) and I've got a point which has stopped me in my tracks.
In this project, you enter the weight of a box, and the percentage of materials held in that box. So for example, a box may have 10% of wood, 40% of plastic, and 50% of paper. The weights of the materials are held in the database as they dont change. So the value of 1% of wood is held in the database, as 1% of plastic and paper.
The controller should then take the percentage of material given, and then multiply it by the 1% value held in the database.
So far, this is my code:
foreach($wastestream as $ws) {
$genweight = $ws['Materialtype']['1pc'] * $ws['Valuegiven']['percent'];
}
And this gives the following values:
10% Wood = 97.5
40% Plastic = 100.4
50% Paper = 40.3
The problem I now have is that I have to add all of these variables together. So essentially what I want to do after it pumps out the values is to add them all up together, so:
97.5 + 100.4 + 40.3 = 238.2
What should I do?
Cheers!
I think I have misunderstood something, since it seems not complicated at all...
$total=0;
foreach($wastestream as $ws) {
$total += $ws['Materialtype']['1pc'] * $ws['Valuegiven']['percent'];
}
$total will hold the total value
Could you add the values as they are created?
$totalweight = 0;
foreach($wastestream as $ws) {
$genweight = $ws['Materialtype']['1pc'] * $ws['Valuegiven']['percent'];
$totalweight += $genweight;
}

Calculate circumference values

I have a rectangular map, stored as multidimensional array (ie $map[row][col]) and I have to track down which squares are seen by a player, placed anywhere on this map.
Player visibility is circular with unknown radius (but given at run-time) and I only need integer solutions.
I know that circumference formula is
x^2 + y^2 <= r^2
but how can I store everything?
I need these values since then I can "reveal" map squares.
The best would be a multidimesional array (ie __$sol[x][y]__).
This is a piece of code that I'm using. It's not corrected since it assumes that vision is a square and not a circle.
Calculating the square
$this->vision_offsets_2 = array();
//visibility given as r^2
$mx = (int)(sqrt($this->viewradius2));
$mxArr = range($mx * -1, $mx + 1);
foreach ($mxArr as $d_row)
{
foreach ($mxArr as $d_col)
{
$this->vision_offsets_2[] = array($d_row, $d_col);
}
}
This is how I apply that
foreach($player as $bot)
{
foreach($visibility as $offset)
{
$vision_row = $offset[0] + $bot[0];
$vision_col = $offset[1] + $bot[1];
if(isset($map[$vision_row][$vision_col]))
{
if( $map[$vision_row][$vision_col] == UNSEEN) {
$map[$vision_row][$vision_col] = LAND; }
}
}
}
Here you can find the bot view: as you can see is a non perfect circle.
How can I track it? By the way, in this example radius^2 is 55, the orange circle is the player, brown squares are visible ones.
Structure
You're already referencing terrain in a grid. Store terrain objects in those grid values. Apply attributes to those objects. Check with something like
$map[$x][$y]->isVisible($player);
You'll need some methods in there for setting vision and tests for checking the user that is passed against a list of users who can see it. While you're at it, setup other related methods in those objects (I see references to land... isLand() and isWater() perhaps?).
You can even have vision cascade within objects such that you only need to move the position of a user and the object takes care of triggering off all the code to set nearby plots of land to visible.
Math
We are given circumference.
double diameter = circumference / 3.14159
double radius = diameter / 2 //Normally done in one step / variable
Now we must know the distance between two points to compare it. Let's use map[4][7] and map[3][9].
int x0 = 4;
int y0 = 7;
int x1 = 3;
int y1 = 9;
double distance = Math.sqrt(
Math.pow(x0 - x1, 2) +
Math.pow(y0 - y1, 2)
);
System.out.println(distance); //2.23606797749979
Test distance > radius.
Testing each square
Put the above in a method: visibleFrom(Square target)
radius should be a globally accessible static variable when comparing.
Your Square object should be able to hand over its coordinates.
target.getX()
target.getY()
Some optimizations can be had
Only checking things for circular distance when they're in the square.
Not checking anything for circular distance when purely along the x or y axis.
Figuring out the largest square that fits inside the circle and not checking boxes in that range for circular distance.
Remember that premature optimization and over optimization are pitfalls.
A function like this would tell you if a map square is visible (using the distance of the centers of the squares as a metric; if you want to define visibility in another manner, which you probably would, things get much more complicated):
function is_visible($mapX, $mapX, $playerX, $playerY, $r) {
return sqrt(pow($mapX - $playerX, 2) + pow($mapY - $playerY, 2)) <= $r;
}
You probably don't really need to store these values since you can easily calculate them on demand.
I think that Bresenham's circle drawing algorithm is what you're looking for.
I don't know exactly what you want, but here's some things that should help you along. As a warning these are untested, but the logic is sound.
//You mentioned circumference, this will find out the circumference but I don't
//think you actually need it.
$circumference_length = 2 * $visibility_range * 3.1415;
//Plug in the player and target coordinates and how far you can see, this will
//tell you if the player can see it. This can be optimized using your object
//and player Objects.
function canSee($player_x, $player_y, $vision_length, $target_x, $target_y){
$difference_x = $target_x - $player_x;
$difference_y = $target_y - $player_y;
$distance = sqrt((pow($difference_x,2) + pow($difference_y, 2));
if($vision < $distance){
return false;
} else {
return true;
}
}
Edit: In response to your clarification, you can use the above function to figure out if you should show the terrain objects or not.
foreach($player as $bot)
{
foreach($terrain_thing as $terrain)
{
//ASSUMING THAT [0] IS ALWAYS X AND [1] IS ALWAYS y, set a third variable
//to indicate visibility
$terrain["is_visible"] = canSee($bot[0], $bot[1], $visibility_range, $terrain[0], $terrain[1])
}
}

Categories