forked from AdaGold/binary-and-decimal
-
Notifications
You must be signed in to change notification settings - Fork 48
Leaves - Natalie Tapias #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NatalieTapias
wants to merge
7
commits into
Ada-C12:master
Choose a base branch
from
NatalieTapias:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
25d834c
Defined a method binary_to_decimal. All tests are passing.
NatalieTapias 85fd744
Added a decimal_to_binary method with a test. Made modifications to m…
NatalieTapias 7bc7b2b
Reorganized and dry out the decimal_to_binary method. Test still passes.
NatalieTapias a16af1b
Added a test to convert 0 in decimal to binary, test passes.
NatalieTapias 5ef6e3c
not quite there yet with the negative number decimal to binary test a…
NatalieTapias bc91297
Removed the negative number test (I don't fully understand how to han…
NatalieTapias ec6f4d5
Made a few final edits.
NatalieTapias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| notes.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,27 @@ | ||
| # 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) | ||
| raise NotImplementedError | ||
| decimal = 0 | ||
| exponent = 7 | ||
| binary_array.each do |digit| | ||
| decimal += (digit * (2**exponent)) | ||
| exponent -= 1 | ||
| end | ||
| return decimal | ||
| end | ||
|
|
||
|
|
||
| def decimal_to_binary(num) | ||
| binary_number = (num % 2).to_s | ||
| num /= 2 | ||
| until num == 0 | ||
| modulo = (num % 2).to_s | ||
| num /= 2 | ||
| binary_number = modulo+binary_number | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clever use of string concatenation to reverse things. |
||
| end | ||
| binary_array = binary_number.split(//) | ||
| return binary_array.map! {|digit| digit = digit.to_i} | ||
|
|
||
|
|
||
|
|
||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,52 @@ | ||
| 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 | ||
| 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 | ||
|
|
||
|
|
||
| describe "decimal to binary" do | ||
| it "Converts a decimal to its corresponding binary number" do | ||
| 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 | ||
|
|
||
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: I suggest making 7 a constant or
binary_array.length - 1to avoid hardcoding in specific numbers.