Skip to content

Commit

Permalink
Add trinary exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Jun 14, 2013
1 parent 2bac650 commit bcfd3fc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
17 changes: 17 additions & 0 deletions assignments/ruby/trinary/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Trinary

BASE = 3

attr_reader :digits
def initialize(decimal)
@digits = decimal.reverse.chars.collect(&:to_i)
end

def to_decimal
decimal = 0
digits.each_with_index do |digit, index|
decimal += digit * BASE**index
end
decimal
end
end
49 changes: 49 additions & 0 deletions assignments/ruby/trinary/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'minitest/autorun'
require 'minitest/pride'
require_relative 'trinary'

class TrinaryTest < MiniTest::Unit::TestCase
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 32091, Trinary.new("1122000120").to_decimal
end

def test_invalid_trinary_is_decimal_0
skip
assert_equal 0, Trinary.new("carrot").to_decimal
end
end
23 changes: 23 additions & 0 deletions assignments/shared/trinary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.

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

If you want to write extra tests and/or check your answers, feel free to use irb:

```ruby
irb(main):001:0> 302.to_s(3)
=> "102012"
irb(main):002:0> 81.to_s(3)
=> "10000"
irb(main):003:0> 91.to_s(3)
=> "10101"
```
4 changes: 4 additions & 0 deletions assignments/shared/trinary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
blurb: "Write a program that will convert a trinary number, represented as a string (i.e. '102012'), to it's decimal equivalent using first principles."
source: "All of Computer Science"
source_url: "http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-"
2 changes: 1 addition & 1 deletion lib/exercism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.current_curriculum
grade-school robot-name leap etl space-age grains
gigasecond triangle scrabble-score roman-numerals
binary prime-factors raindrops allergies strain
atbash-cipher accumulate crypto-square
atbash-cipher accumulate crypto-square trinary
)
@curriculum.add(rb, rb_slugs)

Expand Down

0 comments on commit bcfd3fc

Please sign in to comment.