I searched some code in php that creates the following output:
*
* *
* * *
* * * *
* * * * *
I noticed that there are lots of different approach they used to achieve this output. The other codes are long and the other is short. .
But there is one code that caught my attention. The code he used is too short and it's interesting for me, but I do not know how he do it because he did not left any explanation about it. The code is:
<pre><?php
$n = $i = 5;
while ($i--)
echo str_repeat(' ', $i).str_repeat('* ', $n - $i)."\n";
?></pre>
I do not know how the code works. I think the $n =$i = 5 is for the loop? Am I right? Is there anyone here can explain to me how the code works with simulation.
I found this code at Pyramid of asterisks in php
Any answers are welcome and much appreciated. Thank you guys!
Your code:
DEMO
<?php
$n = $i = 5;
while ($i--)
echo str_repeat(' ', $i).str_repeat('* ', $n - $i)."\n";
Loop 1:
$i = 5 (true) and decrement $i by 1, now $i = 4
str_repeat(' ', $i), means print empty space 4 times
then print '* ' $n - $i (5 - 4 = 1) time
Present output:
*
Loop 2:
$i = 4 (true) and decrement $i by 1, now $i = 3
str_repeat(' ', $i), means print empty space 3 times
then print '* ' $n - $i (5 - 3 = 2) times
Present output:
*
* *
Loop 3:
$i = 3 (true) and decrement $i by 1, now $i = 2
str_repeat(' ', $i), means print empty space 2 times
then print '* ' $n - $i (5 - 2 = 3) times
Present output:
*
* *
* * *
Loop 4:
$i = 2 (true) and decrement $i by 1, now $i = 1
str_repeat(' ', $i), means print empty space 1 times
then print '* ' $n - $i (5 - 1 = 4) times
Present output:
*
* *
* * *
* * * *
Loop 5:
$i = 1 (true) and decrement $i by 0, now $i = 0
str_repeat(' ', $i), means print empty space 0 times
then print '* ' $n - $i (5 - 0 = 5) times
Present output:
*
* *
* * *
* * * *
* * * * *
Loop 6:
$i = 0 (false) and loop breaks
Related
I want to ask your help. I don't remember the Math, but I need to count by formula
I am going to make the loops like that.
$incomeDepositDay = $totalSum * $percent / ($typePerm *100);
for ($i = 1; $i <= $term; $i++) {
$incomeDepositTax = $percent/100 * $typePerm * ($totalSum * $i + ($incomeDepositDay * (1 - 13/100)));
}
But I see, that my code isn't correct. But I don't understand, where?
You don't need a loop, as the formula is a straightforward calculation.
You have a couple of errors in your sum. Try:
$incomeDepositDay = $totalSum * $percent / ($typePerm *100);
$incomeDepositTax = $percent/(100 * $typePerm) * ($totalSum * $i + ($i - 1) * $incomeDepositDay * (1 - 13/100));
I want to know how to get variables and it's values from a variable?
Here is the sample code;
$x = 10;
$y = 20;
$z = 30;
$sum = $x * 5; // It has only one variable
echo $sum; // Page # 1
$sum = ($x * 5) + ($y * 5); // It has two variables
echo $sum; // Page # 2
$sum = ($x * 5) + ($y * 5) + ($z * 5); // It has three variables
echo $sum; // Page # 3
Now I want to get the variables inside of the variable $sum and their values to output a breakup of the calculation.
Something like this;
----- Page # 1 -----
X: 10 * 5 = 50
----- Page # 2 -----
X: 10 * 5 = 50
Y: 20 * 5 = 100
Sum: X + Y = 150
----- Page # 3 -----
X: 10 * 5 = 50
Y: 20 * 5 = 100
Z: 30 * 5 = 150
Sum: X + Y + Z = 300
Is there any PHP function or any other solution to achieve this without making any changes to the variable $sum?
I know I can do it in other ways, but the problem is; I can't change the code $sum as it's required to change it on thousands of pages. It would be highly appreciated if anyone could help me with this.
Thanks in advance :-)
There's no way to use $sum to retrieve the calculations or input values which were used to give it its current value. PHP does not work like that. As far as I am aware, no programming language currently provides that.
A variable simply contains its current value...it does not contain any other variables or calculations, or keep a history of how it got like that.
The only way to get the output you've specified is to directly echo the data explicitly from the original data, for example:
$x = 10;
$y = 20;
$z = 30;
// Page # 1
$sum = $x * 5;
echo "X: ".$x."* 5 = ".$sum;
// Page # 2
$sum = ($x * 5) + ($y * 5);
echo "X: ".$x."* 5 = ".($x * 5);
echo "Y: ".$y."* 5 = ".($y * 5);
echo "Sum: X + Y = ".$sum;
// Page # 3
$sum = ($x * 5) + ($y * 5) + ($z * 5);
echo "X: ".$x."* 5 = ".($x * 5);
echo "Y: ".$y."* 5 = ".($y * 5);
echo "Z: ".$z."* 5 = ".($z * 5);
echo "Sum: X + Y + Z = ".$sum;
I get similar MACD and Signal values, using the following class in Binance:
I got this code from:
https://github.com/hurdad/doo-forex/blob/master/protected/class/Technical%20Indicators/MACD.php
How can I modify the class to arrive at the exact value?
You can use the php-trader lib, note that it works as CLI only.
But this is fairly simple math:
MACD = EMA26 - EMA12
/*
* Exponential moving average (EMA)
*
* The start of the EPA is seeded with the first data point.
* Then each day after that:
* EMAtoday = α⋅xtoday + (1-α)EMAyesterday
*
* where
* α: coefficient that represents the degree of weighting decrease, a constant smoothing factor between 0 and 1.
*
* #param array $numbers
* #param int $n Length of the EPA
* #return array of exponential moving averages
*/
function exponentialMovingAverage( $numbers, $n)
{
$m = count($numbers);
$α = 2 / ($n + 1);
$EMA = [];
// Start off by seeding with the first data point
$EMA[] = $numbers[0];
// Each day after: EMAtoday = α⋅xtoday + (1-α)EMAyesterday
for ($i = 1; $i < $m; $i++) {
$EMA[] = ($α * $numbers[$i]) + ((1 - $α) * $EMA[$i - 1]);
}
return $EMA;}
I have a string $string = "CONSTANTMAINONE";
I want to print distinct characters and print stars in next rows that number of characters are repeated.
C O N S T A M I E
* * * * * * * * *
* * * *
*
*
What I had tried
Loop the characters
Add the character as a key and count as a value
Then printed, but that output is not coming vertically and the question should be do not use table or pre tags
$string = "CONSTANTMAINONE";
$repeated_char = [];
for($i=0; $i<=strlen($string);$i++){
if(!array_key_exists($string[$i], $repeated_char)){
$repeated_char[$string[$i]] = 1;
}else{
$repeated_char[$string[$i]] += 1;
}
}
So far, output is
C => 1
o => 1
N => 4
s => 1
T => 1
..etc..,
Then, printing the characters
foreach($repeated_char as $key=>$val){
echo $key. " ";
for($j=0; $j<$val; $j++){
echo "*";
}
echo "<br/>";
}
So, the final output from my code
C *
o **
N ****
S *
T **
A **
M *
I *
E *
This answer is form the first revision:
https://stackoverflow.com/revisions/28536477/1
This should work for you:
(Here i use str_split() to create a array out of the string. After this i count all array values with array_count_values(). Then it's just a simple printing thing. First i print all unique values and after this i print the starts and decrement the value in a tmp array)
<?php
$string = "CONSTANTMAINONE";
$array = str_split($string);
$count = array_count_values($array);
$tmp = $count;
echo "<table border='1'><tr>";
foreach($count as $k => $v)
echo "<td>" . $k . "</td>";
echo "</tr>";
for($i = 0; $i < max($count); $i++) {
echo "<tr>";
foreach($tmp as $k => $v) {
if($v >= 1)
echo "<td>*</td>";
else
echo "<td></td>";
$tmp[$k]--;
}
echo "</tr>";
}
echo "</table>";
?>
Output:
C O N S T A M I E
* * * * * * * * *
* * * * * * * * *
* * * *
*
I am trying to take an amount and convert it to a units type format...
For example:
( note: don't worry about the dollar sign )
Total is $400
I need to display it as 4 * 100
Another Example
Total is $450
I need to display it as 4 * 100 | 50 * 1
So another words there are only 100 and 1 units.
I was thinking for 3 hours already and nothing seems to come to mind...Perhaps someone out there has done something similar and already know the answer?
Hoping I am not doing your homework. Try this:
$num = 450;
$ones = $num % 100;
$hundreds = floor($num / 100);
echo "$hundreds * 100 | $ones * 1";
Here's a simple implementation
$amount = 450;
$hundreds = floor($amount / 100);
$ones = $amount % 100;
$string = array();
if( $hundreds )
$string[] = "$hundreds * 100";
if( $ones )
$string[] = "$ones * 1";
echo implode(' | ', $string);
Check out the modulus (%) operator
Here's a simple solution which will only show the units present, you can get rid of the array/join stuff if you always need to show both units:
$total = 400;
$out = array();
$hundreds = floor($total / 100);
if ($hundreds) {
$out[] = $hundreds . ' * 100';
}
$ones = $total % 100;
if ($ones) {
$out[] = $ones . ' * 1';
}
echo join(' | ', $out);
Use the modulus operator to break down the number (this kind of thing is good to learn how to do because you'll need it for many other units conversion tasks like seconds -> minutes & seconds conversion):
$value=450;
$ones = $value % 100;
$hundreds = floor($value / 100);
echo "$hundreds * 100 | $ones * 1\n";