Decrypting the Triangle Cipher

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to decrypt a triangle cipher, which is a method of arranging characters into a right triangle. The hidden message is contained in the last character of each row, and these last characters are concatenated to form the transmitted information.

👀 Preview

text = " LcadcbsdxEsdxcx"
decryption_text = "LabEx"

ðŸŽŊ Tasks

In this project, you will learn:

  • How to create a function to decrypt a triangle cipher
  • How to handle empty or None input
  • How to implement the logic to extract the last character of each row and concatenate them

🏆 Achievements

After completing this project, you will be able to:

  • Understand the concept of a triangle cipher
  • Implement a function to decrypt a triangle cipher
  • Handle different types of input, including empty or None input
  • Apply your knowledge of string manipulation to solve a real-world problem

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python/BasicConceptsGroup -.-> python/strings("`Strings`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") subgraph Lab Skills python/strings -.-> lab-302719{{"`Decrypting the Triangle Cipher`"}} python/while_loops -.-> lab-302719{{"`Decrypting the Triangle Cipher`"}} end

Set Up the Triangle Decryption Function

An example is provided below to aid understanding, given the encrypted string: "LcadcbsdxEsdxcx", when filled into a right-angled triangle, it looks like this:

L
c a
d c b
s d x E
s d x c x

By extracting the last character of each row and concatenating them, we get the decrypted message: "LabEx".

In this step, you will learn how to create the triangle_decryption() function in the triangle.py file.

  1. Open the triangle.py file in your code editor.
  2. Define the triangle_decryption() function that takes a string text as input and returns the decrypted text as a string.
def triangle_decryption(text: str) -> str:
    ## Initialize an empty string to store the decrypted text
    decryption_text = ""

Handle Empty or None Input

In this step, you will learn how to handle the case where the input text is empty or None.

  1. Add an if statement to check if the text is not empty.
if text:
    ## Remove leading and trailing spaces from the text
    while text.startswith(" "):
        text = text[1:]
    while text.endswith(" "):
        text = text[:-1]

    ## Proceed with the decryption process
    ## ...
else:
    ## If the text is None, set the decrypted text to None
    decryption_text = None

Implement the Decryption Logic

In this step, you will learn how to implement the logic to decrypt the triangle cipher.

  1. Initialize a variable i to keep track of the current index in the text string.
  2. Initialize a variable step to keep track of the step size for each row.
  3. Use a while loop to iterate through the text string and append the last character of each row to the decryption_text string.
i = 0
step = 1
while i < len(text):
    ## Append the current character to the decrypted text
    decryption_text += text[i]
    i = i + 1 + step
    step += 1

## Append the last character to the decrypted text if it is not the last character of the text
if i - step + 1 != len(text):
    decryption_text += text[-1]

Return the Decrypted Text

In this final step, you will learn how to return the decrypted text.

  1. After the decryption process, return the decryption_text string.
return decryption_text

The complete triangle_decryption() function should look like this:

def triangle_decryption(text: str) -> str:
    ## Initialize an empty string to store the decrypted text
    decryption_text = ""

    if text:
        ## Remove leading and trailing spaces from the text
        while text.startswith(" "):
            text = text[1:]
        while text.endswith(" "):
            text = text[:-1]

        i = 0
        step = 1
        while i < len(text):
            ## Append the current character to the decrypted text
            decryption_text += text[i]
            i = i + 1 + step
            step += 1

        ## Append the last character to the decrypted text if it is not the last character of the text
        if i - step + 1 != len(text):
            decryption_text += text[-1]

    else:
        ## If the text is None, set the decrypted text to None
        decryption_text = None

    return decryption_text

if __name__ == "__main__":
    print(triangle_decryption("LcadcbsdxEsdxcx"))
    print(triangle_decryption("Lcadb"))
    print(triangle_decryption(" LcadcbsdxEsdxcx"))
    print(triangle_decryption("L ab"))
    print(triangle_decryption(None))

Test the Script

In this step, you will learn how to test the script with the provided examples.

  1. Save the triangle.py file.
  2. Run the script with the provided examples:
python3 /home/labex/project/triangle.py

The output should match the expected results:

## LcadcbsdxEsdxcx
LabEx
## Lcadb
Lab
## LcadcbsdxEsdxcx
LabEx
## L ab
Lab
## None
None

Congratulations! You have completed the project.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like