From 87dd2f1cc7db236d1268bcd518ec4499575f93d2 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 21 Aug 2018 23:08:27 -0700 Subject: [PATCH 1/2] Wrote method binary_to_decimal --- 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..5df9d86 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,21 @@ # 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 + index = 0 + power = 7 + decimal = 0 + + binary_array.each do |bit| + decimal += binary_array[index] * (2 ** power) + index += 1 + power -= 1 + end + + return decimal + # raise NotImplementedError end + +loom = [0, 0, 0, 0, 1, 1, 0, 1] +puts binary_to_decimal(loom) From 5c8026dfaa3b91ffd25e3cac950b2f0e62875424 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 21 Aug 2018 23:13:02 -0700 Subject: [PATCH 2/2] Added binary_to_decimal method --- lib/binary_to_decimal.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 5df9d86..b845a6e 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -19,6 +19,3 @@ def binary_to_decimal(binary_array) return decimal # raise NotImplementedError end - -loom = [0, 0, 0, 0, 1, 1, 0, 1] -puts binary_to_decimal(loom)