Skip to content

Instantly share code, notes, and snippets.

@karmatr0n
Created July 5, 2023 04:55
Show Gist options
  • Save karmatr0n/b411eb3bf43d1732da1602f12714565a to your computer and use it in GitHub Desktop.
Save karmatr0n/b411eb3bf43d1732da1602f12714565a to your computer and use it in GitHub Desktop.
Convert integer to sixty two
require 'test/unit'
def integer_to_sixtytwo(n)
return '0' if n.zero?
sixtytwo_digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
result = ''
while n > 0 do
result << sixtytwo_digits[n % 62]
n /= 62
end
result.reverse
end
class TestIntegerToSixtyTwo < Test::Unit::TestCase
def test_integer_to_sixty_two
assert_equal('0', integer_to_sixtytwo(0))
assert_equal('8hl7c', integer_to_sixtytwo(122343434))
assert_equal('3KXFG2Wm', integer_to_sixtytwo(13232322343434))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment