Acrostic Poetry Decryption in Python

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to decipher the hidden message in an acrostic poem. Acrostic poems are a type of poetry where the first letters of each line spell out a word or phrase. Your task is to write a Python function that can extract the first words of each line and concatenate them to reveal the hidden message.

👀 Preview

## Example
$ python3 acrostic_poetry.py
RainItItAnd

ðŸŽŊ Tasks

In this project, you will learn:

  • How to understand the problem statement and requirements for the Acrostic Poetry Decryption project
  • How to implement the acrostic_poetry_decryption(poem) function to decrypt the message in the acrostic poem
  • How to test the decryption function with an example input

🏆 Achievements

After completing this project, you will be able to:

  • Understand and solve problems involving text manipulation and pattern extraction
  • Write a Python function that can decipher hidden messages in acrostic poems
  • Test and debug your code to ensure it works correctly

Understand the Problem

In this step, you will learn how to understand the problem statement and the requirements for the Acrostic Poetry Decryption project.

The goal of this project is to write a function acrostic_poetry_decryption(poem) that takes a string representing the content of a poem as input, and returns the decrypted message by extracting the first words of each line and concatenating them together.

The requirements for the project are:

  1. Except for the re library, do not use any other standard libraries or third-party libraries.
  2. The poem parameter of the function can be a string that may be empty, None, or any other value.
  3. Do not modify the file path, file name (acrostic_poetry.py), or function name (acrostic_poetry_decryption(poem)).
  4. When debugging the code in the online environment, use the python3 acrostic_poetry.py command to call Python3.

Implement the Decryption Function

In this step, you will implement the acrostic_poetry_decryption(poem) function to decrypt the message in the acrostic poem.

Here's the starter code for the acrostic_poetry.py file:

import re
from typing import Optional


def acrostic_poetry_decryption(poem: str) -> Optional[str]:
    """
    Decipher the message in the acrostic poem by extracting the first words of each line and linking them together.
    """
    ## If the input content is empty, return `None`
    if not poem:
        return None

    ## TODO: Implement the decryption logic
    ## 1. Split the poem into lines using the ',' or '.' separator
    ## 2. Extract the first word from each line
    ## 3. Concatenate the first words together to get the decryption text
    ## 4. Assign the resulting string to the variable `decryption_text`

    return decryption_text

Here's how you can implement the decryption function:

  1. Split the poem string into a list of lines using the re.split() function with the r"[,.]" regular expression pattern.
  2. Iterate through the list of lines, and for each line, extract the first word by splitting the line by spaces and taking the first element.
  3. Concatenate all the first words together to get the decryption text.
  4. Assign the decryption text to the decryption_text variable and return it.

Here's the completed code:

import re
from typing import Optional


def acrostic_poetry_decryption(poem: str) -> Optional[str]:
    """
    Decipher the message in the acrostic poem by extracting the first words of each line and linking them together.
    """
    ## If the input content is empty, return `None`
    if not poem:
        return None

    ## Split the poem into lines using the ',' or '.' separator
    lines = re.split(r"[,.]", poem)

    ## Extract the first word from each line
    words = [line.strip().split(" ")[0] for line in lines]

    ## Concatenate the first words together to get the decryption text
    decryption_text = "".join(words)

    return decryption_text

Test the Decryption Function

In this step, you will test the acrostic_poetry_decryption(poem) function with the example provided in the challenge.

  1. Add the following code at the end of the acrostic_poetry.py file:
if __name__ == "__main__":
    string = "Rain is falling all around, It falls on field and tree. It rains on the umbrella here, And on the ships at sea."
    print(acrostic_poetry_decryption(string))
  1. Save the file and run the following command in the terminal:
python3 acrostic_poetry.py

The output should be:

RainItItAnd

This confirms that the acrostic_poetry_decryption(poem) function is working as expected.

Congratulations! You have completed the Acrostic Poetry Decryption project. If you have any questions or need further assistance, feel free to ask.

âœĻ 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