From 2d90e60923d0f1c8190e07bdd9437e369691efb4 Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Sat, 2 Mar 2019 08:49:06 -0800 Subject: [PATCH] for shruti --- lib/binary_to_decimal.rb | 10 +++++++++- specs/binary_to_decimal_spec.rb | 7 ++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..5765d34 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -5,5 +5,13 @@ # 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 + sum = 0 + exp = binary_array.length - 1 + binary_array.each do |x| + if x == 1 + sum += 2 ** exp + end + exp -= 1 + end + return sum end diff --git a/specs/binary_to_decimal_spec.rb b/specs/binary_to_decimal_spec.rb index ba17713..0cd87bf 100644 --- a/specs/binary_to_decimal_spec.rb +++ b/specs/binary_to_decimal_spec.rb @@ -1,6 +1,7 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require_relative '../lib/binary_to_decimal' + +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/binary_to_decimal" describe "binary to decimal" do it "From 10011001 to 153" do