Skip to content

Instantly share code, notes, and snippets.

@coyotebush
Created April 2, 2013 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coyotebush/5289348 to your computer and use it in GitHub Desktop.
Save coyotebush/5289348 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
def incnums(mx, amt, nums):
if len(nums) == 0:
nums.append(-1)
(q, r) = divmod(nums[-1] + amt, mx + 1)
nums.pop()
if q > 0:
incnums(mx, q, nums)
nums.append(r)
def add_inputbase(s, amt):
mn = 32
mx = 126
nums = map(lambda x: ord(x) - mn, s)
incnums(mx - mn, amt, nums)
return ''.join(map(lambda x: chr(x + mn), nums))
def test_add_inputbase():
print add_inputbase('a', 3) == 'd'
print add_inputbase('glhf', 3) == 'glhi'
print add_inputbase('glhf', 24) == 'glh~'
print add_inputbase('glhf', 25) == 'gli '
print add_inputbase('~~~~', 1) == ' '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment