-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberRhombus.php
46 lines (45 loc) · 1.79 KB
/
NumberRhombus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* This function creates a rhombus of numbers.
* @param int $n: It is the size to the midpoint of the rhombus
*/
function numberRhombus($n)
{
//upper half
for ($i = 1; $i <= $n; $i++) { // Create each row, from least to greatest
for ($j = 0; $j < $n - $i; $j++) { // Places the spaces, always is $n - $i spaces
echo " ";
}
for ($j = 1; $j <= $i; $j++) { // Places the numbers until to the current $i ($i is the row)
//echo $j; // works well until $n = 9, if $n > 9 the shape of the rhombus deforms
echo substr($j, 0, 1); // leave a single digit
}
for ($j = $i - 1; $j >= 1; $j--) { // Places missing numbers
//echo $j; // works well until $n = 9, if $n > 9 the shape of the rhombus deforms
echo substr($j, 0, 1); // leave a single digit
}
echo "\n";
}
//lower half
for ($i = $n; $i > 1; $i--) { // Create each row, from highest to lowest
for ($j = $n - $i; $j >= 0; $j--) { // Places the spaces, invert first for
echo " ";
}
for ($j = 1; $j < $i; $j++) { // Places the numbers, only $j < $i, avoid repeat the midpoint
//echo $j; // works well until $n = 9, if $n > 9 the shape of the rhombus deforms
echo substr($j, 0, 1); // leave a single digit
}
for ($j = $i - 2; $j >= 1; $j--) { // Places missing numbers
//echo $j; // works well until $n = 9, if $n > 9 the shape of the rhombus deforms
echo substr($j, 0, 1); // leave a single digit
}
echo "\n";
}
}
$n = 5;
if (count($argv) > 1) {
$n = intval($argv[1]);
} else {
echo "\e[0;33mYou have not passed the midpoint of the rhombus, by default one of 5 will be created.\e[0m\n";
}
echo numberRhombus($n);