Skip to content

Instantly share code, notes, and snippets.

@drcjar
Created January 7, 2016 21:46
Show Gist options
  • Save drcjar/974a5dadf6c6f1d0c494 to your computer and use it in GitHub Desktop.
Save drcjar/974a5dadf6c6f1d0c494 to your computer and use it in GitHub Desktop.
import sys
def run(text, shift):
text = text.lower()
ord_of_a = ord('a')
result = ''
for letter in text:
if letter in 'qwertyuiopasdfghjklzxcvbnm':
letter = chr(abs((ord(letter) - ord_of_a + shift) % 26 + ord_of_a))
result += letter
return result
def test_a_to_b():
actual = run('a', 1)
expected = 'b'
if actual != expected:
print 'Test a to b fail!'
def test_z_to_a():
actual = run('z', 1)
expected = 'a'
if actual != expected:
print 'Test z to a fail!'
def test_leaves_spaces():
actual = run('Hello World', 1)
expected = 'Ifmmp Xpsme'
if actual != expected:
print 'Test leaves spaces fail!'
def test_decrypt():
actual = run('ifmmp xpsme', -1)
expected = 'hello world'
if actual != expected:
print 'Test decrypt fail!'
if __name__=='__main__':
test_a_to_b()
test_z_to_a()
test_leaves_spaces()
test_decrypt()
op = sys.argv[1].lower()
text = sys.argv[2]
shift = int(sys.argv[3])
if op == 'd':
shift = -shift
print run(text, shift)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment