From d4f0b95287f93c5d5c7da3b3fb31c52833dd49a2 Mon Sep 17 00:00:00 2001 From: Anibel America Date: Sun, 19 Aug 2018 20:42:10 -0700 Subject: [PATCH 1/2] Implemented binary to decimal algorithm --- lib/binary_to_decimal.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..cfae38b 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -5,5 +5,20 @@ # 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 + # raise NotImplementedError + # # with each_with_index + # sum = 0 + # binary_array.each_with_index do |digit, index| + # sum += digit * ( 2 ** ( 7 - index)) + # end + # return sum + + # each_with_index + sum = 0 + index = 0 + binary_array.each do |digit| + sum += digit * ( 2 ** ( 7 - index)) + index += 1 + end + return sum end From f87ba004871fd0f6517bf4345e098ae38142497e Mon Sep 17 00:00:00 2001 From: Anibel America Date: Wed, 22 Aug 2018 08:42:43 -0700 Subject: [PATCH 2/2] removed extra comments --- lib/binary_to_decimal.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index cfae38b..9e8ef50 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -5,15 +5,8 @@ # 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 - # # with each_with_index - # sum = 0 - # binary_array.each_with_index do |digit, index| - # sum += digit * ( 2 ** ( 7 - index)) - # end - # return sum - # each_with_index + # without each_with_index sum = 0 index = 0 binary_array.each do |digit|