Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 37dfd22

Browse files
author
Holger Lösken
committed
Add task8 in php
1 parent 74d51fc commit 37dfd22

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ and return the last `6` non-zero numbers.
106106

107107
Given the follwing function you should suggest what could be improved. There are no other documents explaining why this function has been written or what the purpose is/should be.
108108

109+
**Example in python**
109110
```python
110111
def multiply(x, y):
111112
if y > 0:
@@ -122,6 +123,22 @@ def multiply(x, y):
122123
- What can happen when using this with big numbers, f. ex. > 1.000.000?
123124
- Type hints
124125

126+
### Task 8
127+
128+
Do an in-place mirroring of a one dimensional array. In-place switching is key here as the input array can be very big
129+
and no additional memory should be occupied -
130+
131+
**Requirements:**
132+
133+
- In-place mirroring
134+
- Handle array with even and odd amount of items
135+
- Do not use the [`array_reverse`](https://www.php.net/manual/de/function.array-reverse.php) function in PHP
136+
137+
**Example:**
138+
139+
- Even amount: `[8,5,1,4]` -> `[4,1,5,8]`
140+
- Odd amount: `[6,2,7,9,3]` -> `[3,9,7,2,6]`
141+
125142
## Contribute
126143

127144
Feel free to contribute. Use the issue list to propose new tasks or open PRs. Just provide proper tests

src/php/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"t3/s1.php",
1313
"t4/s1.php",
1414
"t5/s1.php",
15-
"t6/s1.php"
15+
"t6/s1.php",
16+
"t8/s1.php"
1617
],
1718
"psr-4": {
1819
"Tests\\": "tests/"

src/php/t8/s1.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
function mirror(array $numbers): array {
6+
$max = max(array_keys($numbers));
7+
8+
for($i=0; $i<=(floor(count($numbers)/2))-1; $i++) {
9+
$tmp = $numbers[$max-$i];
10+
$numbers[$max-$i] = $numbers[$i];
11+
$numbers[$i] = $tmp;
12+
}
13+
14+
return $numbers;
15+
}

src/php/tests/T8Test.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Exception;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class T8Test extends TestCase
11+
{
12+
public function testEvenInputAmount(): void
13+
{
14+
$input = [8,5,1,4];
15+
$this->assertSame(array_reverse($input), mirror($input));
16+
}
17+
18+
public function testOddInputAmount(): void
19+
{
20+
$input = [6,2,7,9,3];
21+
$this->assertSame(array_reverse($input), mirror($input));
22+
}
23+
}

0 commit comments

Comments
 (0)