From 25d834c824f4d27b75eed9f125af0f8ef83eb528 Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Thu, 22 Aug 2019 12:19:24 -0700 Subject: [PATCH 1/7] Defined a method binary_to_decimal. All tests are passing. --- .gitignore | 1 + lib/binary_to_decimal.rb | 9 +++++++++ test/binary_to_decimal_test.rb | 14 +++++++------- 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d669de9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +notes.txt \ No newline at end of file diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..1a64735 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -5,5 +5,14 @@ # Calculate and return the decimal value for this binary number using # the algorithm you devised in class. def binary_to_decimal(binary_array) + decimal = 0 + exponent = 7 + + binary_array.each do |digit| + decimal += (digit * (2**exponent)) + exponent -= 1 + end + return decimal raise NotImplementedError end + diff --git a/test/binary_to_decimal_test.rb b/test/binary_to_decimal_test.rb index ba17713..5fb557d 100644 --- a/test/binary_to_decimal_test.rb +++ b/test/binary_to_decimal_test.rb @@ -6,28 +6,28 @@ it "From 10011001 to 153" do binary_array = [1, 0, 0, 1, 1, 0, 0, 1] expected_decimal = 153 - + binary_to_decimal(binary_array).must_equal expected_decimal end - + it "From 00001101 to 13" do binary_array = [0, 0, 0, 0, 1, 1, 0, 1] expected_decimal = 13 - + binary_to_decimal(binary_array).must_equal expected_decimal end - + it "From 10000000 to 128" do binary_array = [1, 0, 0, 0, 0, 0, 0, 0] expected_decimal = 128 - + binary_to_decimal(binary_array).must_equal expected_decimal end - + it "From random binary to decimal" do binary_array = Array.new(8) { rand(0..1) } expected_decimal = binary_array.join.to_s.to_i(2) - + binary_to_decimal(binary_array).must_equal expected_decimal end end From 85fd74452320dee94cc5735043ce9cafb090d6ae Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 2 Sep 2019 07:50:34 -0700 Subject: [PATCH 2/7] Added a decimal_to_binary method with a test. Made modifications to method to make the test pass. --- lib/binary_to_decimal.rb | 22 ++++++++++++++++++++++ test/binary_to_decimal_test.rb | 9 +++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 1a64735..55da541 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -16,3 +16,25 @@ def binary_to_decimal(binary_array) raise NotImplementedError end + +def decimal_to_binary(num) + binary_number = (num % 2).to_s + num = num / 2 + + until num == 0 + modulo = (num % 2).to_s + num = num / 2 + binary_number = modulo+binary_number + end + + binary_array = binary_number.split(//) + binary_array.map! {|num| num = num.to_i} + return binary_array + #psuedocode notes: + # take the number mod 2, remainder goes in the array[0] position +end +# Add a `decimal_to_binary` method which converts a decimal number +# received as a parameter into an array of binary digits. +# Then write 3 tests for the method. + + diff --git a/test/binary_to_decimal_test.rb b/test/binary_to_decimal_test.rb index 5fb557d..ee705e6 100644 --- a/test/binary_to_decimal_test.rb +++ b/test/binary_to_decimal_test.rb @@ -1,6 +1,7 @@ require 'minitest/autorun' require 'minitest/reporters' require_relative '../lib/binary_to_decimal' +require 'minitest/pride' describe "binary to decimal" do it "From 10011001 to 153" do @@ -31,3 +32,11 @@ binary_to_decimal(binary_array).must_equal expected_decimal end end + + +describe "decimal to binary" do + it "Converts a decimal to its corresponding binary number" do + num = 13 + expect(decimal_to_binary(num)).must_equal [1, 1, 0, 1] + end +end \ No newline at end of file From 7bc7b2b77795bc24e3a1b5e8e17cdb99013ca489 Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 2 Sep 2019 07:55:59 -0700 Subject: [PATCH 3/7] Reorganized and dry out the decimal_to_binary method. Test still passes. --- lib/binary_to_decimal.rb | 23 ++++------------------- test/binary_to_decimal_test.rb | 4 ++-- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 55da541..30d384c 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -1,9 +1,4 @@ -# 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. + def binary_to_decimal(binary_array) decimal = 0 exponent = 7 @@ -13,28 +8,18 @@ def binary_to_decimal(binary_array) exponent -= 1 end return decimal - raise NotImplementedError end def decimal_to_binary(num) binary_number = (num % 2).to_s - num = num / 2 - + num /= 2 until num == 0 modulo = (num % 2).to_s - num = num / 2 + num /= 2 binary_number = modulo+binary_number end - binary_array = binary_number.split(//) - binary_array.map! {|num| num = num.to_i} - return binary_array - #psuedocode notes: - # take the number mod 2, remainder goes in the array[0] position + return binary_array.map! {|digit| digit = digit.to_i} end -# Add a `decimal_to_binary` method which converts a decimal number -# received as a parameter into an array of binary digits. -# Then write 3 tests for the method. - diff --git a/test/binary_to_decimal_test.rb b/test/binary_to_decimal_test.rb index ee705e6..77799ab 100644 --- a/test/binary_to_decimal_test.rb +++ b/test/binary_to_decimal_test.rb @@ -36,7 +36,7 @@ describe "decimal to binary" do it "Converts a decimal to its corresponding binary number" do - num = 13 - expect(decimal_to_binary(num)).must_equal [1, 1, 0, 1] + number = 13 + expect(decimal_to_binary(number)).must_equal [1, 1, 0, 1] end end \ No newline at end of file From a16af1b3c9dbb51a5354c4bbc3f74017be1ced1c Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 2 Sep 2019 07:59:05 -0700 Subject: [PATCH 4/7] Added a test to convert 0 in decimal to binary, test passes. --- test/binary_to_decimal_test.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/binary_to_decimal_test.rb b/test/binary_to_decimal_test.rb index 77799ab..7a47ae3 100644 --- a/test/binary_to_decimal_test.rb +++ b/test/binary_to_decimal_test.rb @@ -39,4 +39,9 @@ number = 13 expect(decimal_to_binary(number)).must_equal [1, 1, 0, 1] end + + it "Converts 0 to its corresponding binary number" do + number = 0 + expect(decimal_to_binary(number)).must_equal [0] + end end \ No newline at end of file From 5ef6e3cfe726a6ab29f2cc7a5239f394337c4dde Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 2 Sep 2019 08:07:45 -0700 Subject: [PATCH 5/7] not quite there yet with the negative number decimal to binary test and method. Still working on it but saving my work here. --- lib/binary_to_decimal.rb | 5 +++++ test/binary_to_decimal_test.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 30d384c..3d05c92 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -12,6 +12,11 @@ def binary_to_decimal(binary_array) def decimal_to_binary(num) + if num < 0 + binary_number = "1" + end + num = num.abs + binary_number = (num % 2).to_s num /= 2 until num == 0 diff --git a/test/binary_to_decimal_test.rb b/test/binary_to_decimal_test.rb index 7a47ae3..f5f2d03 100644 --- a/test/binary_to_decimal_test.rb +++ b/test/binary_to_decimal_test.rb @@ -44,4 +44,9 @@ number = 0 expect(decimal_to_binary(number)).must_equal [0] end + + it "Hanldes negative values and converts to binary" do + number = -15 + expect(decimal_to_binary(number)).must_equal [1, 0, 1, 1, 1, 1] + end end \ No newline at end of file From bc91297923ce0e6eeda4b7734a3519791ac3eb59 Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 2 Sep 2019 08:13:32 -0700 Subject: [PATCH 6/7] Removed the negative number test (I don't fully understand how to handle negative numbers yet). Added a large number test and modified the method to make it pass. --- lib/binary_to_decimal.rb | 4 ---- test/binary_to_decimal_test.rb | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 3d05c92..120e287 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -12,10 +12,6 @@ def binary_to_decimal(binary_array) def decimal_to_binary(num) - if num < 0 - binary_number = "1" - end - num = num.abs binary_number = (num % 2).to_s num /= 2 diff --git a/test/binary_to_decimal_test.rb b/test/binary_to_decimal_test.rb index f5f2d03..0313699 100644 --- a/test/binary_to_decimal_test.rb +++ b/test/binary_to_decimal_test.rb @@ -45,8 +45,8 @@ expect(decimal_to_binary(number)).must_equal [0] end - it "Hanldes negative values and converts to binary" do - number = -15 - expect(decimal_to_binary(number)).must_equal [1, 0, 1, 1, 1, 1] + it "Handles a large number" do + number = 1_000 + expect(decimal_to_binary(number)).must_equal [1, 1, 1, 1, 1, 0, 1, 0, 0, 0] end end \ No newline at end of file From ec6f4d590406304b93b7a734fcf3b0a254e51cfb Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 2 Sep 2019 08:25:25 -0700 Subject: [PATCH 7/7] Made a few final edits. --- lib/binary_to_decimal.rb | 5 +++-- test/binary_to_decimal_test.rb | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 120e287..250540f 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -2,7 +2,6 @@ def binary_to_decimal(binary_array) decimal = 0 exponent = 7 - binary_array.each do |digit| decimal += (digit * (2**exponent)) exponent -= 1 @@ -12,7 +11,6 @@ def binary_to_decimal(binary_array) def decimal_to_binary(num) - binary_number = (num % 2).to_s num /= 2 until num == 0 @@ -22,5 +20,8 @@ def decimal_to_binary(num) end binary_array = binary_number.split(//) return binary_array.map! {|digit| digit = digit.to_i} + + + end diff --git a/test/binary_to_decimal_test.rb b/test/binary_to_decimal_test.rb index 0313699..ade1ba7 100644 --- a/test/binary_to_decimal_test.rb +++ b/test/binary_to_decimal_test.rb @@ -8,28 +8,28 @@ binary_array = [1, 0, 0, 1, 1, 0, 0, 1] expected_decimal = 153 - binary_to_decimal(binary_array).must_equal expected_decimal + expect(binary_to_decimal(binary_array)).must_equal expected_decimal end it "From 00001101 to 13" do binary_array = [0, 0, 0, 0, 1, 1, 0, 1] expected_decimal = 13 - binary_to_decimal(binary_array).must_equal expected_decimal + expect(binary_to_decimal(binary_array)).must_equal expected_decimal end it "From 10000000 to 128" do binary_array = [1, 0, 0, 0, 0, 0, 0, 0] expected_decimal = 128 - binary_to_decimal(binary_array).must_equal expected_decimal + expect(binary_to_decimal(binary_array)).must_equal expected_decimal end it "From random binary to decimal" do binary_array = Array.new(8) { rand(0..1) } expected_decimal = binary_array.join.to_s.to_i(2) - binary_to_decimal(binary_array).must_equal expected_decimal + expect(binary_to_decimal(binary_array)).must_equal expected_decimal end end