-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask1.php
59 lines (49 loc) · 1.19 KB
/
task1.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
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* Задача 1: лесенка
*
* Нужно вывести лесенкой числа от 1 до 100.
* 1
* 2 3
* 4 5 6
* ...
*/
class StairsPrinter
{
private const ELEMENT_DELIMETER = ' ';
private const STRING_DELIMETER = '<br>';
private $start;
private $finish;
/**
* @param int $start
* @param int $finish
*/
public function __construct(int $start, int $finish)
{
$this->start = $start;
$this->finish = $finish;
}
/**
* Возвращает «лесенку» от $this->start до $this->finish
* @return string
*/
public function handle(): string
{
$res = [];
$current = $this->start;
$count = 1;
while ($current <= $this->finish) {
$newLevel = [];
for ($i = 0; $i < $count; $i++) {
if ($current > $this->finish) {
break;
}
$newLevel[] = $current++;
}
$res[] = implode(self::ELEMENT_DELIMETER, $newLevel);
$count++;
}
return implode(self::STRING_DELIMETER, $res);
}
}
echo (new StairsPrinter(1, 100))->handle();