From c062d566c34a780fb03a17624c07b67bb21b591d Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Wed, 22 Aug 2018 08:44:25 -0700 Subject: [PATCH 1/2] added code block to change a binary number to a decimal --- lib/binary_to_decimal.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..cb64baf 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -4,6 +4,11 @@ # 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 +# def binary_to_decimal(binary_array) +# raise NotImplementedError +# end + +def binary_to_decimal(bin_array) + decimal = (bin_array[7] * 1) + (bin_array[6] * 2) + (bin_array[5] * 4) + (bin_array[4] * 8) + (bin_array[3] * 16) + (bin_array[2] * 32) + (bin_array[1] * 64) + (bin_array[0] * 128) +return decimal end From 89caae46d02665f097e347a7146fc8943e434a32 Mon Sep 17 00:00:00 2001 From: Melissa O Date: Mon, 27 Aug 2018 13:30:45 -0700 Subject: [PATCH 2/2] Create Binarytodecimal.rb --- Binarytodecimal.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Binarytodecimal.rb diff --git a/Binarytodecimal.rb b/Binarytodecimal.rb new file mode 100644 index 0000000..507b6d6 --- /dev/null +++ b/Binarytodecimal.rb @@ -0,0 +1,25 @@ +# write a method that finds the decimal value of a binary number + +def binary_to_decimal(bin_array) + decimal = (bin_array[7] * 1) + (bin_array[6] * 2) + (bin_array[5] * 4) + (bin_array[4] * 8) + (bin_array[3] * 16) + (bin_array[2] * 32) + (bin_array[1] * 64) + (bin_array[0] * 128) +return decimal +end + +puts "#{binary_to_decimal([1, 0, 1, 0, 1, 0, 1, 1])}" + + +# Author a method named array_equals that accepts two integer arrays as input parameters. The method should return true if the arrays contain the same count of elements, the element values in the array are the same and they are in the same exact order. Otherwise, the method should return false. +# Note: Do not use Array class methods for comparing the whole array at once. You may use array indexing to retrieve one element at a time, you may compare individual elements with each other and you may retrieve the length of an array. Do not use any other Ruby provided functions. + +def array_equals(array1, array2) + array1.length.times do + if array1.length == array2.length && array1[0] == array2[0] && array1[1] == array2[1] && array1[2] == array2[2] && array1[3] == array2[3] && array1[4] == array2[4] && array1[5] == array2[5] && array1[6] == array2[6] && array1[7] == array2[7] + return true + else + return false + end + end +end + + +puts "#{array_equals([1, 2, 3, 5], [1, 2, 3, 4, 5])}"