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

jaishreedhage'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/AllYourBaseTest/AllYourBase.h

//
//  AllYourBase.h
//  AllYourBase
//
//  Created by Jaishree Dhage on 11/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface AllYourBase : NSObject

+(NSArray<NSNumber *> *)outputDigitsForInputBase:(NSInteger)inputBase inputDigits:(NSArray<NSNumber*> *)input outputBase:(NSInteger)outputBase;

@end

AllYourBase/AllYourBaseTest/AllYourBase.m

//
//  AllYourBase.m
//  AllYourBase
//
//  Created by Jaishree Dhage on 11/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import "AllYourBase.h"

@implementation AllYourBase

+(NSInteger) convertBase:(NSArray<NSNumber *> *)input initialBase:(NSInteger) base{
    
    NSInteger value = 0;
    for(int i=[input count]-1,k=0;i>=0;i--,k++){
        value += [input[i] integerValue]*pow(base,k);
    }
    return value;
}

+(void)testGenuinityForInputBase:(NSInteger)inputBase inputDigits:(NSArray<NSNumber *> *)input outputBase:(NSInteger)outputBase{
    
    if(inputBase<=1 || outputBase<=1)
        @throw [NSException exceptionWithName:@"Invalid base values" reason:@"Invalid" userInfo:@{}];
    
    for(NSNumber *n in input){
        if([n integerValue]>= inputBase || [n integerValue]<0)
            @throw [NSException exceptionWithName:@"Invalid array" reason:@"Invalid" userInfo:@{}];

    }
    
    
}

+(NSArray<NSNumber *> *)outputDigitsForInputBase:(NSInteger)inputBase inputDigits:(NSArray<NSNumber *> *)input outputBase:(NSInteger)outputBase{

    [AllYourBase testGenuinityForInputBase:(NSInteger)inputBase inputDigits:(NSArray<NSNumber *> *)input outputBase:(NSInteger)outputBase];
    
    NSInteger value;
    if(inputBase!=10)
        value = [AllYourBase convertBase:input initialBase:inputBase];
    else
        value = [[input componentsJoinedByString:@""] integerValue];
    
    NSMutableArray<NSNumber *> * output = [NSMutableArray array];
    while(value>0){
        [output addObject:[NSNumber numberWithInteger:(value%outputBase)]];
        value /= outputBase;
    }
    return [[output reverseObjectEnumerator] allObjects];
    
}

@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?