Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@
# Calculate and return the decimal value for this binary number using
# the algorithm you devised in class.
def binary_to_decimal(binary_array)
raise NotImplementedError
sum = 0
exp = binary_array.length - 1
binary_array.each do |x|
if x == 1
sum += 2 ** exp
end
exp -= 1
end
return sum
end
7 changes: 4 additions & 3 deletions specs/binary_to_decimal_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/binary_to_decimal'

require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/binary_to_decimal"

describe "binary to decimal" do
it "From 10011001 to 153" do
Expand Down