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

Commit f88d0c9

Browse files
author
Holger Lösken
committed
Add task4 in php
1 parent f3c2ad8 commit f88d0c9

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,32 @@ Write a `calculate` function that accepts an input and returns the result of the
3636
* Return 0 (integer) when nothing is entered
3737
* Return the numeric value when no operand is given, like `1 2 3.5` return `3.5`
3838

39+
### Task 4
40+
41+
Given you have an array of numbers, you move inside the array by the value of the current element.
42+
Write a function `jump_out_of_array` that outputs
43+
44+
* the amount of jumps until you jump out of the array
45+
* `-1` when you reach the end of the array but do not jump out
46+
47+
** Requirements:**
48+
* Array size is indefinite
49+
* Array elements are integers, positive and negative
50+
51+
**Example:**
52+
53+
Given an array of `A[2, 3, -1, 1, 6, 4]`.
54+
55+
![](./docs/t4/task4.png)
56+
57+
* Jump 1: A[0] + 2 = A[2]
58+
* Jump 2: A[2] + (-1) = A[1]
59+
* Jump 3: A[1] + 3 = A[4]
60+
* Jump 4: A[4] + 6 = out of range
61+
62+
So the result is `4`, you need 4 jumps to jump out of the array.
63+
3964
## Contribute
4065

4166
Feel free to contribute. Use the issue list to propose new tasks or open PRs. Just provide proper tests
42-
and description and requirements for the tasks.
67+
and description and requirements for the tasks.

docs/t4/task4.png

7.18 KB
Loading

src/php/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codedge/coding-challenges",
3-
"author": "Holger Lösken <[email protected]>",
3+
"description": "",
44
"require": {
55
"php": "^8.0",
66
"phpunit/phpunit": "^9.5"
@@ -9,7 +9,8 @@
99
"files": [
1010
"t1/s1.php",
1111
"t2/s1.php",
12-
"t3/s1.php"
12+
"t3/s1.php",
13+
"t4/s1.php"
1314
],
1415
"psr-4": {
1516
"Tests\\": "tests/"

src/php/t4/s1.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
function jump_out_of_array(array $a): int {
6+
$sum = 0;
7+
$jumps = 1;
8+
9+
// If first entry in array is negative, then the first jump is out of the array
10+
if($a[0] < 0) {
11+
return $jumps;
12+
}
13+
14+
for ($i=0; $i<= count($a); $i++) {
15+
$p = $a[$sum];
16+
$sum = $sum + $p;
17+
18+
// Handle jumps out of array at the beginning of the array
19+
if($sum < 0) {
20+
return $jumps;
21+
}
22+
23+
$jumps++;
24+
25+
// Handle jumps out of array at the end of the array
26+
if (($sum + $p) > count($a)
27+
) {
28+
return $jumps;
29+
}
30+
31+
if(($i + 1) === count($a)) {
32+
return -1;
33+
}
34+
}
35+
}

src/php/tests/T4Test.php

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

0 commit comments

Comments
 (0)