Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Trinary in the CoffeeScript Track

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

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert a trinary number, represented as a string (e.g. '102012'), to its decimal equivalent using first principles.

The program should consider strings specifying an invalid trinary as the value 0.

Trinary numbers contain three symbols: 0, 1, and 2.

The last place in a trinary number is the 1's place. The second to last is the 3's place, the third to last is the 9's place, etc.

# "102012"
    1       0       2       0       1       2    # the number
1*3^5 + 0*3^4 + 2*3^3 + 0*3^2 + 1*3^1 + 2*3^0    # the value
  243 +     0 +    54 +     0 +     3 +     2 =  302

If your language provides a method in the standard library to perform the conversion, pretend it doesn't exist and implement it yourself.

Refer to the Exercism CoffeScript page for getting started with CoffeeScript.

In order to run the test, you can run the test file from the exercise directory:

jasmine-node --coffee .

Source

All of Computer Science http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-

Submitting Incomplete Solutions

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

trinary_test.spec.coffee

Trinary = require './trinary'

describe 'Trinary', ->

  it '1 is decimal 1', ->
    expect(new Trinary('1').toDecimal()).toEqual 1

  xit '2 is decimal 2', ->
    expect(new Trinary('2').toDecimal()).toEqual 2

  xit '10 is decimal 3', ->
    expect(new Trinary('10').toDecimal()).toEqual 3

  xit '11 is decimal 4', ->
    expect(new Trinary('11').toDecimal()).toEqual 4

  xit '100 is decimal 9', ->
    expect(new Trinary('100').toDecimal()).toEqual 9

  xit '112 is decimal 14', ->
    expect(new Trinary('112').toDecimal()).toEqual 14

  xit '222 is 26', ->
    expect(new Trinary('222').toDecimal()).toEqual 26

  xit '1122000120 is 32091', ->
    expect(new Trinary('1122000120').toDecimal()).toEqual 32091

  xit 'invalid trinary is decimal 0', ->
    expect(new Trinary('carrot').toDecimal()).toEqual 0
module.exports = class Trinary
  constructor: (@str) ->
    @reverse_str = @str.split("").reverse().join("")

  toDecimal: ->
    return 0 if (/[^012]/).test(@str)

    result = 0
    for i in [0..@reverse_str.length - 1]
      result += @reverse_str[i] * (3 ** i)

    result

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?