Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
# 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.
# Calculate and return the decimal value for this binary number using the algorithm you devised in class.

def binary_to_decimal(binary_array)
Comment on lines +2 to 4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Well done

raise NotImplementedError
dec_total = 0
binary_array.each do |number|
dec_total = 2 * dec_total + number
end

return dec_total

end

# Generates random array to pass as argument in binary_to_decimal method
binary_numbers = Array.new(8) {rand (0..1)}

binary_to_decimal(binary_numbers)


# Test Arrays

array = [1, 1, 1] #7

arr2 = [1, 0, 0, 1] #9

arr3 = [1, 0, 1, 1] #11

arr4 = [0, 1, 1, 1] #7


# puts binary_to_decimal(array)

# puts binary_to_decimal(arr2)

# puts binary_to_decimal(arr3) + binary_to_decimal(arr4)
Comment on lines +13 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Generates random array to pass as argument in binary_to_decimal method
binary_numbers = Array.new(8) {rand (0..1)}
binary_to_decimal(binary_numbers)
# Test Arrays
array = [1, 1, 1] #7
arr2 = [1, 0, 0, 1] #9
arr3 = [1, 0, 1, 1] #11
arr4 = [0, 1, 1, 1] #7
# puts binary_to_decimal(array)
# puts binary_to_decimal(arr2)
# puts binary_to_decimal(arr3) + binary_to_decimal(arr4)