From 84792ab250a10ca53a4d53f91bf80d92781eedfd Mon Sep 17 00:00:00 2001 From: SelamawitA Date: Sat, 24 Feb 2018 16:09:41 -0800 Subject: [PATCH 1/2] added method to convert binary number to an integer value --- lib/binary_to_decimal.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..0cbb9a5 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,18 @@ # The least significant bit is at index 7. # 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 + total = 0 + index = 0 + size = binary_array.length + bit_value = size - 1 + + size.times do + element = binary_array[index] + total += element * (2**bit_value) + bit_value -= 1 + index+=1 + end + return total end From cf8c91f124417775b3ac9fde2e3029d3d1273efa Mon Sep 17 00:00:00 2001 From: SelamawitA Date: Sat, 24 Feb 2018 16:11:57 -0800 Subject: [PATCH 2/2] updated return type to float class --- lib/binary_to_decimal.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 0cbb9a5..126139f 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -17,5 +17,6 @@ def binary_to_decimal(binary_array) bit_value -= 1 index+=1 end - return total + #convert to float (decimal) type. + return total.to_f end