Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
# Calculate and return the decimal value for this binary number using
# the algorithm you devised in class.
def binary_to_decimal(binary_array)
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
decimal_sum = 0
binary_array.each_with_index do |bit, index|
decimal = bit * 2**(binary_array.length - index - 1)
decimal_sum = decimal_sum + decimal
end
return decimal_sum
end

10 changes: 10 additions & 0 deletions lib/decimal_to_binary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# converts a decimal number received as a parameter into an array of binary digits
def decimal_to_binary(decimal)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

binary_array = []
until decimal == 0
binary_array << decimal % 2
decimal = decimal / 2
end
return binary_array.reverse
end