 # rootulp's solution

## to Trinary in the Ruby 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.

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby trinary_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride trinary_test.rb
``````

## 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.rb

``````require 'minitest/autorun'
require_relative 'trinary'

class TrinaryTest < Minitest::Test
def test_trinary_1_is_decimal_1
assert_equal 1, Trinary.new('1').to_decimal
end

def test_trinary_2_is_decimal_2
skip
assert_equal 2, Trinary.new('2').to_decimal
end

def test_trinary_10_is_decimal_3
skip
assert_equal 3, Trinary.new('10').to_decimal
end

def test_trinary_11_is_decimal_4
skip
assert_equal 4, Trinary.new('11').to_decimal
end

def test_trinary_100_is_decimal_9
skip
assert_equal 9, Trinary.new('100').to_decimal
end

def test_trinary_112_is_decimal_14
skip
assert_equal 14, Trinary.new('112').to_decimal
end

def test_trinary_222_is_26
skip
assert_equal 26, Trinary.new('222').to_decimal
end

def test_trinary_1122000120_is_32091
skip
assert_equal 32_091, Trinary.new('1122000120').to_decimal
end

def test_invalid_trinary_is_decimal_0
skip
assert_equal 0, Trinary.new('carrot').to_decimal
end

def test_invalid_trinary_with_digits_is_decimal_0
skip
assert_equal 0, Trinary.new('0a1b2c').to_decimal
end

def test_invalid_trinary_with_multiline_string
skip
assert_equal 0, Trinary.new("Invalid\n201\nString").to_decimal
end

def test_number_out_of_range
skip
assert_equal 0, Trinary.new('4').to_decimal
end

# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
#
# Define a constant named VERSION inside of the top level BookKeeping
# module.
#  In your file, it will look like this:
#
# module BookKeeping
#   VERSION = 1 # Where the version number matches the one in the test.
# end
#
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.htm
def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end``````
``````# Trinary
class Trinary
def initialize(trinary)
@trinary = trinary
end

def to_decimal
return 0 unless valid?
trinary.chars.reverse.map.with_index do |multiplier, power|
(3**power) * multiplier.to_i
end.reduce(:+)
end

private

def valid?
!trinary.match(/[^0-2]/)
end
end``````