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 439e8c6..9a109c7 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -2,8 +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) - raise NotImplementedError -end + bit = binary_array.length - 1 + exponent = 0 + decimal_result = 0 + + if binary_array.class != Array + raise ArgumentError + else + until bit < 0 + decimal_result += binary_array[bit] * (2 ** exponent) + bit -= 1 + exponent += 1 + end + end + + return decimal_result +end \ No newline at end of file