This question already has answers here:
PHP FizzBuzz Logic
(3 answers)
Another FizzBuzz solution [closed]
(3 answers)
PHP Fizzbuzz Challenge [closed]
(3 answers)
PHP loop increment quirk - (FizzBuzz in one line)
(4 answers)
Closed 2 months ago.
I came across this php interview question while researching pre-interview test questions.
Given an array of integers, compute a total score based on the following rules:
Add 1 point for every even number in the array
Add 3 points for every odd number in the array
Add 5 points for every time you encounter an 8 in the array
Examples:
Input: my_numbers = [1, 2, 3, 4, 5]
Output: 11
Input: my_numbers=[15, 25, 35]
Output: 9
Input: my_numbers=[8, 8]
Output: 10
How to approach this particular task?
my attempt
<?php function find_total( $my_numbers ) {
//Insert your code here \
$total = 0;
foreach($my_numbers as $val) {
if ($val % 2 == 0) {
echo (++$val);
} else
echo ($val += 3);
}
if ($val == 8) {
echo($val += 5);}
}
?>
I am not sure your sample answers are correct (8 is an even number and should attract 1 for that as well as the 5), but to give the answer you asked for use this (PHP):
(EDITED in response to mickmackusa's and Robin Bastiaan's comments)
$total = 0;
foreach($myarray as $value) {
if ($value === 8) {
$total += 5;
} elseif (!($value % 2)) {
++$total;
} else
$total += 3;
}
Related
This question already has answers here:
Find number which is greater than or equal to N in an array
(11 answers)
Closed 8 years ago.
I want to round up any given number to the nearest set of specific values only. The values are 5,10,20,25,50,100
So if the given number is 2 it will be rounding up to 5. If it's 71.6 it will be rounding down to 50, if it's 250 it will be rounding down to 100
Anybody can give me a clue how to do that?
Thanks a lot!
use a loop:
$numbers = array(5, 10, 20, 25, 50, 100);
$result = $input;
foreach ($numbers as $n) {
if ($input < $n) {
$result = $n;
break;
}
}
I found the answer here : Find number which is greater than or equal to N in an array. Thanks to #kAlmAcetA
function closest($array, $number) {
sort($array);
foreach ($array as $a) {
if ($a >= $number) return $a;
}
return end($array) // or return NULL;
}
This question already has answers here:
How to make 5 random numbers with sum of 100 [duplicate]
(8 answers)
Closed 8 years ago.
Can a random number being generated and split/divided into let say 3 individual numbers randomly in PHP? (so they add up to the random number)
Basically I have a random number (range from 0 to 15)
For example:
There are 14 available now
- 2 in Gotham City
- 8 in Lakeview Hills
- 4 in Eskimoville
(just a dummy to give you the idea)
IsnĀ“t there a simple approach to accomplish this?
Have a nice weekend!
basic implementation would need error checking + not 100% random
<?php
//first initial number
// range must be 3-15 to get 3 random additions
$i = rand(3,15);
echo $i;
// generate what 3 number adds to $i
$one = rand(1,$i-2);
$i-=$one;
$two = rand(1,$i-1);
$i -= $two;
$three = rand(1,$i);
$i -= $three;
if($i > 0){
// add remained to third number
$three += $i;
}
echo $one;
echo $two;
echo $three;
?>
This question already has answers here:
Sum values in foreach loop php [closed]
(5 answers)
Closed 8 years ago.
I have no idea how can I display a sum of numbers from array. I work inside wordpress loop. Here is my code:
<?php
$a = array($apples);
foreach ($a as $b)
{
$totalapples = $b . " ";
echo "$totalapples";
}
?>
This code display (I have numbers like 4,5 etc.):
4,5 13,5 10 13,5 14 12,3 7,5 5,4 9,5 5,4 4,5 4 3,3 5,7
To add numbers, use +=.
$totalapples = 0;
foreach ($apples as $apple) {
$totalapples += $apple;
}
This question already has answers here:
Compare floats in php
(17 answers)
Closed 9 years ago.
hello I have a problem wtich two variables in php. i have this code:
var_dump($total);echo '<br/>';
var_dump($reserva->getAdelanto());
if ($total == $reserva->getAdelanto()){
$total = 0;
echo "hello";
}
else
$total = $total - $reserva->getAdelanto();
print :
float(3940.2)
float(3940.2)
but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.
May be try with abs like
if ((abs($a)-abs($b)) <= 0.00001) {
echo "same";
}
Or
if (abs($a - $b) <= 0.00001) {
echo "same";
}
Or you also try like
var_dump( bccomp($a, $b) == 0 )
returns true if they are same
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP shift right
What is the ability to get the same result of javascript's triple great than ( >>> ) in php ?
function urshift($n, $s) {
return ($n >= 0) ? ($n >> $s) :
(($n & 0x7fffffff) >> $s) |
(0x40000000 >> ($s - 1));
}
Untested, credit goes here.