views:

178

answers:

1

I need a function that does the same thing as the following:

static public IEnumerable<string> permute(string word)
{
    if (word.Length > 1)
    {

        char character = word[0];
        foreach (string subPermute in permute(word.Substring(1)))
        {

            for (int index = 0; index <= subPermute.Length; index++)
            {
                string pre = subPermute.Substring(0, index);
                string post = subPermute.Substring(index);

                if (post.Contains(character))
                        continue;                       

                yield return pre + character + post;
            }

        }
    }
    else
    {
        yield return word;
    }
}

I tried this

-(NSString *) permute:(NSString *)str
{
    NSLog(@"permuting %@", str);

    NSString *permutedString = [[NSString alloc] init];
    if (str.length > 1) {
        NSString *subPermute = [[NSString alloc] init];
        for (subPermute in [str substringFromIndex:1])
             {
                 for (int i = 0; i <= [subPermute length];i++) {
                     NSString *pre = [[NSString alloc] initWithString:[subPermute substringToIndex:i]];
                     NSString *post = [[NSString alloc] initWithString:[subPermute substringFromIndex:i]];

                     return [pre stringByAppendingFormat:@"%@%@", [str substringToIndex:1], post];

                 }

             }

        }
    else {
        NSLog(@"permuted string = %@", permutedString);
        return permutedString;
    }
    return permutedString;



}

However I get a warning message for the following line:

for (subPermute in [str substringFromIndex:1])

How should this be changed?

+3  A: 

Well, first off, comparing the original to the Objective C++:

foreach (string subPermute in permute(word.Substring(1)))

for (subPermute in [str substringFromIndex:1])

The original is recursively calling itself to compute all the permutations of the tail of the input string, from the second character on. Your version has no such recursive call.

Secondly, the original function returns an IEnumerable, which is not the same thing as just returning a string. IEnumerables basically let you write a loop that can suspend itself and simultaneously return one value in a set of values. The next time a value in the set is needed, execution picks up at the point after the yield.

I don't believe Objective C++ has such a feature. Therefore, I think your best option would be to have your new function return an NSMutableArray of NSStrings. Objective C++ does allow you to iterate over the elements of an array. Here's a working version:

-(NSMutableArray *) permute:(NSString *)str
{
    NSMutableArray * result;

    if ([str length] > 1) {
        result = [NSMutableArray array];

        for (NSString * subPermute in [self permute:[str substringFromIndex:1]]) {
            for (int i = 0; i <= [subPermute length]; i++) {
                NSString * pre = [subPermute substringToIndex:i];
                NSString * post = [subPermute substringFromIndex:i];

                [result addObject:[pre stringByAppendingFormat:@"%@%@", [str substringToIndex:1], post]];
            }
        }
    }
    else {
        result = [NSMutableArray arrayWithObject:str];
    }

    return result;
}
SCFrench
I get a warning message for the line that says for (NSString *subPermute in [self permute:[str substringFromIndex:1]])
awakeFromNib
See edited answer for a working version.
SCFrench