Skip to content

Instantly share code, notes, and snippets.

@anhkind
Last active December 21, 2015 05:19
Show Gist options
  • Save anhkind/6256444 to your computer and use it in GitHub Desktop.
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/
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