views:

151

answers:

2

Given a string and a number, this function will produce all possible combinations of n letters from that string.

i.e. If I pass "abcd" and 3, then I should get the following output:

abc
abd
acd
bcd

This is the code

- (void)viewDidLoad {
    [super viewDidLoad];
    [self expand_combinations:@"abcd" arg2:@"" arg3:3];
}

-(void) expand_combinations: (NSString *) remaining_string arg2:(NSString *)s arg3:(int) remain_depth
{
    if(remain_depth==0)
    {
        printf("%s\n",[s UTF8String]);
        return;
    }

    for(int k=0; k < [remaining_string length]; ++k)
    {
        s = [s stringByAppendingString:[[remaining_string substringFromIndex:k] substringToIndex:1]];
        [self expand_combinations:[remaining_string substringFromIndex:k+1] arg2:s arg3:remain_depth - 1];
    }
    return;
}

Instead this is what it prints out

abc
abcd
abcd
abcd
+1  A: 

Just a quick and dirty solution (didn't rename variable names and so on) for your function

-(void) expand_combinations: (NSString *) remaining_string arg2:(NSString *)s arg3:(int) remain_depth
{
    NSString *newString = [remaining_string stringByReplacingCharactersInRange:NSMakeRange(remain_depth, 1) withString:@""];
    // NSLog(newString);

    if(remain_depth==0)
    {
        printf("%s\n",[s UTF8String]);
        return;
    }
    [self expand_combinations:remaining_string arg2:s arg3:remain_depth - 1];
    return;
}

Output (I used NSLog)

2010-09-26 04:27:26.462 Untitled[5417:207] abc
2010-09-26 04:27:26.462 Untitled[5417:207] abd
2010-09-26 04:27:26.463 Untitled[5417:207] acd
2010-09-26 04:27:26.467 Untitled[5417:207] bcd
Henrik P. Hessel
There's something wrong with this. It doesn't print anything. Where did you put the log statement?
awakeFromNib
@awake, there's nothing wrong, please look at the code (and its comments) again.
Arjan
I copy and pasted the code. It doesn't work.
awakeFromNib
@awake, did you look at the comment in the code, like I wrote?
Arjan
Just uncomment the NSLog to see the new string in your Console. I commented it out.
Henrik P. Hessel
A: 

I found the solution. Here it is.

- (void)viewDidLoad {
    [super viewDidLoad];
    [self expand_combinations:@"abcd" arg2:@"" arg3:3];
}

-(void) expand_combinations: (NSString *) remaining_string arg2:(NSString *)s arg3:(int) remain_depth
{
    if(remain_depth==0)
    {
        printf("%s\n",[s UTF8String]);
        return;
    }

    NSString *str = [[NSString alloc] initWithString:s];
    for(int k=0; k < [remaining_string length]; ++k)
    {

        str = [s stringByAppendingString:[[remaining_string substringFromIndex:k] substringToIndex:1]];
        [self expand_combinations:[remaining_string substringFromIndex:k+1] arg2:str arg3:remain_depth - 1];

    }
    return;
}

The changes I made to make it work are in the line

NSString *str = [[NSString alloc] initWithString:s];

From there I replace every instance of s with str. It seems that by using s instead of str, some important value was getting overwritten which was causing the problem to not work correctly. It is necessary to create another NSString variable str so that the contents of s do not get overwritten.

awakeFromNib
Arjan
I added an explanation to my answer, so remove the -1 vote. :)
awakeFromNib
awakeFromNib: This version leaks the string you created in that initializer, by replacing it with the string you obtained from your first `stringByAppendingString:` message. There is no reason to create that first string object at all. Also, you should read up on `substringWithRange:`.
Peter Hosey