Skip to content

Instantly share code, notes, and snippets.

@pmatsinopoulos
Created June 27, 2023 05:23
Show Gist options
  • Save pmatsinopoulos/a4d03f3a151ce3466c5e77fa89e9623a to your computer and use it in GitHub Desktop.
Save pmatsinopoulos/a4d03f3a151ce3466c5e77fa89e9623a to your computer and use it in GitHub Desktop.
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