-
-
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
87 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,4 @@ | ||
--- | ||
blurb: "Write a program that will convert a binary number, represented as a string (i.e. '101010'), to it's decimal equivalent using first principles" | ||
source: "All of Computer Science" | ||
source_url: "http://www.wolfram-alpha.com" |
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 @@ | ||
You may not use built-in ruby libraries or gems to accomplish the conversion. | ||
|
||
The program should consider strings specifying an invalid binary as the value 0. | ||
|
||
This is how computers work: | ||
|
||
```bash | ||
# "1011001" | ||
1 0 1 1 0 0 1 | ||
2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 | ||
64 + 0 + 16 + 8 + 0 + 0 + 1 = 89 | ||
``` | ||
|
||
If you want to write extra tests and/or check your answers, feel free to use irb: | ||
|
||
```ruby | ||
irb(main):001:0> 0b1011001 | ||
=> 89 | ||
irb(main):002:0> 0b1101 | ||
=> 13 | ||
irb(main):003:0> 0b110110110110101 | ||
=> 28085 | ||
``` |
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,15 @@ | ||
class Binary | ||
|
||
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 * 2**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,44 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
require_relative 'binary' | ||
|
||
class BinaryTest < MiniTest::Unit::TestCase | ||
def test_binary_1_is_decimal_1 | ||
assert_equal 1, Binary.new("1").to_decimal | ||
end | ||
|
||
def test_binary_10_is_decimal_2 | ||
skip | ||
assert_equal 2, Binary.new("10").to_decimal | ||
end | ||
|
||
def test_binary_11_is_decimal_3 | ||
skip | ||
assert_equal 3, Binary.new("11").to_decimal | ||
end | ||
|
||
def test_binary_100_is_decimal_4 | ||
skip | ||
assert_equal 4, Binary.new("100").to_decimal | ||
end | ||
|
||
def test_binary_1001_is_decimal_9 | ||
skip | ||
assert_equal 9, Binary.new("1001").to_decimal | ||
end | ||
|
||
def test_binary_11010_is_decimal_26 | ||
skip | ||
assert_equal 26, Binary.new("11010").to_decimal | ||
end | ||
|
||
def test_binary_10001101000_is_decimal_1128 | ||
skip | ||
assert_equal 1128, Binary.new("10001101000").to_decimal | ||
end | ||
|
||
def test_invalid_binary_is_decimal_0 | ||
skip | ||
assert_equal 0, Binary.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