From 4e0942244134b1b01809adafde3ec5fa7e00bce8 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Sun, 25 Feb 2018 11:47:38 -0800 Subject: [PATCH] Binary to decimal method implemented and tests passed --- .DS_Store | Bin 0 -> 6148 bytes lib/binary_to_decimal.rb | 14 +++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..10f4d43063cf2afdea345ed0e8725c067b6d3ef7 GIT binary patch literal 6148 zcmeH~O=`nH427SXECStlndNM9fZkvT$q90S5(*^{5-7CmIeMRdHg&o#raXc4Mj8v- z-@;=7u>I%T3orrL&|R_fFf(Jm!W9>szfK>w>;3l5idTWBh?%i6VYXk}5)lvq5fA|p z5P<~|$Wt7f=LJ2J9z_I1U>OAb`_SmFy>z6;r-LCz0P33MFs@^kpf)d1d+A7Jg=RH9 zShZS=AzqJmYOCvd=}66XSPdUmcQ&75XqN4;#)M`)L_q{ZU`Ak-`Q+#Sk^bBKKWkAc z0wVCw2-x~?I_&vUb+$gdp4VTi>gz$L#^nq@egc^IQM{#xaliS3+Dk_&D>VHG1O^2W H_)`MkvH%f) literal 0 HcmV?d00001 diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..e1b22ae 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,18 @@ # 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 = [] +8.times do + binary_array << rand(0..1) +end + def binary_to_decimal(binary_array) - raise NotImplementedError + # raise NotImplementedError + n = binary_array.length - 1 + decimal_number = 0 + binary_array.length.times do |i| + decimal_number += binary_array[i] * 2 ** n + n -= 1 + end + return decimal_number end