Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
Add rotate function
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k committed Apr 7, 2019
1 parent 6c5ad71 commit 4e4c758
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion data/database.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"all":["array"],"any":["array"],"average":["math"],"deepFlatten":["array"],"drop":["array"],"endsWith":["string"],"factorial":["math"],"fibonacci":["math"],"findLast":["array"],"findLastIndex":["array"],"firstStringBetween":["string"],"flatten":["array"],"gcd":["math"],"groupBy":["array"],"hasDuplicates":["array"],"head":["array"],"isAnagram":["string"],"isEven":["math"],"isLowerCase":["string"],"isPrime":["math"],"isUpperCase":["string"],"last":["array"],"lcm":["math"],"median":["math"],"palindrome":["string"],"pluck":["array"],"pull":["array"],"reject":["array"],"remove":["array"],"startsWith":["string"],"tail":["array"],"take":["array"],"without":["array"],"compose":["function"],"maxN":["math"],"minN":["math"],"countVowels":["string"],"decapitalize":["string"],"approximatelyEqual":["math"],"clampNumber":["math"],"orderBy":["array"],"memoize":["function"],"curry":["function"],"once":["function"],"isContains":["string"],"variadicFunction":["function"],"bubbleSort":["array"]}
{"all":["array"],"any":["array"],"average":["math"],"deepFlatten":["array"],"drop":["array"],"endsWith":["string"],"factorial":["math"],"fibonacci":["math"],"findLast":["array"],"findLastIndex":["array"],"firstStringBetween":["string"],"flatten":["array"],"gcd":["math"],"groupBy":["array"],"hasDuplicates":["array"],"head":["array"],"isAnagram":["string"],"isEven":["math"],"isLowerCase":["string"],"isPrime":["math"],"isUpperCase":["string"],"last":["array"],"lcm":["math"],"median":["math"],"palindrome":["string"],"pluck":["array"],"pull":["array"],"reject":["array"],"remove":["array"],"startsWith":["string"],"tail":["array"],"take":["array"],"without":["array"],"compose":["function"],"maxN":["math"],"minN":["math"],"countVowels":["string"],"decapitalize":["string"],"approximatelyEqual":["math"],"clampNumber":["math"],"orderBy":["array"],"memoize":["function"],"curry":["function"],"once":["function"],"isContains":["string"],"variadicFunction":["function"],"bubbleSort":["array"],"rotate":["array"]}
26 changes: 26 additions & 0 deletions snippets/rotate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
### rotate

Rotates the array (in left direction) by the number of shifts.

Given the $shift index, it merges the array values after $shift with the values before $shift.

```php
function rotate($arr, $shift=1)
{
$resultStarting = array_slice($arr, $shift); //extracting elements starting from $shift position till end
$resultEnding = array_slice($arr, 0, $shift); //extracting elements from start till $shift position
$result = array_merge($resultStarting, $resultEnding);

return $result;
}
```

<details>
<summary>Examples</summary>

```php
rotate([1, 3, 5, 2, 4]); // [3, 5, 2, 4, 1]
rotate([1, 3, 5, 2, 4], 2); // [5, 2, 4, 1, 3]
```

</details>
14 changes: 12 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,11 @@ function once($function)
};
}

function bubbleSort($array) {
function bubbleSort($array)
{
$array = array_unique($array);
$arrayLength = count($array);
for ($i = 0; $i < $arrayLength - 1; $i++) {
for ($i = 0; $i < $arrayLength - 1; $i++) {
$swapped = false;
for ($j = 0; $j < $arrayLength - 1 - $i; $j++) {
if ($array[$j] > $array[$j + 1]) {
Expand All @@ -392,3 +393,12 @@ function bubbleSort($array) {
}
return $array;
}

function rotate($array, $shift = 1)
{
for ($i = 0; $i < $shift; $i++) {
array_push($array, array_shift($array));
}

return $array;
}
20 changes: 20 additions & 0 deletions tests/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,25 @@ public function testBubblesSort()
bubbleSort([44, 11, 7, 20, 12, 90, 35, 5])
);
}

public function rotateDataProvider()
{
return [
[
[1, 3, 5, 2, 4], [3, 5, 2, 4, 1], 1
],
[
[1, 3, 5, 2, 4], [5, 2, 4, 1, 3], 2
],
];
}

/**
* @dataProvider rotateDataProvider
*/
public function testRotate($array, $expected, $shift)
{
$this->assertEquals(rotate($array, $shift), $expected);
}
}

0 comments on commit 4e4c758

Please sign in to comment.