Created
June 27, 2023 05:23
-
-
Save pmatsinopoulos/a4d03f3a151ce3466c5e77fa89e9623a to your computer and use it in GitHub Desktop.
This file contains 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
class IntegerToBinary | |
def initialize(integer) | |
@integer = integer | |
end | |
def binary | |
return '0' if integer.zero? | |
stack = [] | |
quotient = integer | |
while quotient > 0 | |
remainder = quotient % 2 | |
stack.push remainder | |
quotient = quotient / 2 | |
end | |
result = '' | |
until stack.empty? | |
result = "#{result}#{stack.pop}" | |
end | |
result | |
end | |
private | |
attr_reader :integer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment