From 5d302429fa0d8607b9d2129526fea60d09709a12 Mon Sep 17 00:00:00 2001 From: Leah Scott-Zechlin Date: Wed, 16 Sep 2020 22:14:46 +0200 Subject: [PATCH 1/3] cleaned up --- lib/binary_to_decimal.rb | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..38a3219 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -1,9 +1,33 @@ # A method named `binary_to_decimal` that receives as input an array of size 8. -# The array is randomly filled with 0’s and 1’s. -# The most significant bit is at index 0. -# 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. +# 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 + binary_array.each do |num| + total = 2 * total + num + end + + return total + end + + +binary_numbers = Array.new(8) {rand (0..1)} + +binary_to_decimal(binary_numbers) + + +# Test Arrays +array = [1, 1, 1] #7 + +arr2 = [1, 0, 0, 1] #9 + +arr3 = [1, 0, 1, 1] #11 + +arr4 = [0, 1, 1, 1] #7 + +# puts binary_to_decimal(array) + +# puts binary_to_decimal(arr2) + +# puts binary_to_decimal(arr3) + binary_to_decimal(arr4) \ No newline at end of file From dea85fd23c5d27f517b7d0dd01c4949dd6185ff9 Mon Sep 17 00:00:00 2001 From: Leah Scott-Zechlin Date: Wed, 16 Sep 2020 23:23:31 +0200 Subject: [PATCH 2/3] trying again to upload method --- lib/binary_to_decimal.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 38a3219..be2d5e8 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -2,22 +2,24 @@ # Calculate and return the decimal value for this binary number using the algorithm you devised in class. def binary_to_decimal(binary_array) - total = 0 - binary_array.each do |num| - total = 2 * total + num + dec_total = 0 + binary_array.each do |number| + dec_total = 2 * dec_total + number end - return total + return dec_total end - +# Generates random array to pass as argument in binary_to_decimal method binary_numbers = Array.new(8) {rand (0..1)} + binary_to_decimal(binary_numbers) # Test Arrays + array = [1, 1, 1] #7 arr2 = [1, 0, 0, 1] #9 @@ -26,6 +28,7 @@ def binary_to_decimal(binary_array) arr4 = [0, 1, 1, 1] #7 + # puts binary_to_decimal(array) # puts binary_to_decimal(arr2) From 5d812411a3b04d40eaf50cd1de1d7302feb5b3a7 Mon Sep 17 00:00:00 2001 From: Leah Scott-Zechlin Date: Wed, 16 Sep 2020 23:33:29 +0200 Subject: [PATCH 3/3] trying again --- lib/binary_to_decimal.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index be2d5e8..4bcdc21 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -14,7 +14,6 @@ def binary_to_decimal(binary_array) # Generates random array to pass as argument in binary_to_decimal method binary_numbers = Array.new(8) {rand (0..1)} - binary_to_decimal(binary_numbers)