From 4df17740a0217014277627ce21873189edae6de7 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Thu, 22 Aug 2019 13:46:42 -0700 Subject: [PATCH 1/2] Finished binary_to_decimal method --- lib/binary_to_decimal.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..ef09025 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -5,5 +5,22 @@ # 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 -end + n = binary_array.length - 1 + m = 0 + r = 0 + + if binary_array.class != Array + # Not really sure if we need to implement this expection + # Or what situation this exception applies... + # Assuming and implementing as input validation + raise NotImplementedError + else + until n < 0 + r += binary_array[n] * (2 ** m) + n -= 1 + m += 1 + end + end + + return r +end \ No newline at end of file From e5a128ad4b405ffd6a26197329ee15a2877a97c8 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Sat, 12 Oct 2019 15:17:17 -0700 Subject: [PATCH 2/2] updated naming conviention (feedback) --- Gemfile.lock | 30 ++++++++++++++++++++++++++++++ lib/binary_to_decimal.rb | 23 ++++++++++------------- 2 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..cae865a --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,30 @@ +GEM + remote: http://rubygems.org/ + specs: + ansi (1.5.0) + builder (3.2.3) + minitest (5.10.3) + minitest-reporters (1.3.8) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + minitest-skip (0.0.1) + minitest (~> 5.0) + rake (12.3.0) + ruby-progressbar (1.10.1) + +PLATFORMS + ruby + +DEPENDENCIES + minitest + minitest-reporters + minitest-skip + rake + +RUBY VERSION + ruby 2.5.1p57 + +BUNDLED WITH + 1.17.3 diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index ef09025..9a109c7 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -2,25 +2,22 @@ # 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 +# Calculate and return the decimal value for this binary number using # the algorithm you devised in class. def binary_to_decimal(binary_array) - n = binary_array.length - 1 - m = 0 - r = 0 + bit = binary_array.length - 1 + exponent = 0 + decimal_result = 0 if binary_array.class != Array - # Not really sure if we need to implement this expection - # Or what situation this exception applies... - # Assuming and implementing as input validation - raise NotImplementedError + raise ArgumentError else - until n < 0 - r += binary_array[n] * (2 ** m) - n -= 1 - m += 1 + until bit < 0 + decimal_result += binary_array[bit] * (2 ** exponent) + bit -= 1 + exponent += 1 end end - return r + return decimal_result end \ No newline at end of file