ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Instructions

Test suite

Solution

Convert an octal number, represented as a string (e.g. '1735263'), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion).

Implement octal to decimal conversion. Given an octal input string, your program should produce a decimal output.

- Implement the conversion yourself. Do not use something else to perform the conversion for you.
- Treat invalid input as octal 0.

Decimal is a base-10 system.

A number 233 in base 10 notation can be understood as a linear combination of powers of 10:

- The rightmost digit gets multiplied by 10^0 = 1
- The next number gets multiplied by 10^1 = 10
- ...
- The
*n*th number gets multiplied by 10^*(n-1)*. - All these values are summed.

So:

```
233 # decimal
= 2*10^2 + 3*10^1 + 3*10^0
= 2*100 + 3*10 + 3*1
```

Octal is similar, but uses powers of 8 rather than powers of 10.

So:

```
233 # octal
= 2*8^2 + 3*8^1 + 3*8^0
= 2*64 + 3*8 + 3*1
= 128 + 24 + 3
= 155
```

To run the tests, run the command `busted`

from within the exercise directory.

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.

All of Computer Science http://www.wolframalpha.com/input/?i=base+8

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

```
local Octal = require('./octal')
describe('octal', function()
it('should convert 1 to decimal 1', function()
assert.equal(1, Octal('1').to_decimal())
end)
it('should convert 10 to decimal 8', function()
assert.equal(8, Octal('10').to_decimal())
end)
it('should convert 17 to decimal 15', function()
assert.equal(15, Octal('17').to_decimal())
end)
it('should convert 11 to decimal 9', function()
assert.equal(9, Octal('11').to_decimal())
end)
it('should convert 130 to decimal 88', function()
assert.equal(88, Octal('130').to_decimal())
end)
it('should convert 2047 to decimal 1063', function()
assert.equal(1063, Octal('2047').to_decimal())
end)
it('should convert 7777 to decimal 4095', function()
assert.equal(4095, Octal('7777').to_decimal())
end)
it('should convert 1234567 to decimal 342391', function()
assert.equal(342391, Octal('1234567').to_decimal())
end)
it('should return 0 when the octal string contains invalid characters', function()
assert.equal(0, Octal('carrot').to_decimal())
assert.equal(0, Octal('123z456').to_decimal())
end)
it('should return 0 when the octal string contains invalid digits', function()
assert.equal(0, Octal('8').to_decimal())
assert.equal(0, Octal('9').to_decimal())
end)
end)
```

```
return function(input)
return { to_decimal = function() return tonumber(input, 8) or 0 end }
end
```

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?

Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

Hmm... I don't think you're supposed to use the built-in Lua functions :)