Created
January 7, 2016 21:13
-
-
Save drcjar/8dabe9f93d6e66f361a2 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
import sys | |
def run(text, shift): | |
text = text.lower() | |
ord_of_a = ord('a') | |
result = '' | |
for letter in text: | |
if letter in 'qwertyuopasdghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM': | |
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