-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters