Skip to content

Commit bcfd3fc

Browse files
committed
Add trinary exercise
1 parent 2bac650 commit bcfd3fc

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

assignments/ruby/trinary/example.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Trinary
2+
3+
BASE = 3
4+
5+
attr_reader :digits
6+
def initialize(decimal)
7+
@digits = decimal.reverse.chars.collect(&:to_i)
8+
end
9+
10+
def to_decimal
11+
decimal = 0
12+
digits.each_with_index do |digit, index|
13+
decimal += digit * BASE**index
14+
end
15+
decimal
16+
end
17+
end

assignments/ruby/trinary/test.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'minitest/autorun'
2+
require 'minitest/pride'
3+
require_relative 'trinary'
4+
5+
class TrinaryTest < MiniTest::Unit::TestCase
6+
def test_trinary_1_is_decimal_1
7+
assert_equal 1, Trinary.new("1").to_decimal
8+
end
9+
10+
def test_trinary_2_is_decimal_2
11+
skip
12+
assert_equal 2, Trinary.new("2").to_decimal
13+
end
14+
15+
def test_trinary_10_is_decimal_3
16+
skip
17+
assert_equal 3, Trinary.new("10").to_decimal
18+
end
19+
20+
def test_trinary_11_is_decimal_4
21+
skip
22+
assert_equal 4, Trinary.new("11").to_decimal
23+
end
24+
25+
def test_trinary_100_is_decimal_9
26+
skip
27+
assert_equal 9, Trinary.new("100").to_decimal
28+
end
29+
30+
def test_trinary_112_is_decimal_14
31+
skip
32+
assert_equal 14, Trinary.new("112").to_decimal
33+
end
34+
35+
def test_trinary_222_is_26
36+
skip
37+
assert_equal 26, Trinary.new("222").to_decimal
38+
end
39+
40+
def test_trinary_1122000120_is_32091
41+
skip
42+
assert_equal 32091, Trinary.new("1122000120").to_decimal
43+
end
44+
45+
def test_invalid_trinary_is_decimal_0
46+
skip
47+
assert_equal 0, Trinary.new("carrot").to_decimal
48+
end
49+
end

assignments/shared/trinary.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The program should consider strings specifying an invalid trinary as the value 0.
2+
3+
Trinary numbers contain three symbols: 0, 1, and 2.
4+
5+
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.
6+
7+
```bash
8+
# "102012"
9+
1 0 2 0 1 2 # the number
10+
1*3^5 + 0*3^4 + 2*3^3 + 0*3^3 + 1*3^1 + 2*3^0 # the value
11+
243 + 0 + 54 + 0 + 3 + 2 = 302
12+
```
13+
14+
If you want to write extra tests and/or check your answers, feel free to use irb:
15+
16+
```ruby
17+
irb(main):001:0> 302.to_s(3)
18+
=> "102012"
19+
irb(main):002:0> 81.to_s(3)
20+
=> "10000"
21+
irb(main):003:0> 91.to_s(3)
22+
=> "10101"
23+
```

assignments/shared/trinary.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
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."
3+
source: "All of Computer Science"
4+
source_url: "http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-"

lib/exercism.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def self.current_curriculum
3434
grade-school robot-name leap etl space-age grains
3535
gigasecond triangle scrabble-score roman-numerals
3636
binary prime-factors raindrops allergies strain
37-
atbash-cipher accumulate crypto-square
37+
atbash-cipher accumulate crypto-square trinary
3838
)
3939
@curriculum.add(rb, rb_slugs)
4040

0 commit comments

Comments
 (0)