From ef8d22714c1a3f509714579172e4b24db2ac1956 Mon Sep 17 00:00:00 2001 From: Linnea Johnson Date: Tue, 3 Sep 2019 08:19:37 -0700 Subject: [PATCH 1/4] code complete, functions --- lib/binary_to_decimal.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..809bfea 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,18 @@ # 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. +# this_array should equal 227 + def binary_to_decimal(binary_array) - raise NotImplementedError + exponent = (binary_array.length) - 1 + num = 0 + + binary_array.each do |i| + num += (i * 2**exponent) + exponent -= 1 + end + return num end + +this_array = [1, 1, 1, 0, 0, 0, 1, 1] +binary_to_decimal(this_array) \ No newline at end of file From cd641eec14863e6bd3526b811e529e8b7d2fb81c Mon Sep 17 00:00:00 2001 From: Linnea Johnson Date: Tue, 3 Sep 2019 14:07:06 -0700 Subject: [PATCH 2/4] code with array example --- lib/binary_to_decimal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 809bfea..e59d26a 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -11,7 +11,7 @@ def binary_to_decimal(binary_array) num = 0 binary_array.each do |i| - num += (i * 2**exponent) + num += (i * 2 ** exponent) exponent -= 1 end return num From bea0b664bc24e800492b30d4f23cfd1df15fdd21 Mon Sep 17 00:00:00 2001 From: Linnea Johnson Date: Tue, 3 Sep 2019 14:07:36 -0700 Subject: [PATCH 3/4] code compatible with rake file, does not contain array example --- lib/binary_to_decimal.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index e59d26a..f64132f 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -17,5 +17,3 @@ def binary_to_decimal(binary_array) return num end -this_array = [1, 1, 1, 0, 0, 0, 1, 1] -binary_to_decimal(this_array) \ No newline at end of file From 10746754f8e4b54b861647e421c03ea92d4786cd Mon Sep 17 00:00:00 2001 From: Linnea Johnson Date: Tue, 3 Sep 2019 14:08:41 -0700 Subject: [PATCH 4/4] deleted comment that is no longer revelant and changed iterator to be clearer --- lib/binary_to_decimal.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index f64132f..8148de4 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,14 +4,13 @@ # 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. -# this_array should equal 227 def binary_to_decimal(binary_array) exponent = (binary_array.length) - 1 num = 0 - binary_array.each do |i| - num += (i * 2 ** exponent) + binary_array.each do |digit| + num += (digit * 2 ** exponent) exponent -= 1 end return num