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
37 changes: 36 additions & 1 deletion lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,41 @@
# 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 = Array.new(8) { rand(0..1)}

# this method will take in the array of 8 bits randomly created
# and output the decimal value.
# def binary_to_decimal(binary_array)
# # raise NotImplementedError
# i = 0
# expected_decimal = []
# binary_array.reverse.each do |index|
# expected_decimal << index * 2 ** i
# i += 1
# end
# return expected_decimal.inject(:+)
# end


# a little unsure how to update the method to not use Ruby functions.
# Shruti mentioned that times loop is okay. So I updated the above
# method to be a times loop insted.
def binary_to_decimal(binary_array)
raise NotImplementedError
# raise NotImplementedError
i = 0
expected_decimal = []
new_binary_array = Array.new(binary_array.reverse)

8.times do |index|
expected_decimal << new_binary_array[index] * 2 ** i
i += 1
end
return expected_decimal.inject(:+)
end


# # UI to test the above code
# expected_decimal = binary_to_decimal([1, 0, 0, 0, 0, 0, 0, 0])
# puts expected_decimal