In [103]:
import unittest

def encrypt(text, n):
    if n < 1:
        return text
    
    for i in range(0, n):
        text = text[1::2] + text[::2]
        
    return text

def decrypt(encrypted_text, n):
    if n < 1:
        return encrypted_text
    
    add = ""
    if len(encrypted_text) % 2 == 1:
        add = encrypted_text[-1]
    
    for i in range(0, n):
        mid = len(encrypted_text) / 2
        a = encrypted_text[:mid]
        b = encrypted_text[mid:]
        
        # problem is that zip will ignore an unpaired value...
        encrypted_text = ''.join([x+y for x, y in zip(b, a)])

    return encrypted_text + add

In [104]:
print encrypt("This is a test!", 0) #, "This is a test!")
print encrypt("This is a test!", 1) #, "hsi  etTi sats!")
print encrypt("This is a test!", 2) #, "s eT ashi tist!")
print encrypt("This is a test!", 3) #, " Tah itse sits!")
print encrypt("This is a test!", 4) #, "This is a test!")
print encrypt("This is a test!", -1) #, "This is a test!")


This is a test!
hsi  etTi sats!
s eT ashi tist!
 Tah itse sits!
This is a test!
This is a test!

In [105]:
a = "This is a test!"[::2]
b = "This is a test!"[1::2]
print b+a


hsi  etTi sats!

In [106]:
decrypt("hsi  etTi sats!", 1)


Out[106]:
'This is a test!'

In [107]:
decrypt("s eT ashi tist!", 2)


Out[107]:
'This is a test!'

In [108]:
# code wars top solution
def decrypt_cw(text, n):
    if text in ("", None):
        return text
    
    ndx = len(text) // 2 # this is floor division

    for i in range(n):
        a = text[:ndx]
        b = text[ndx:]
        text = "".join(b[i:i+1] + a[i:i+1] for i in range(ndx + 1))
    return text



def encrypt_cw(text, n):
    for i in range(n):
        text = text[1::2] + text[::2]
    return text

In [112]:
if None in ("", None, "abs"):
    print "Something"


Something

In [115]:
ndx = len("this is") // 2
print ndx


3

In [ ]: