From e4fae910a4691ec1d42bd103a58aba0f4035802d Mon Sep 17 00:00:00 2001 From: dnsrocha Date: Mon, 14 Sep 2020 20:13:06 -0700 Subject: [PATCH 1/2] task done --- lib/binary_to_decimal.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..703c561 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,19 @@ # 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 +binary = Array.new(8) {rand(0..1)} + +def binary_to_decimal(binary) + puts binary.join + "\n" + a = binary.length + decimal = 0 + + for i in 0...a do + reverse_index = 7 - i + num = binary[reverse_index].to_i * 2**i + decimal +=num + end + puts decimal end + +binary_to_decimal(binary) From d011648e4945a719ff2ad59918ed43f0e0ac1453 Mon Sep 17 00:00:00 2001 From: dnsrocha Date: Tue, 15 Sep 2020 15:28:56 -0700 Subject: [PATCH 2/2] binary_to_decimal - all done --- Rakefile | 2 +- lib/binary_to_decimal.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Rakefile b/Rakefile index 0c2d13f..fb007c6 100644 --- a/Rakefile +++ b/Rakefile @@ -6,4 +6,4 @@ Rake::TestTask.new do |t| t.test_files = FileList['test/*_test.rb'] end -task default: :test +task default: :test \ No newline at end of file diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 703c561..8a0fa1e 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,7 @@ # 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. + binary = Array.new(8) {rand(0..1)} def binary_to_decimal(binary) @@ -16,7 +17,7 @@ def binary_to_decimal(binary) num = binary[reverse_index].to_i * 2**i decimal +=num end - puts decimal + return decimal end binary_to_decimal(binary)