Avatar of mstange22

mstange22's solution

to All Your Base in the C# Track

Published at Oct 01 2019 · 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!

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit AllYourBase.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

AllYourBaseTest.cs

// This file was auto-generated based on version 2.3.0 of the canonical data.

using System;
using Xunit;

public class AllYourBaseTest
{
    [Fact]
    public void Single_bit_one_to_decimal()
    {
        var inputBase = 2;
        var digits = new[] { 1 };
        var outputBase = 10;
        var expected = new[] { 1 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Binary_to_single_decimal()
    {
        var inputBase = 2;
        var digits = new[] { 1, 0, 1 };
        var outputBase = 10;
        var expected = new[] { 5 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Single_decimal_to_binary()
    {
        var inputBase = 10;
        var digits = new[] { 5 };
        var outputBase = 2;
        var expected = new[] { 1, 0, 1 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Binary_to_multiple_decimal()
    {
        var inputBase = 2;
        var digits = new[] { 1, 0, 1, 0, 1, 0 };
        var outputBase = 10;
        var expected = new[] { 4, 2 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Decimal_to_binary()
    {
        var inputBase = 10;
        var digits = new[] { 4, 2 };
        var outputBase = 2;
        var expected = new[] { 1, 0, 1, 0, 1, 0 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Trinary_to_hexadecimal()
    {
        var inputBase = 3;
        var digits = new[] { 1, 1, 2, 0 };
        var outputBase = 16;
        var expected = new[] { 2, 10 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Hexadecimal_to_trinary()
    {
        var inputBase = 16;
        var digits = new[] { 2, 10 };
        var outputBase = 3;
        var expected = new[] { 1, 1, 2, 0 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_15_bit_integer()
    {
        var inputBase = 97;
        var digits = new[] { 3, 46, 60 };
        var outputBase = 73;
        var expected = new[] { 6, 10, 45 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Empty_list()
    {
        var inputBase = 2;
        var digits = Array.Empty<int>();
        var outputBase = 10;
        var expected = new[] { 0 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Single_zero()
    {
        var inputBase = 10;
        var digits = new[] { 0 };
        var outputBase = 2;
        var expected = new[] { 0 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_zeros()
    {
        var inputBase = 10;
        var digits = new[] { 0, 0, 0 };
        var outputBase = 2;
        var expected = new[] { 0 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Leading_zeros()
    {
        var inputBase = 7;
        var digits = new[] { 0, 6, 0 };
        var outputBase = 10;
        var expected = new[] { 4, 2 };
        Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Input_base_is_one()
    {
        var inputBase = 1;
        var digits = new[] { 0 };
        var outputBase = 10;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Input_base_is_zero()
    {
        var inputBase = 0;
        var digits = Array.Empty<int>();
        var outputBase = 10;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Input_base_is_negative()
    {
        var inputBase = -2;
        var digits = new[] { 1 };
        var outputBase = 10;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_digit()
    {
        var inputBase = 2;
        var digits = new[] { 1, -1, 1, 0, 1, 0 };
        var outputBase = 10;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Invalid_positive_digit()
    {
        var inputBase = 2;
        var digits = new[] { 1, 2, 1, 0, 1, 0 };
        var outputBase = 10;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Output_base_is_one()
    {
        var inputBase = 2;
        var digits = new[] { 1, 0, 1, 0, 1, 0 };
        var outputBase = 1;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Output_base_is_zero()
    {
        var inputBase = 10;
        var digits = new[] { 7 };
        var outputBase = 0;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Output_base_is_negative()
    {
        var inputBase = 2;
        var digits = new[] { 1 };
        var outputBase = -7;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }

    [Fact(Skip = "Remove to run test")]
    public void Both_bases_are_negative()
    {
        var inputBase = -2;
        var digits = new[] { 1 };
        var outputBase = -7;
        Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
    }
}
using System;
using System.Collections.Generic;

public static class AllYourBase
{
    public static int[] Rebase(int inputBase, int[] inputDigits, int outputBase)
    {
        int base10 = 0;
        var outputDigits = new List<int>();

        if (inputBase < 2 || outputBase < 2) {
            throw new ArgumentException("Invalid base.");
        }

        int exp = inputDigits.Length - 1;
        foreach (var digit in inputDigits) {
            if (digit < 0 || digit >= inputBase) {
                throw new ArgumentException("Invalid digit.");
            }
            base10 += digit * (int)Math.Pow(inputBase, exp--);
        }

        if (base10 == 0) {
            return new []{ 0 };
        }

        while (base10 > 0) {
            outputDigits.Insert(0, base10 % outputBase);
            base10 /= outputBase;
        }

        return outputDigits.ToArray();
    }
}

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?