railfence.py

Created by nick-koberstein

Created on January 03, 2022

1.09 KB


def text_clean(text):
  LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  text = text.upper()
  cleaned = ''
  for char in text:
    if char in LETTERS:
      cleaned += char
      
  return cleaned
  
def railfence(text, rows, encipher=True):
    """
    Example of Use:
        >>> railfence('samplemessage', 3)
        'SLSEAPEESGMMA'
    
    Arguments:
        text (str):  represents either plaintext or ciphertext
        rows (int):  integer that represents the number of rows
        
    Returns:
        (str): represents the output, either plaintext or ciphertext
        (str): an error message
    """
    
    text = text_clean( text )
    
    if rows == 2:
        if encipher == True:
            return text[::2].upper() + text[1::2].upper()
        else:
            return "this is where the 2 row decipher code will go"
    elif rows == 3:
        if encipher == True:
            return text[::4].upper() + text[1::2].upper() + text[2::4].upper()
        else:
            return "this is where the 3 row decipher code will go"
    else:
        return "number of rails not supported"