Kero
Kero

Reputation: 21

How to compare two numbers and pick the smallest to proceed with execution?

Let say number = 0 and otherNumber = 10. The loop will execute. But what if number = 10 and otherNumber = 0. I want the compiler to pick the smallest number and use it to proceed.

The code I have so far is here:

- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
    if (number <= otherNumber) {
        NSMutableString *indices = [NSMutableString stringWithCapacity:1];
        for (NSInteger i= number; i <= otherNumber; i++) {
            [indices appendFormat:@"%d", i];
        }
    } else {
        NSMutableString *indices = [NSMutableString stringWithCapacity:1];
        for (NSInteger i= otherNumber; i <= number; i++) {
            [indices appendFormat:@"%d", i];
        }
    }
    return @"%@", indices;
}

Upvotes: 2

Views: 58

Answers (3)

Reece Sheppard
Reece Sheppard

Reputation: 131

Pick one of the variables to always be considered the larger variable, and then do a simple swap-test before the function body proper.

- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
    if (number < otherNumber) {
        NSInteger temp = number;
        number = otherNumber;
        otherNumber = temp;
    }

    NSMutableString *indices = [NSMutableString stringWithCapacity:1];
    for (NSInteger i= otherNumber; i <= number; i++) {
        [indices appendFormat:@"%d", i];
    }

    return @"%@", indices;
}

(Apologies for any code errors, Objective-C isn't my primary language)

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

A simple fix is to move the declaration of indices outside the conditional:

- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
    NSMutableString *indices = [NSMutableString stringWithCapacity:1];
    if (number <= otherNumber) {
        for (NSInteger i= number; i <= otherNumber; i++) {
            [indices appendFormat:@"%d", i];
        }
    } else {
        for (NSInteger i= otherNumber; i <= number; i++) {
            [indices appendFormat:@"%d", i];
        }
    }
    return indices;
}

You could further unify your like this:

- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
    NSInteger from, to;
    if (number <= otherNumber) {
        from = number;
        to = otherNumber;
    } else {
        from = otherNumber;
        to = number;
    }
    NSMutableString *indices = [NSMutableString stringWithCapacity:1];
    for (NSInteger i= from; i <= tp; i++) {
        [indices appendFormat:@"%d", i];
    }
    return indices;
}

Upvotes: 2

rmaddy
rmaddy

Reputation: 318794

One option would be:

- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
    NSInteger minNum = MIN(number, otherNumber);
    NSInteger maxNum = MAX(number, otherNumber);

    NSMutableString *indices = [NSMutableString stringWithCapacity:maxNum - minNum + 1];
    for (NSInteger i = minNum; i <= maxNum; i++) {
        [indices appendFormat:@"%d", i];
    }

    return indices; // or [indices copy];
}

Upvotes: 2

Related Questions