Advertisement
Geometrian

Pendulum Code

Dec 19th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. def pendulum(string):
  2.     result = ""
  3.     side = 0
  4.     while len(string)>0:
  5.         if side == 0:
  6.             result = string[0] + result
  7.         else:
  8.             result = result + string[0]
  9.         string = string[1:]
  10.         side = 1 - side
  11.     return result
  12. def antipendulum(string):
  13.     result = ""
  14.    
  15.     index = (len(string)-1) // 2
  16.     swing = 1
  17.     swing_dir = 1
  18.  
  19.     while index>=0 and index<len(string):
  20.         result += string[index]
  21.         index += swing * swing_dir
  22.         swing += 1
  23.         swing_dir = -swing_dir
  24.  
  25.     return result
  26.  
  27. def encrypt():
  28.     while True:
  29.         pt = input("Plaintext: ")
  30.  
  31.         if pt == "": break
  32.         ct = pendulum(pt)
  33.  
  34.         print("Ciphertext: \""+ct+"\"")
  35. def decrypt():
  36.     while True:
  37.         ct = input("Ciphertext: ")
  38.  
  39.         if ct == "": break
  40.         pt = antipendulum(ct)
  41.  
  42.         print("Plaintext: \""+pt+"\"")
  43. encrypt()
  44. ##decrypt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement