From 8f65ed349beb46f78e8c24e7826f875538059fa6 Mon Sep 17 00:00:00 2001 From: Nora Peters Date: Sun, 25 Feb 2018 15:24:28 -0800 Subject: [PATCH] binary_to_decimal method written and passing all tests --- lib/binary_to_decimal.rb | 42 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..3bef3ea 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,46 @@ # 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. + +# my_binary_array = [0, 1, 1, 0, 1, 1, 0, 1] + def binary_to_decimal(binary_array) - raise NotImplementedError + results_array = [] + results_array << binary_array[7] * (2**0) + results_array << binary_array[6] * (2**1) + results_array << binary_array[5] * (2**2) + results_array << binary_array[4] * (2**3) + results_array << binary_array[3] * (2**4) + results_array << binary_array[2] * (2**5) + results_array << binary_array[1] * (2**6) + results_array << binary_array[0] * (2**7) + sum = 0 + results_array.each do |e| + sum += e + end + return sum + # raise NotImplementedError end + +# puts binary_to_decimal(my_binary_array) + +# My pseudocode from class: +# Come up with random 8-bit number made of zeros and ones (ex: 01101101) +# Starting from the right-most (aka least-significant) digit, take that digit, and multiply it by the result of 2^0, then write down the result +# Ex: 1 * (2^0) = 1 +# Take the next number to the left, multiply it by the result of 2^1 then write down the result +# Ex: 0 * (2^1) = 0 +# Take 3rd number from the left, multiply by the result of 2^2, write down result +# 1 * (2^2) = 4 +# Take 4th number from the left, multiply by the result of 2^3, write down result +# 1 * (2^3) = 8 +# Take 5th number from the left, multiply by the result of 2^4, write down result +# 0 * (2^4) = 0 +# Take 6th number from the left, multiply by the result of 2^5, write down result +# 1 * (2^5) = 32 +# Take 7th number from the left, multiply by the result of 2^6, write down result +# 1 * (2^6) = 64 +# Take 8th number from the left, multiply by the result of 2^7, write down result +# 0 * (2^7) = 0 +# Add up the sum of all the results +# 1 + 0 + 4 + 8 + 0 + 32 + 64 + 0 = 109