From 86ca4dab5ae7570e614a1ce64c46c2384c2e5c32 Mon Sep 17 00:00:00 2001 From: Abiaina Date: Sun, 25 Feb 2018 22:38:14 -0800 Subject: [PATCH 1/2] failed then passed all tests, removed raise argument error --- lib/binary_to_decimal.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..b9cd547 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -5,5 +5,15 @@ # 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 + exponent = 7 + index = 0 + 8.times do + if binary_array[index] == 1 + sum += (2 ** exponent) + end + exponent -= 1 + index += 1 + end + return sum end From f671bd1bc2bb948c3cb3d77cb33fac254b3e6366 Mon Sep 17 00:00:00 2001 From: Abiaina Date: Wed, 28 Feb 2018 19:13:29 -0800 Subject: [PATCH 2/2] Update binary_to_decimal.rb --- lib/binary_to_decimal.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index b9cd547..3eef7f4 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -17,3 +17,15 @@ def binary_to_decimal(binary_array) end return sum end + +#improved verision: +def binary_to_decimal(binary_array) + sum = 0 + 8.times do |index| + if binary_array[index] == 1 + sum += (2 ** (7 - index)) + end + end + return sum +end +