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:
To include color from the command line:
ruby -r minitest/pride trinary_test.rb
All of Computer Science http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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, # not your solution. # # 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 # # If you are curious, read more about constants on RubyDoc: # 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 attr_reader :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
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.