-
-
Save fluency03/a3f6e762cd6b13fe9de99d7277af643c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env python | |
| # | |
| # Converts any integer into a base [BASE] number. I have chosen 62 | |
| # as it is meant to represent the integers using all the alphanumeric | |
| # characters, [no special characters] = {0..9}, {A..Z}, {a..z} | |
| # | |
| # I plan on using this to shorten the representation of possibly long ids, | |
| # a la url shortenters | |
| # | |
| # saturate() takes the base 62 key, as a string, and turns it back into an integer | |
| # dehydrate() takes an integer and turns it into the base 62 string | |
| # | |
| import math | |
| import sys | |
| BASE = 62 | |
| UPPERCASE_OFFSET = 55 | |
| LOWERCASE_OFFSET = 61 | |
| DIGIT_OFFSET = 48 | |
| def true_ord(char): | |
| """ | |
| Turns a digit [char] in character representation | |
| from the number system with base [BASE] into an integer. | |
| """ | |
| if char.isdigit(): | |
| return ord(char) - DIGIT_OFFSET | |
| elif char >= 'A' and char <= 'Z': | |
| return ord(char) - UPPERCASE_OFFSET | |
| elif char >= 'a' and char <= 'z': | |
| return ord(char) - LOWERCASE_OFFSET | |
| else: | |
| raise ValueError("%s is not a valid character" % char) | |
| def true_chr(integer): | |
| """ | |
| Turns an integer [integer] into digit in base [BASE] | |
| as a character representation. | |
| """ | |
| if integer < 10: | |
| return chr(integer + DIGIT_OFFSET) | |
| elif integer >= 10 and integer <= 35: | |
| return chr(integer + UPPERCASE_OFFSET) | |
| elif integer >= 36 and integer < 62: | |
| return chr(integer + LOWERCASE_OFFSET) | |
| else: | |
| raise ValueError("%d is not a valid integer in the range of base %d" % (integer, BASE)) | |
| def saturate(key): | |
| """ | |
| Turn the base [BASE] number [key] into an integer | |
| """ | |
| idx = 0 | |
| int_sum = 0 | |
| reversed_key = key[::-1] | |
| for char in reversed_key: | |
| int_sum += true_ord(char) * int(math.pow(BASE, idx)) | |
| idx += 1 | |
| return int_sum | |
| def dehydrate(integer): | |
| """ | |
| Turn an integer [integer] into a base [BASE] number | |
| in string representation | |
| """ | |
| if integer == 0: | |
| return '0' | |
| digit = integer | |
| string = "" | |
| while digit > 0: | |
| remainder = digit % BASE | |
| string = true_chr(remainder) + string | |
| digit /= BASE | |
| return string | |
| if __name__ == '__main__': | |
| #!/usr/bin/env python | |
| # | |
| # Converts any integer into a base [BASE] number. I have chosen 62 | |
| # as it is meant to represent the integers using all the alphanumeric | |
| # characters, [no special characters] = {0..9}, {A..Z}, {a..z} | |
| # | |
| # I plan on using this to shorten the representation of possibly long ids, | |
| # a la url shortenters | |
| # | |
| # saturate() takes the base 62 key, as a string, and turns it back into an integer | |
| # dehydrate() takes an integer and turns it into the base 62 string | |
| # | |
| import math | |
| import sys | |
| BASE = 62 | |
| UPPERCASE_OFFSET = 55 | |
| LOWERCASE_OFFSET = 61 | |
| DIGIT_OFFSET = 48 | |
| def true_ord(char): | |
| """ | |
| Turns a digit [char] in character representation | |
| from the number system with base [BASE] into an integer. | |
| """ | |
| if char.isdigit(): | |
| return ord(char) - DIGIT_OFFSET | |
| elif char >= 'A' and char <= 'Z': | |
| return ord(char) - UPPERCASE_OFFSET | |
| elif char >= 'a' and char <= 'z': | |
| return ord(char) - LOWERCASE_OFFSET | |
| else: | |
| raise ValueError("%s is not a valid character" % char) | |
| def true_chr(integer): | |
| """ | |
| Turns an integer [integer] into digit in base [BASE] | |
| as a character representation. | |
| """ | |
| if integer < 10: | |
| return chr(integer + DIGIT_OFFSET) | |
| elif integer >= 10 and integer <= 35: | |
| return chr(integer + UPPERCASE_OFFSET) | |
| elif integer >= 36 and integer < 62: | |
| return chr(integer + LOWERCASE_OFFSET) | |
| else: | |
| raise ValueError("%d is not a valid integer in the range of base %d" % (integer, BASE)) | |
| def saturate(key): | |
| """ | |
| Turn the base [BASE] number [key] into an integer | |
| """ | |
| idx = 0 | |
| int_sum = 0 | |
| reversed_key = key[::-1] | |
| for char in reversed_key: | |
| int_sum += true_ord(char) * int(math.pow(BASE, idx)) | |
| idx += 1 | |
| return int_sum | |
| def dehydrate(integer): | |
| """ | |
| Turn an integer [integer] into a base [BASE] number | |
| in string representation | |
| """ | |
| # the loop below doesn't work for 0 so let's just check here | |
| if integer == 0: | |
| return '0' | |
| digit = integer | |
| string = "" | |
| while digit > 0: | |
| remainder = digit % BASE | |
| string = true_chr(remainder) + string | |
| digit /= BASE | |
| return string | |
| if __name__ == '__main__': | |
| # slightly amusing tests | |
| # could use more tests | |
| if sys.argv[1] == '-tests': | |
| passed_tests = True | |
| for i in xrange(0, 1000): | |
| passed_tests &= i == saturate(dehydrate(i)) | |
| print passed_tests | |
| user_input = sys.argv[2] | |
| if sys.argv[1] == '-s': | |
| try: | |
| result = saturate(user_input) | |
| print result | |
| except ValueError as e: | |
| print e | |
| elif sys.argv[1] == '-d': | |
| try: | |
| result = dehydrate(int(user_input)) | |
| print result | |
| except ValueError as e: | |
| print e | |
| else: | |
| print "I don't understand option %s" % sys.argv[1] | |
| if sys.argv[1] == '-tests': | |
| passed_tests = True | |
| for i in xrange(0, 1000): | |
| passed_tests &= i == saturate(dehydrate(i)) | |
| print passed_tests | |
| user_input = sys.argv[2] | |
| if sys.argv[1] == '-s': | |
| try: | |
| result = saturate(user_input) | |
| print result | |
| except ValueError as e: | |
| print e | |
| elif sys.argv[1] == '-d': | |
| try: | |
| result = dehydrate(int(user_input)) | |
| print result | |
| except ValueError as e: | |
| print e | |
| else: | |
| print "I don't understand option %s" % sys.argv[1] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment