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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'http://rubygems.org'

ruby '2.6.6'
ruby '2.6.5'

gem 'rake'

Expand Down
74 changes: 73 additions & 1 deletion lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,77 @@
# 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 = 0
index = 0

until index == binary_array.length
power = (binary_array.length - 1) - index

decimal += binary_array[index] * ( 2 ** (power))

index += 1
end

return decimal
end


# Method named decimal_to_binary receives decimal number as input
# Calculates and returns the binary value as a number for the decimal number
def decimal_to_binary(decimal)
Comment on lines +23 to +25
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.

binary = 0
power = 0

if decimal.class == Integer
until decimal == 0
binary_digit = decimal % 2
binary += binary_digit * (10 ** power)

decimal /= 2
power += 1
end

return binary
elsif decimal.class == Float
decimal_before_dot = decimal - (decimal % 1)
binary_before_dot = 0


until decimal_before_dot == 0
binary_digit = decimal_before_dot % 2
binary_before_dot += binary_digit * (10 ** power)


decimal_before_dot = (decimal_before_dot / 2)
decimal_before_dot = decimal_before_dot - (decimal_before_dot % 1)

power += 1
end


decimal_after_dot = (decimal % 1)
binary_after_dot_string = "0."

until decimal_after_dot == 0
binary_digit = (decimal_after_dot * 2) - ((decimal_after_dot * 2) % 1)

if binary_digit == 0
binary_after_dot_string << "0"
elsif binary_digit == 1
binary_after_dot_string << "1"
end

decimal_after_dot = (decimal_after_dot * 2) % 1
end

binary_after_dot = binary_after_dot_string.to_f


binary = binary_before_dot + binary_after_dot

return binary
else # if decimal input is not an integer or float
raise ArgumentError, "Not a number."
end

end
30 changes: 30 additions & 0 deletions test/binary_to_decimal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,33 @@
expect(binary_to_decimal(binary_array)).must_equal expected_decimal
end
end


describe "decimal to binary" do
it "From 0 to 0" do
decimal = 0
expected_binary = 0

expect(decimal_to_binary(decimal)).must_equal expected_binary
end

it "From 87 to 1010111" do
decimal = 87
expected_binary = 1010111

expect(decimal_to_binary(decimal)).must_equal expected_binary
end

it "From 3.703125 to 11.101101" do
decimal = 3.703125
expected_binary = 11.101101

expect(decimal_to_binary(decimal)).must_equal expected_binary
end

it "Raises an ArgumentError if decimal is not a number" do
decimal = 'a'

expect{ decimal_to_binary(decimal) }.must_raise ArgumentError
end
end