forked from AdaGold/binary-and-decimal
-
Notifications
You must be signed in to change notification settings - Fork 54
Finished code #43
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
indiakato
wants to merge
1
commit into
Ada-C14:master
Choose a base branch
from
indiakato: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
Finished code #43
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -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 | ||
| #Write a method named binary_to_decimal that receives as input parameter an array of size 8. The array is randomly filled | ||
| # with 0’s and 1’s. Assuming that the array represents an eight bit binary number with the most significant bit at index | ||
| # 0 and the least significant bit at index 7, calculate the decimal value for this binary number using the algorithm you | ||
| # devised in class. Return the decimal value. | ||
| # | ||
| # Note: Do not use Ruby functions. You may use .length method on the array and retrieve values at a particular index in | ||
| # the array. Author using the algorithm you devised in class. | ||
| # | ||
| # You may however write your own versions of these methods | ||
|
|
||
| #test_array = [1, 0, 1, 1, 0, 0, 1, 1] | ||
| # the decimal number of test array should be 179 | ||
|
|
||
| def binary_to_decimal(array) | ||
| bit_1 = array[0] * (2**7) | ||
| bit_2 = array[1] * (2**6) | ||
| bit_3 = array[2] * (2**5) | ||
| bit_4 = array[3] * (2**4) | ||
| bit_5 = array[4] * (2**3) | ||
| bit_6 = array[5] * (2**2) | ||
| bit_7 = array[6] * (2**1) | ||
| bit_8 = array[7] * (2**0) | ||
| result = bit_1 + bit_2 + bit_3 + bit_4 + bit_5 + bit_6 + bit_7 + bit_8 | ||
| return result | ||
| end | ||
|
|
||
| #puts binary_to_decimal(test_array) | ||
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.
This works, but it doesn't scale. Can you see how to make this into a loop?