Skip to content

Commit 8252c9b

Browse files
committed
String and Integer Permutations
1 parent 576a6bd commit 8252c9b

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

src/Algorithm/Various/Permutation.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace doganoo\PHPAlgorithms\Algorithm\Various;
27+
28+
use doganoo\PHPUtil\Util\NumberUtil;
29+
use doganoo\PHPUtil\Util\StringUtil;
30+
31+
/**
32+
* Class Permutation
33+
*
34+
* @package doganoo\PHPAlgorithms\Algorithm\Various
35+
*/
36+
class Permutation {
37+
/**
38+
* returns all permutations of a given string
39+
*
40+
* @param string $string
41+
* @return array
42+
*/
43+
public function stringPermutations(string $string): array {
44+
$result = [];
45+
$strLen = \strlen($string);
46+
if (0 === $strLen) {
47+
return $result;
48+
}
49+
if (1 === $string) {
50+
$result[] = $string;
51+
return $result;
52+
}
53+
54+
$array = StringUtil::stringToArray($string);
55+
$result = $this->permute($array, "", $result);
56+
return $result;
57+
}
58+
59+
/**
60+
* returns all permutations of an given array of objects
61+
*
62+
* @param array $objects
63+
* @param $prefix
64+
* @param array $result
65+
* @return array
66+
*/
67+
private function permute(array $objects, $prefix, array $result) {
68+
$length = \count($objects);
69+
if (0 === $length) {
70+
$result[] = $prefix;
71+
} else {
72+
for ($i = 0; $i < $length; $i++) {
73+
$newObjects = \array_merge(
74+
\array_slice($objects, 0, $i),
75+
\array_slice($objects, $i + 1)
76+
);
77+
$newPrefix = $prefix . $objects[$i];
78+
79+
$result = $this->permute($newObjects, $newPrefix, $result);
80+
}
81+
}
82+
return $result;
83+
}
84+
85+
/**
86+
* returns all permutations of a given string
87+
*
88+
* @param int $number
89+
* @return array
90+
*/
91+
public function numberPermutations(int $number) {
92+
$result = [];
93+
$array = NumberUtil::intToArray($number);
94+
$result = $this->permute($array, "", $result);
95+
foreach ($result as &$item) {
96+
\settype($item, "integer");
97+
}
98+
return $result;
99+
}
100+
}

tests/Sorting/SortTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
225

326

427
use doganoo\PHPAlgorithms\Algorithm\Sorting\BubbleSort;
528
use doganoo\PHPAlgorithms\Algorithm\Sorting\MergeSort;
629
use doganoo\PHPAlgorithms\Algorithm\Sorting\SelectionSort;
730

31+
/**
32+
* Class SortTest
33+
*/
834
class SortTest extends \PHPUnit\Framework\TestCase {
935
public function testBubbleSort() {
1036
$bubbleSort = new BubbleSort();

tests/Various/PermutationTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
27+
namespace Various;
28+
29+
30+
use doganoo\PHPAlgorithms\Algorithm\Various\Permutation;
31+
use PHPUnit\Framework\TestCase;
32+
33+
class PermutationTest extends TestCase {
34+
35+
36+
public function testStringPermutation() {
37+
$permutation = new Permutation();
38+
$permutations = $permutation->stringPermutations("abcd");
39+
$this->assertTrue(24 === \count($permutations));
40+
41+
$permutations = $permutation->stringPermutations("abc");
42+
$this->assertTrue(\in_array("cba", $permutations));
43+
$this->assertTrue(\in_array("bac", $permutations));
44+
$this->assertTrue(\in_array("acb", $permutations));
45+
}
46+
47+
public function testNumberPermutation() {
48+
$permutation = new Permutation();
49+
$permutations = $permutation->numberPermutations(1234);
50+
$this->assertTrue(24 === \count($permutations));
51+
52+
$permutations = $permutation->numberPermutations(1234);
53+
$this->assertTrue(\in_array(4321, $permutations));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)