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

Jaboblee's solution

to All Your Base in the Lua Track

Published at Apr 07 2020 · 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 busted from within the exercise directory.

Further information

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

Submitting Incomplete Solutions

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

all-your-base_spec.lua

local all_your_base = require 'all-your-base'

describe('all-your-base', function()
  it('should convert a single binary digit to a single decimal digit', function()
    assert.are.same({ 1 }, all_your_base.convert({ 1 }, 2).to(10))
  end)

  it('should convert multiple binary digits to a single decimal digit', function()
    assert.are.same({ 5 }, all_your_base.convert({ 1, 0, 1 }, 2).to(10))
  end)

  it('should convert a single decimal digit to binary', function()
    assert.are.same({ 1, 0, 1 }, all_your_base.convert({ 5 }, 10).to(2))
  end)

  it('should convert binary to decimal', function()
    assert.are.same({ 1, 0, 1, 0, 1, 0 }, all_your_base.convert({ 4, 2 }, 10).to(2))
  end)

  it('should convert decimal to binary', function()
    assert.are.same({ 4, 2 }, all_your_base.convert({ 1, 0, 1, 0, 1, 0 }, 2).to(10))
  end)

  it('should convert trinary to hexadecimal', function()
    assert.are.same({ 2, 10 }, all_your_base.convert({ 1, 1, 2, 0 }, 3).to(16))
  end)

  it('should convert hexadecimal to trinary', function()
    assert.are.same({ 1, 1, 2, 0 }, all_your_base.convert({ 2, 10 }, 16).to(3))
  end)

  it('should convert between large bases', function()
    assert.are.same({ 6, 10, 45 }, all_your_base.convert({ 3, 46, 60 }, 97).to(73))
  end)

  it('should convert no digits to 0', function()
    assert.are.same({ 0 }, all_your_base.convert({ }, 2).to(10))
  end)

  it('should convert no digits to 0', function()
    assert.are.same({ 0 }, all_your_base.convert({ }, 10).to(2))
  end)

  it('should convert multiple 0 digits to 0', function()
    assert.are.same({ 0 }, all_your_base.convert({ 0, 0, 0 }, 10).to(2))
  end)

  it('should ignore leading zeros', function()
    assert.are.same({ 4, 2 }, all_your_base.convert({ 0, 6, 0 }, 7).to(10))
  end)

  it('should not allow negative digits', function()
    assert.has_error(function()
      all_your_base.convert({ 1, -1, 1, 0, 1, 0 }, 2).to(10)
    end, 'negative digits are not allowed')
  end)

  it('should not allow digits that are out of range', function()
    assert.has_error(function()
      all_your_base.convert({ 1, 2, 1, 0, 1, 0 }, 2).to(10)
    end, 'digit out of range')
  end)

  it('should not allow an input base less than 2', function()
    assert.has_error(function()
      all_your_base.convert({ 1, 0, 1, 0, 1, 0 }, -1).to(10)
    end, 'invalid input base')

    assert.has_error(function()
      all_your_base.convert({ 1, 0, 1, 0, 1, 0 }, 0).to(10)
    end, 'invalid input base')

    assert.has_error(function()
      all_your_base.convert({ 1, 0, 1, 0, 1, 0 }, 1).to(10)
    end, 'invalid input base')
  end)

  it('should not allow an output base less than 2', function()
    assert.has_error(function()
      all_your_base.convert({ 1, 0, 1, 0, 1, 0 }, 2).to(-1)
    end, 'invalid output base')

    assert.has_error(function()
      all_your_base.convert({ 1, 0, 1, 0, 1, 0 }, 2).to(0)
    end, 'invalid output base')

    assert.has_error(function()
      all_your_base.convert({ 1, 0, 1, 0, 1, 0 }, 2).to(1)
    end, 'invalid output base')
  end)
end)
local all_your_base = {}

all_your_base.convert = function(from_digits, from_base)
    if (from_base < 2) then error("invalid input base") end

    local numberOfDigits = #from_digits - 1
    local rawResult = 0

    for k,v in ipairs(from_digits) do
        if (v < 0) then error("negative digits are not allowed") end
        if (v >= from_base) then error("digit out of range") end
        rawResult = rawResult + (v * from_base ^ numberOfDigits)
        numberOfDigits = numberOfDigits - 1
    end

    -- Returns digit table of raw result converted to to_base 
    local function to(to_base)
        if (to_base < 2) then error("invalid output base") end

        local result = {0}
        local i, j = 1, 1

        -- Divides raw result by desired base, saving each remainder as a digit from lsb to msb
        while (rawResult ~= 0) do
            result[i] = rawResult % to_base
            i = i + 1
            rawResult = math.floor(rawResult / to_base)
        end

        -- Reverses result order to match desired output
        i = i - 1
        while (j<i) do
            result[j], result[i] = result[i], result[j]
            j = j + 1
            i = i - 1
        end

        return result
    end
    return {to = to}
end

return all_your_base

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?