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
27 changes: 26 additions & 1 deletion lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@
# 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 to Decimal
def binary_to_decimal(binary_array)
Comment on lines +7 to 9
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 Nice work!

raise NotImplementedError
array_length = binary_array.length
num = 0
index = 0
while array_length > 0
num += binary_array[index].to_i * 2 ** (array_length - 1)
array_length -= 1
index += 1
end
return num
end

### Decimal to Binary
# def decimal_to_binary(number_base_ten)
# quotient = number_base_ten.divmod(2)[0]
# modulus = Array(number_base_ten.divmod(2)[1])
# until quotient == 0
# modulus.unshift(quotient.divmod(2)[1])
# quotient = quotient.divmod(2)[0]
# end
# return "For #{ number_base_ten }, its binary number is \"#{ modulus.join }\""
# end

# puts decimal_to_binary(255)
# puts decimal_to_binary(456)
# puts decimal_to_binary(9)