CodeIgniter Forums
n Strings taken r at a time - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: n Strings taken r at a time (/showthread.php?tid=21081)



n Strings taken r at a time - El Forum - 07-30-2009

[eluser]The Spider[/eluser]
Hi,

I have a set of strings $set={a,b,c,...,z}.
My aim is to develop a function to return all possible unique combinations.
The function must have 2 arguments. one for the entire set of string and the 2nd argument is an integer.

Eg: If the function name is strComb($set, 3) will return:

abc
abd
abe
abf....
...
abz
....
...
....
zzz.

(That means abc,acb,bca, bac, cab, cba are taken as identical quantity and so included only once:abc.)

Can anyone help me?


n Strings taken r at a time - El Forum - 07-30-2009

[eluser]jedd[/eluser]
[quote author="The Spider" date="1248969994"]
Can anyone help me?[/quote]

Have you tried asking your teacher and/or the other people in your class that got this homework question?


n Strings taken r at a time - El Forum - 07-30-2009

[eluser]The Spider[/eluser]
Hi jedd,

Thanks for your response.
No one has any idea about this.
I didnt get any help.


n Strings taken r at a time - El Forum - 08-05-2009

[eluser]The Spider[/eluser]
Hi,
I got it!!!

function stringPermute($string,$num){
$last = substr($orgString, 0, $num);
$result = array();
while($last != substr($orgString, -$num)){
$result[] = $last;
$last = getNextString($orgString,$last,$num-1);
}
$result[] = $last;
return $result;
}
function getNextString($fullString,$string,$char){
if($string{$char} <> lastElement($fullString)){
$string{$char} = $fullString{strpos($fullString,$string{$char})+1};
return $string;
}else{
$string = change($string,$fullString{0},$char);
return getNextString($fullString,$string,$char-1);
}
}
function lastElement($string){
return $string{strlen($string)-1};
}
function change($string,$char,$start = 0,$end = 0){
if($end == 0) $end = strlen($string)-1;
for($i=$start;$i<=$end;$i++){
$string{$i} = $char;
}
return $string;
}

$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
try stringPermute($string,3)