-
Notifications
You must be signed in to change notification settings - Fork 54
completed assignment #32
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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' | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
|
|
||
| def decimal_to_binary(decimal) | ||
|
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. 👍 Nice work You can use |
||
| bin = Array.new(8) | ||
| i = 0 | ||
|
|
||
| while i < bin.length | ||
|
|
||
| if(decimal - 2 ** (bin.length - 1 - i) < 0) | ||
| bin[i] = 0 | ||
| else | ||
| bin[i] = 1 | ||
| decimal -= 2 ** (bin.length - 1 - i) | ||
| end | ||
| i += 1 | ||
| end | ||
|
|
||
|
|
||
| return bin | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| require 'minitest/autorun' | ||
| require 'minitest/reporters' | ||
| require_relative '../lib/decimal_to_binary' | ||
|
|
||
| Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
|
||
| describe "decimal to binary" do | ||
| it "From 153 to 10011001" do | ||
|
|
||
| decimal = 153 | ||
| expected_binary_array = [1, 0, 0, 1, 1, 0, 0, 1] | ||
|
|
||
| expect(decimal_to_binary(decimal)).must_equal expected_binary_array | ||
| end | ||
|
|
||
| it "From 13 to 00001101" do | ||
|
|
||
| decimal = 13 | ||
| expected_binary_array = [0, 0, 0, 0, 1, 1, 0, 1] | ||
|
|
||
| expect(decimal_to_binary(decimal)).must_equal expected_binary_array | ||
| end | ||
|
|
||
| it "From 128 to 10000000" do | ||
|
|
||
| expected_binary_array = [1, 0, 0, 0, 0, 0, 0, 0] | ||
| decimal = 128 | ||
|
|
||
| expect(decimal_to_binary(decimal)).must_equal expected_binary_array | ||
| end | ||
|
|
||
|
|
||
| end |
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.
👍