Last active
December 21, 2015 05:19
-
-
Save anhkind/6256444 to your computer and use it in GitHub Desktop.
Ruby script to solve the puzzle in https://www.spotify.com/au/jobs/tech/reversed-binary/
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 BinaryReverse | |
def reverse(n) | |
res = 0 | |
while n > 1 do | |
res = res * 2 + n % 2 | |
n = n / 2 | |
end | |
res * 2 + 1 | |
end | |
end | |
require 'test/unit' | |
class TestReverse < Test::Unit::TestCase | |
def test_reserve_1 | |
obj = BinaryReverse.new | |
output = obj.reverse 13 | |
assert_equal 11, output | |
end | |
def test_reserve_2 | |
obj = BinaryReverse.new | |
output = obj.reverse 47 | |
assert_equal 61, output | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment