Skip to content

How to Decrypt a Triangle Cipher in Python

Learn how to decrypt a triangle cipher in Python by extracting the last character of each row in a right-angled triangular arrangement, handling edge cases like whitespace and incomplete rows.

A triangle cipher is a transposition cipher where characters are arranged in a right-angled triangle. To decrypt it, we extract the last character of every row to form the hidden message. This guide implements a robust decryption function in Python that handles arithmetic sequences and edge cases like whitespace or empty inputs.

Understanding the Triangle Logic

Visualizing the cipher is crucial before coding. Given the string "Tcudctsdxosdxcr", the characters fill a triangle row by row, increasing the row length by 1 each time.

Row 1 (1 char):  T              -> Last char: T
Row 2 (2 chars): c u            -> Last char: u
Row 3 (3 chars): d c t          -> Last char: t
Row 4 (4 chars): s d x o        -> Last char: o
Row 5 (5 chars): s d x c r      -> Last char: r

Decrypted Message: "Tutor"

We need to jump through the string indices based on an increasing step size (1, 2, 3, 4, ...).

Step 1: Input Validation and Cleaning

Before processing, we must ensure the input is valid. The function should handle None types gracefully and strip unnecessary whitespace that might affect index calculations.

Handling None and Whitespace:

def prepare_text(text):
    
    if text:
        while text.startswith(" "):
            text = text[1:]
        while text.endswith(" "):
            text = text[:-1]
        return text
    else:
        return None

Step 2: Implementing the Decryption Loop

The core logic involves calculating the index of the last character of each row. We start at index 0, and the gap between the ends of rows increases by 1 in every iteration.

The Index Jumping Algorithm:

text = "Tcudctsdxosdxcr"
decryption_text = ""
i = 0      # Current index
step = 1   # Size of the current row

while i < len(text):
    decryption_text += text[i]
    
    i = i + 1 + step
    
    step += 1

if i - step + 1 != len(text):
    decryption_text += text[-1]

print(f"Result: {decryption_text}")

Output:

Result: Tutor

Complete Code Solution

Here is the fully functional script combining validation, cleaning, and the arithmetic logic.

from typing import Optional

def triangle_decryption(text: Optional[str]) -> Optional[str]:
    """
    Decrypts a string arranged in a right-triangle pattern by 
    extracting the last character of each row.
    """
    decryption_text = ""

    if text:
        while text.startswith(" "):
            text = text[1:]
        while text.endswith(" "):
            text = text[:-1]

        i = 0
        step = 1
        while i < len(text):
            decryption_text += text[i]
            
            i = i + 1 + step
            step += 1

        if i - step + 1 != len(text):
            decryption_text += text[-1]

    else:
        decryption_text = None

    return decryption_text

if __name__ == "__main__":
    print(f"Input: 'Tcudctsdxosdxcr' -> {triangle_decryption('Tcudctsdxosdxcr')}")
    
    print(f"Input: 'Tcudc'           -> {triangle_decryption('Tcudc')}")
    
    print(f"Input: ' Tcudctsdxosdxcr'-> {triangle_decryption(' Tcudctsdxosdxcr')}")
    
    print(f"Input: None              -> {triangle_decryption(None)}")

Output:

Input: 'Tcudctsdxosdxcr' -> Tutor
Input: 'Tcudc'           -> Tuc
Input: ' Tcudctsdxosdxcr'-> Tutor
Input: None              -> None

Conclusion

Decrypting a triangle cipher is an exercise in identifying arithmetic progressions within string indices.

  1. Visualize the shape to determine the row lengths (1, 2, 3, ...).
  2. Calculate the indices: The gap between selected characters grows linearly.
  3. Validate inputs to prevent errors with None or whitespace.
Navigation