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
decimal_value = 0
# Note: The input array has to be reversed in order for the
# algorithm I devised in class to work.
binary_array.reverse!
binary_array.each_with_index do |bit, index|
bit_value = bit * (2 ** index)
decimal_value = decimal_value + bit_value
end
return decimal_value
end
6 changes: 6 additions & 0 deletions specs/binary_to_decimal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
require 'minitest/reporters'
require_relative '../lib/binary_to_decimal'

# Edited this to allow use of mintest reporters. Made no other
# edits.

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new


describe "binary to decimal" do
it "From 10011001 to 153" do
binary_array = [1, 0, 0, 1, 1, 0, 0, 1]
Expand Down