-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-dynamic-pyramid.php
47 lines (40 loc) · 1.18 KB
/
php-dynamic-pyramid.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
47
<?php
/**
* Two-side pyramid
*/
echo "Two-side pyramid: \n";
echo "\n";
$start = microtime(TRUE);
$levels = 10;
$range = range(3, $levels * 2, 2);
$range = array_merge(array_reverse($range, TRUE), [1], $range);
array_walk_recursive($range, function($key) use($levels){ echo str_pad(str_repeat('*', $key), $levels * 2, ' ', STR_PAD_BOTH)."\n";});
echo "\n";
echo microtime(TRUE) - $start."\n";
echo "\n";
/**
* One-side-up pyramid
*/
echo "One-side-up pyramid: \n";
echo "\n";
$start = microtime(TRUE);
$levels = 20;
$range = range(1, $levels * 1, 2);
//$range = array_merge($range, [1], array_reverse($range, TRUE));
array_walk_recursive($range, function($key) use($levels){ echo str_pad(str_repeat('*', $key), $levels * 1, ' ', STR_PAD_BOTH)."\n";});
echo "\n";
echo microtime(TRUE) - $start."\n";
echo "\n";
/**
* One-side-bottom pyramid
*/
echo "One-side-bottom pyramid: \n";
echo "\n";
$start = microtime(TRUE);
$levels = 20;
$range = range(1, $levels * 1, 2);
$range = array_reverse($range, TRUE);
array_walk_recursive($range, function($key) use($levels){ echo str_pad(str_repeat('*', $key), $levels * 1, ' ', STR_PAD_BOTH)."\n";});
echo "\n";
echo microtime(TRUE) - $start."\n";
echo "\n";