-3

I want to develop in Objective-C all possible sums to reach a result, like in this another thread, but in Objective-C

Example in JAVA

The code in JAVA is:

public void sum_up_recursive(ArrayList<Integer> numbers, int target, ArrayList<Integer> partial) {
           int s = 0;
           for (int x: partial) s += x;
           if (s == target){
                System.out.println("sum("+Arrays.toString(partial.toArray())+")="+target);
           }
           if (s >= target)
                return;
           for(int i=0;i<numbers.size();i++) {
                 ArrayList<Integer> remaining = new ArrayList<Integer>();
                 int n = numbers.get(i);
                 for (int j=i+1; j<numbers.size();j++) remaining.add(numbers.get(j));
                 ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
                 partial_rec.add(n);
                 sum_up_recursive(remaining,target,partial_rec);
           }
        }

My intent in Objective-C:

Main Class:

    NSMutableArray *numbers = [[NSMutableArray alloc] init];

    [numbers addObject:@2];
    [numbers addObject:@4];
    [numbers addObject:@8];
    [numbers addObject:@16];
    [numbers addObject:@32];

    NSMutableArray *partial = [[NSMutableArray alloc] init];

    [testclass sum_up_recursive:numbers : 12 :partial];

Method:

- (void) sum_up_recursive: (NSMutableArray*) numbers: (int) target:(NSMutableArray*) partial{

    NSInteger s = 0;

    for (NSNumber *x in partial){
        s = s + [x integerValue];        
    }

    if (s == target){
        NSLog(@"RESULT: %@", partial);
    }

    if (s >= target){
        return;
    }

    for (NSInteger i = 0 ; i < [numbers count]; i++) {

        NSMutableArray* remaining;
        NSInteger n = [numbers objectAtIndex:i];

        for (NSInteger j=i+1 ; j < [numbers count]; j++) {
            [remaining addObject:[numbers objectAtIndex:j]];
        }

        NSMutableArray *partial_rec = partial;
        [partial_rec addObject: [NSNumber numberWithInteger:n]];

        [self sum_up_recursive:remaining:target:partial_rec];
    }

}

I have not errors, but I have not result, I can't reach the target value, I'm a Android developer probably anybody with more experience can see what is the error in my code or give another opinion.

I fixed the errors, this method works perfectly:

- (void) sumUpRecursive:(NSMutableArray *)numbers withTarget:(NSInteger)target withPartial:(NSMutableArray *)partial {

    NSInteger s = 0;

    for (NSNumber *x in partial){
        s = s + [x integerValue];
    }

    if (s == target){
        NSLog(@"RESULT: %@", partial);
    }

    if (s >= target){
        return;
    }

    for (NSInteger i = 0; i < [numbers count]; i++) {

        NSMutableArray *remaining = [[NSMutableArray alloc] init];
        NSInteger n = [[numbers objectAtIndex:i] integerValue];

        for (NSInteger j = i + 1; j < [numbers count]; j++) {
            [remaining addObject:[numbers objectAtIndex:j]];
        }

        NSMutableArray *partial_rec = [[NSMutableArray alloc] init];
        [partial_rec addObjectsFromArray:partial];

        [partial_rec addObject: [NSNumber numberWithInteger:n]];

        [self sumUpRecursive:remaining withTarget:target withPartial:partial_rec];
    }

}

The output of this method is:

numbers(2, 4, 8, 16, 32)

1) Example, sum target: 12, Output: 4, 8 means.. 4+8.

2) Example, sum target: 14, Output: 2, 4, 8.

3) Example, sum target: 44, Ouput: 4, 8, 32

This question is not a duplicate, because in the another question the idea is search all combinations to sum, but the problem is only works with 2 numbers, in my case I need to try with N+ numbers.

Community
  • 1
  • 1

2 Answers2

1

First, you use quite non-standard syntax for your method.

- (void) sum_up_recursive: (NSMutableArray*) numbers: (int) target:(NSMutableArray*) partial{

Usually you would do something like

- (void)sumupRecursive:(NSMutableArray*)numbers target:(int)target partial:(NSMutableArray*)partial

The call is then something like

[self sumupRecursive:numbers target:100 partial:someOtherArray];

Two bugs in the code are that "remaining" will always be nil since you never set it to a mutable array, and "NSInteger n = [numbers objectAtIndex:i]" shouldn't compile; objectAtIndex returns an NSNumber* and not an NSInteger.

I didn't even try to understand what the code is supposed to do.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

I would expect your method definition to look more like the following:

- (void) sum_up_recursive:(NSMutableArray *)numbers withTarget:(NSInteger)target withPartial:(NSMutableArray *)partial

I can't really follow the Objective-C standards for a method because I have no idea what your method is actually doing.

My attempt at your method (I don't know Java) is below (CODE NOT TESTED):

- (void) sumUpRecursive:(NSMutableArray *)numbers withTarget:(NSInteger)target withPartial:(NSMutableArray *)partial {

    NSInteger s = 0;

    for (NSNumber *x in partial){
        s = s + [x integerValue];        
    }

    if (s == target){
        NSLog(@"RESULT: %@", partial);
    }

    if (s >= target){
        return;
    }

    for (NSInteger i = 0; i < [numbers count]; i++) {

        NSMutableArray *remaining = [[NSMutableArray alloc] init];
        NSInteger n = [[numbers objectAtIndex:i] integerValue];

        for (NSInteger j = i + 1; j < [numbers count]; j++) {
            [remaining addObject:[numbers objectAtIndex:j]];
        }

        // Initial code
        //NSMutableArray *partial_rec = partial;
        //[partial_rec addObject: [NSNumber numberWithInteger:n]];

        // Correct code from OP
        NSMutableArray *partial_rec = [[NSMutableArray alloc] init];
        [partial_rec addObjectsFromArray:partial];

        [self sumUpRecursive:remaining withTarget:target withPartial:partial_rec];
    }

}

You would call it as follows:

NSMutableArray *numbers = [[NSMutableArray alloc] init];

[numbers addObject:@2];
[numbers addObject:@4];
[numbers addObject:@8];
[numbers addObject:@16];
[numbers addObject:@32];

NSMutableArray *partial = [[NSMutableArray alloc] init];

[testclass sumUpRecursive:numbers withTarget:12 withPartial:partial];

I'm not really sure this is an answer but it's too long for a comment. You will get a better answer if you edit your question to give the expected output and the actual output.

Robotic Cat
  • 5,899
  • 4
  • 41
  • 58
  • Thanks a lot Robotic Cat, your code helps me a lot, but didn't work, but if you change the line of: *partial_rec, for : NSMutableArray *partial_rec = [[NSMutableArray alloc] init]; [partial_rec addObjectsFromArray:partial] works perfectly! – user3882109 Jan 05 '15 at 10:18
  • I add the new code and the Examples of the Ouputs from this method in the main question. – user3882109 Jan 05 '15 at 10:19