From 9c996aea0a7e9edeb81ca363d954046da8983420 Mon Sep 17 00:00:00 2001 From: Grace Shea Date: Mon, 25 Feb 2019 07:03:16 -0800 Subject: [PATCH 1/2] first commit --- .DS_Store | Bin 0 -> 6148 bytes lib/binary_to_decimal.rb | 7 +++++++ 2 files changed, 7 insertions(+) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..575f71e606ab245d651c7c0a944942818dce0f72 GIT binary patch literal 6148 zcmeHK!EVz)5S>lZvJOJzfK<5og2W+0PJ~ntLNXye^niqL1qVQ_T^nlQdZXAOf)L~j zKLKCBukZtW4t=w`R3wF4g+#R@&A!>$nX&e*<@FGe7|qfFQICiul(FSu_=E5|>ltZT z&n8fC4Rx3lD#47)Ir$$L;InH}K{+M6#mf2J$I7jrTJ%P2R5Gt7_LRRTlOnI`Aow9# z?as#LB`LeId!_p-I#Tnfj_X-9il;Al_9U*45^L`%Rh-&!@+{Bh`@L(AR9VM)nN4+a zo@NMn`8+REH6N*2nO3?^WEQ0Kq_^L@u~-a)pzjZZ?PcFz4DQ?=^!@F7!{ySGTQ>&} zb`OrhodD;DWH^ltT zl1EQzhaS?H9s_UFF{P^+Vrwm9gDRS!{vlOsk)PKhF?He?a11yG&V>Q{HI$dnrJ5_^ z7;p?+BnJ5V;6oWhi;Y43>A;{{0AL4hEwJ^^KhS3fFtpehga;x_D$t|~+hPdYI|!4G z_0YyQ22DB%{bba!PZqXA5w`UZz78i58g!#$z%j7OK-)}veE;wKz5icLax=$(W8gwD zAUfk{Ji?aj-FjwoeAoKW11Jl}H3m;pVCbtDv3wO@hiZY}X9E~oYz)E!aX$i@1~)hc H{wV_|`9FS? literal 0 HcmV?d00001 diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..29817ff 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -6,4 +6,11 @@ # the algorithm you devised in class. def binary_to_decimal(binary_array) raise NotImplementedError + power = binary_array.length - 1 + decimal = 0 + binary_array.each do |b| + decimal += b * (2 ** power) + power -= 1 end +return decimal +end \ No newline at end of file From a103afdda55c9350590d88fa3cd3c24437860324 Mon Sep 17 00:00:00 2001 From: Grace Shea Date: Thu, 28 Feb 2019 05:54:54 -0800 Subject: [PATCH 2/2] method passes all tests --- .DS_Store | Bin 6148 -> 6148 bytes lib/binary_to_decimal.rb | 22 ++++++++-------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/.DS_Store b/.DS_Store index 575f71e606ab245d651c7c0a944942818dce0f72..b14e30f3ce47861a234d2c44a2f7f8ae6a62a0e9 100644 GIT binary patch delta 16 XcmZoMXffDunu*!a(sJ`TCNoh0F`ETi delta 16 XcmZoMXffDunu*!Sz+m$^CNoh0F)js3 diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 29817ff..e3d7087 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -1,16 +1,10 @@ -# A method named `binary_to_decimal` that receives as input an array of size 8. -# 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 -# the algorithm you devised in class. + def binary_to_decimal(binary_array) - raise NotImplementedError - power = binary_array.length - 1 - decimal = 0 - binary_array.each do |b| - decimal += b * (2 ** power) - power -= 1 + total = 0 + exponent = binary_array.length - 1 + binary_array.length.times do |index| + total += binary_array[index] * (2 ** exponent) + exponent -= 1 + end + return total end -return decimal -end \ No newline at end of file