Gold Challenge - a solution


#1

Hello,

I tried to remember how to create recursive algorithm … seems to work … maybe not very clean. I hope it will help.

I created one container (container1) with 5 items. The third item is a container (container2) which contains 2 items.

Structure :
container1 - randomly filled with BNRItem’s method
|_ item1 (BNRItem *) randomly created
|_ item2 (BNRItem *) randomly created
|_ container2 (BNRContainer *) - randomly filled with BNRItem’s method
…|_ itemA (BNRItem *) randomly created
…|_ itemB (BNRItem *) randomly created
|_ item3 (BNRItem *) randomly created
|_ item4 (BNRItem *) randomly created

Output :

[code]2012-08-26 15:15:50.101 RandomPossessions[35029:403] * Container: Container1 (5 items) cost 100$
2012-08-26 15:15:50.103 RandomPossessions[35029:403] Rusty Spork 73$ (173$)
2012-08-26 15:15:50.103 RandomPossessions[35029:403] Rusty Mac 40$ (213$)
2012-08-26 15:15:50.104 RandomPossessions[35029:403]
2012-08-26 15:15:50.104 RandomPossessions[35029:403] >> New Container
2012-08-26 15:15:50.104 RandomPossessions[35029:403] * Container: Container2 (2 items) cost 10$
2012-08-26 15:15:50.105 RandomPossessions[35029:403] Rusty Spork 40$ (253$)
2012-08-26 15:15:50.105 RandomPossessions[35029:403] Fluffy Spork 99$ (352$)
2012-08-26 15:15:50.106 RandomPossessions[35029:403]
2012-08-26 15:15:50.106 RandomPossessions[35029:403] Container2 10$ (362$)
2012-08-26 15:15:50.107 RandomPossessions[35029:403] Rusty Mac 10$ (372$)
2012-08-26 15:15:50.107 RandomPossessions[35029:403] Shiny Spork 93$ (465$)
2012-08-26 15:15:50.107 RandomPossessions[35029:403]

  • Container1 total = 465$ *[/code]

BNRContainer.h

[code]//
// BNRContainer.h
// RandomPossessions
//
// Created by Lionel on 25/08/12.
// Copyright © 2012 Feyr. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “BNRItem.h”

@interface BNRContainer : BNRItem
{
NSMutableArray *subItems;
}

// METHOD
// will be used to check each item in a container (called by the description method)

  • (int)processContenant:(BNRContainer *)item
    actualValue:(int)value;

// S&G

  • (void)setSubItems:(BNRItem *)item;
  • (id)subItems;

@end[/code]

and the bigger part … BNRContainer.m

[code]//
// BNRContainer.m
// RandomPossessions
//
// Created by Lionel on 25/08/12.
// Copyright © 2012 Feyr. All rights reserved.
//

#import “BNRContainer.h”

@implementation BNRContainer

// Over description

  • (NSString *)description {
    // return string : Total value of the container (sum of all items + container(s))
    NSString descriptionString = [[NSString alloc] initWithFormat:@"\n\n %@ total = %d$ *",[self itemName],[self processContenant:self actualValue:[self valueInDollars]]];
    return descriptionString;
    }

//**************
// recursive process on container items

  • (int)processContenant:(BNRContainer *)item
    actualValue:(int)value {
    // total of the container (= the valueInDollars of the container when it start)
    int totalValue = value;

    // print container name / its value / the total NSLog(@"* Container: %@ (%ld items) cost %d", itemName, [[item subItems] count],valueInDollars);

    // verify each item
    for (int i=0; i<[[item subItems] count]; i++) {
    // test if the new item is a new container
    if ([[subItems objectAtIndex:i] isMemberOfClass:[BNRContainer class]]) {
    // YES ! print “>> News Container"
    NSLog(@” “);
    NSLog(@”>> New Container");
    // recursive call on processContenant with new BNRContainer as argument
    totalValue = [[subItems objectAtIndex:i] processContenant:[subItems objectAtIndex:i] actualValue:totalValue];
    NSLog(@" “);
    }
    // add item value to totalValue
    totalValue += [[subItems objectAtIndex:i] valueInDollars];
    // print itemName / value / totalValue NSLog(@" %@ %d (%d$)”,[[subItems objectAtIndex:i] itemName], [[subItems objectAtIndex:i] valueInDollars], totalValue);
    }

    // retour de la somme des valeurs
    return totalValue;
    }

// S&G

  • (void)setSubItems:(BNRItem *)item {
    if (!subItems) {
    NSMutableArray *newArray = [[NSMutableArray alloc] initWithObjects:item, nil];
    subItems = newArray;
    } else {
    [subItems addObject:item];
    }
    }
  • (id)subItems {
    return subItems;
    }

@end[/code]

and after all … the main …

[code]//
// main.m
// RandomPossessions
//
// Created by Lionel on 25/08/12.
// Copyright © 2012 Feyr. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “BNRItem.h”
#import “BNRContainer.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    // create mutable array and store its address in items - init
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    //
    //GOLD CHALLENGE !!
    //     
    // create 2 containers manually
    BNRContainer *container = [[BNRContainer alloc] initWithItemName:@"Container1"
                                                      valueInDollars:100
                                                        serialNumber:@"AF1523"];
    BNRContainer *container2 = [[BNRContainer alloc] initWithItemName:@"Container2"
                                                       valueInDollars:10
                                                         serialNumber:@"HGT456"];
    
    // create a list of 2 BNRItem > container
    for (int i=0; i<2; i++) {
        BNRItem *p = [BNRItem randomItem];
        [items addObject:p];
        [container setSubItems:p];
    }
            
    // create a list of 2 more BNRItem > container2
    for (int i=0; i<2; i++) {
        BNRItem *p = [BNRItem randomItem];
        [items addObject:p];
        [container2 setSubItems:p];
    }
    
    // insert container2 as container item
    [container setSubItems:container2];
    
    // create a list of 2 new BNRItem > container
    for (int i=0; i<2; i++) {
        BNRItem *p = [BNRItem randomItem];
        [items addObject:p];
        [container setSubItems:p];
    }
    
    NSLog(@"%@",container);
    
    // destroy items
    items = nil;
    
}
return 0;

}[/code]