🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of KiligWY

KiligWY's solution

to All Your Base in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Convert a number, represented as a sequence of digits in one base, to any other base.

Implement general base conversion. Given a number in base a, represented as a sequence of digits, convert it to base b.

Note

  • Try to implement the conversion yourself. Do not use something else to perform the conversion for you.

About Positional Notation

In positional notation, a number in base b can be understood as a linear combination of powers of b.

The number 42, in base 10, means:

(4 * 10^1) + (2 * 10^0)

The number 101010, in base 2, means:

(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)

The number 1120, in base 3, means:

(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)

I think you got the idea!

Yes. Those three numbers above are exactly the same. Congratulations!

Setup

There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/all-your-base/AllYourBase.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

AllYourBaseTest.m

#import <XCTest/XCTest.h>

#if __has_include("AllYourBaseExample.h")
# import "AllYourBaseExample.h"
# else
# import "AllYourBase.h"
#endif

@interface AllYourBaseTest : XCTestCase

@end

@implementation AllYourBaseTest

- (void)testSingleBitOneToDecimal {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:2 inputDigits:@[@1] outputBase:10];
    NSArray<NSNumber *> *expected = @[@1];
    XCTAssertEqualObjects(result, expected);
}

- (void)testBinaryToSingleDecimal {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:2 inputDigits:@[@1, @0, @1] outputBase:10];
    NSArray<NSNumber *> *expected = @[@5];
    XCTAssertEqualObjects(result, expected);
}

- (void)testSingleDecimalToBinary {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:10 inputDigits:@[@5] outputBase:2];
    NSArray<NSNumber *> *expected = @[@1, @0, @1];
    XCTAssertEqualObjects(result, expected);
}

- (void)testBinaryToMultipleDecimal {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:2 inputDigits:@[@1, @0, @1, @0, @1, @0] outputBase:10];
    NSArray<NSNumber *> *expected = @[@4, @2];
    XCTAssertEqualObjects(result, expected);
}

- (void)testDecimalToBinary {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:10 inputDigits:@[@4, @2] outputBase:2];
    NSArray<NSNumber *> *expected = @[@1, @0, @1, @0, @1, @0];
    XCTAssertEqualObjects(result, expected);
}

- (void)testTrinaryToHexadecimal {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:3 inputDigits:@[@1, @1, @2, @0] outputBase:16];
    NSArray<NSNumber *> *expected = @[@2, @10];
    XCTAssertEqualObjects(result, expected);
}

- (void)testHexadecimalToTrinary {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:16 inputDigits:@[@2, @10] outputBase:3];
    NSArray<NSNumber *> *expected = @[@1, @1, @2, @0];
    XCTAssertEqualObjects(result, expected);
}

- (void)test15BitInteger {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:97 inputDigits:@[@3, @46, @60] outputBase:73];
    NSArray<NSNumber *> *expected = @[@6, @10, @45];
    XCTAssertEqualObjects(result, expected);
}

- (void)testEmptyList {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:2 inputDigits:@[] outputBase:10];
    NSArray<NSNumber *> *expected = @[];
    XCTAssertEqualObjects(result, expected);
}

- (void)testSingleZero {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:10 inputDigits:@[@0] outputBase:2];
    NSArray<NSNumber *> *expected = @[];
    XCTAssertEqualObjects(result, expected);
}

- (void)testMultipleZeros {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:10 inputDigits:@[@0, @0, @0] outputBase:2];
    NSArray<NSNumber *> *expected = @[];
    XCTAssertEqualObjects(result, expected);
}

- (void)testLeadingZeros {
    NSArray<NSNumber *> *result = [AllYourBase outputDigitsForInputBase:7 inputDigits:@[@0, @6, @0] outputBase:10];
    NSArray<NSNumber *> *expected = @[@4, @2];
    XCTAssertEqualObjects(result, expected);
}

- (void)testNegativeDigit {
    NSArray<NSNumber *> *inputDigits = @[@1, @(-1), @1, @0, @1, @0];
    XCTAssertThrows([AllYourBase outputDigitsForInputBase:2 inputDigits:inputDigits outputBase:10]);
}

- (void)testInvalidPositiveDigit {
    NSArray<NSNumber *> *inputDigits = @[@1, @2, @1, @0, @1, @0];
    XCTAssertThrows([AllYourBase outputDigitsForInputBase:2 inputDigits:inputDigits outputBase:10]);
}

- (void)testFirstBaseIsOne {
    NSArray<NSNumber *> *inputDigits = @[];
    XCTAssertThrows([AllYourBase outputDigitsForInputBase:1 inputDigits:inputDigits outputBase:10]);
}

- (void)testSecondBaseIsOne {
    NSArray<NSNumber *> *inputDigits = @[@1, @0, @1, @0, @1, @0];
    XCTAssertThrows([AllYourBase outputDigitsForInputBase:2 inputDigits:inputDigits outputBase:1]);
}

- (void)testFirstBaseIsZero {
    NSArray<NSNumber *> *inputDigits = @[];
    XCTAssertThrows([AllYourBase outputDigitsForInputBase:0 inputDigits:inputDigits outputBase:10]);
}

- (void)testSecondBaseIsZero {
    NSArray<NSNumber *> *inputDigits = @[@7];
    XCTAssertThrows([AllYourBase outputDigitsForInputBase:10 inputDigits:inputDigits outputBase:0]);
}

- (void)testFirstBaseIsNegative {
    NSArray<NSNumber *> *inputDigits = @[@1];
    XCTAssertThrows([AllYourBase outputDigitsForInputBase:-2 inputDigits:inputDigits outputBase:10]);
}

- (void)testSecondBaseIsNegative {
    NSArray<NSNumber *> *inputDigits = @[@1];
    XCTAssertThrows([AllYourBase outputDigitsForInputBase:2 inputDigits:inputDigits outputBase:-7]);
}

@end

AllYourBase.h

#import <Foundation/Foundation.h>

@interface AllYourBase : NSObject

+ (NSArray *)outputDigitsForInputBase:(NSInteger)inputBase inputDigits:(NSArray *)inputDigits outputBase:(NSInteger)outputBase;

@end

AllYourBase.m

#import "AllYourBase.h"

@implementation AllYourBase

+ (NSArray *)outputDigitsForInputBase:(NSInteger)inputBase inputDigits:(NSArray *)inputDigits outputBase:(NSInteger)outputBase {
    NSAssert(inputBase > 1, @"inputBase > 1");
    NSAssert(outputBase > 1, @"outputBase > 1");
    
    NSInteger decimalNumber = 0;
    for (NSInteger i = 0; i < inputDigits.count; ++i) {
        NSInteger inputDigit = [inputDigits[i] integerValue];
        NSAssert(inputDigit < inputBase && inputDigit >= 0, @"inputDigit > 0 && inputDigit < inputBase");
        decimalNumber += (inputDigit * pow(inputBase, inputDigits.count - i - 1));
    }
    
    NSInteger index = 0;
    for (; ; ++index) {
        if (pow(outputBase, index) > decimalNumber) {
            index--;
            break;
        }
    }
    
    NSMutableArray *result = [NSMutableArray array];
    while (index >= 0) {
        NSInteger value = pow(outputBase, index);
        NSInteger digit = 0;
        if (value > decimalNumber) {
            value = 0;
        } else {
            digit = decimalNumber / value;
        }
        [result addObject:@(digit)];
        decimalNumber -= (value * digit);
        index--;
    }
    
    return result;
}

@end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?