Welcome Guest, Not a member yet? Register   Sign In
n Strings taken r at a time
#1

[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?
#2

[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?
#3

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

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

[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)




Theme © iAndrew 2016 - Forum software by © MyBB