Created
July 5, 2023 04:55
-
-
Save karmatr0n/b411eb3bf43d1732da1602f12714565a to your computer and use it in GitHub Desktop.
Convert integer to sixty two
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
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