Created
February 17, 2013 15:47
-
-
Save noxifoxi/4971954 to your computer and use it in GitHub Desktop.
Something weird I tried
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/plain'); | |
class Combinations { | |
public static function GetIt($String) { | |
echo '[n, s]'."\n"; | |
self::GetCombinations(str_split($String), 0, 0); | |
} | |
private static function GetCombinations($a, $n, $s) { | |
// [a, s, d, f] | |
$new = [$a[$n]]; | |
for($i = $s; $i < sizeof($a) + 1; $i++) { | |
if($i == $n) | |
continue; | |
if(sizeof($new) != sizeof($a)) { | |
if($i == sizeof($a)) | |
$i = 0; | |
if($i == $n) | |
continue; | |
$new[] = $a[$i]; | |
echo $i; | |
} | |
} | |
echo '['.$n.', '.$s.'] '.implode('', $new)."\n"; | |
if($s < sizeof($a)) | |
self::GetCombinations($a, $n, $s + 1); | |
} | |
} | |
Combinations::GetIt('asdf'); | |
/* Expected: | |
asdf | |
adfs | |
afsd | |
asfd | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment