Skip to content

Commit

Permalink
Add ruby binary assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Jun 11, 2013
1 parent 244cab2 commit 5683133
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions assignments/ruby/binary/data.yml
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"
23 changes: 23 additions & 0 deletions assignments/ruby/binary/details.md
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
```
15 changes: 15 additions & 0 deletions assignments/ruby/binary/example.rb
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
44 changes: 44 additions & 0 deletions assignments/ruby/binary/test.rb
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
2 changes: 1 addition & 1 deletion lib/exercism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.current_curriculum
bob word-count anagram beer-song nucleotide-count
rna-transcription point-mutations phone-number
grade-school leap etl space-age grains gigasecond
triangle scrabble-score roman-numerals
triangle scrabble-score roman-numerals binary
)
@curriculum.add(rb, rb_slugs)

Expand Down

0 comments on commit 5683133

Please sign in to comment.