3.04 Strings — Homework (Signal Interception)

Lesson: https://pages.opencodingsociety.com/python/strings-intercepters/2027

Popcorn Hack 1: Extract the Hidden Message

Code Runner Challenge

Popcorn Hack 1 - Extract the hidden message using indexing and slicing

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - Extract the hidden message using indexing and slicing

transmission = "xxSIGNALxxxCONFIRMEDxx77xxDELTA"

# TODO 1: Use slicing to pull out "SIGNAL" (it starts at index 2 and is 6 characters long)
word_1 = transmission[2:8]
print("Word 1:", word_1)

# TODO 2: Use slicing to pull out "CONFIRMED" (it starts right after "SIGNALxxx")
word_2 = transmission[11:20]
print("Word 2:", word_2)

# TODO 3: Use NEGATIVE indexing/slicing to pull out "DELTA" (it is the last 5 characters)
word_3 = transmission[-5:]
print("Word 3:", word_3)

# TODO 4: Use len() to print how many characters are in the FULL transmission
print("Total length:", len(transmission))

# TODO 5: Use a slice with a step of 2 (transmission[::2]) to print every other character.
#         Look closely: is any part of the hidden message still visible in this pattern?
print("Every other character:", transmission[::2])

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Scrub and Search a Noisy Transmission

Code Runner Challenge

Popcorn Hack 2 - Clean and search a noisy transmission using string methods

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Clean and search a noisy transmission using string methods

noisy = "   ...AGENT>>>Falcon---reports: the PACKAGE has ARRIVED!!!   "

# TODO 1: Use .strip() to remove the leading/trailing whitespace. Save it as trimmed.
trimmed = noisy.strip()
print("Trimmed:", repr(trimmed))

# TODO 2: Use .replace() (you may need it more than once) to remove every ">", "-", and "!" character.
#         Save the result as cleaned.
cleaned = trimmed.replace(">", "").replace("-", "").replace("!", "")
print("Cleaned:", cleaned)

# TODO 3: Use .lower() to make an all-lowercase COPY of cleaned. Save it as lowered.
lowered = cleaned.lower()
print("Lowered:", lowered)

# TODO 4: Use the `in` operator on lowered to check whether "arrived" appears anywhere in the message.
has_arrived = "arrived" in lowered
print("Contains 'arrived':", has_arrived)

# TODO 5: Use .split() with no arguments on cleaned to break it into a list of words.
#         Save it as words, then print how many words there are with len().
words = cleaned.split()
print("Word count:", len(words))

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3: Build a Decoded Report

Code Runner Challenge

Popcorn Hack 3 - Build a decoded report using concatenation and formatting

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 3 - Build a decoded report using concatenation and formatting

agent_name = "Falcon"
target_word = "MIDNIGHT"
confidence = 87          # a percentage, stored as an int
fragments = ["ALPHA", "SEVEN", "DELTA"]

# TODO 1: Use + concatenation to build: "Agent Falcon intercepted: MIDNIGHT"
headline = "Agent " + agent_name + " intercepted: " + target_word
print(headline)

# TODO 2: confidence is an int. Use str() and + concatenation to build: "Confidence: 87%"
confidence_line = "Confidence: " + str(confidence) + "%"
print(confidence_line)

# TODO 3: Rewrite the SAME line as confidence_line_fstring, but using an f-string this time
confidence_line_fstring = f"Confidence: {confidence}%"
print(confidence_line_fstring)

# TODO 4: Use "-".join(fragments) to combine the fragments list into one string
joined_fragments = "-".join(fragments)
print("Joined fragments:", joined_fragments)

# TODO 5: Use an f-string to build ONE final report line combining headline, confidence_line,
#         and joined_fragments, in the form:
#         "REPORT: <headline> | <confidence_line> | Fragments: <joined_fragments>"
final_report = f"REPORT: {headline} | {confidence_line} | Fragments: {joined_fragments}"
print(final_report)

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack: Crack the Final Transmission

Code Runner Challenge

Homework - Crack the Final Transmission using slicing, methods, and formatting

View IPYNB Source
# CODE_RUNNER: Homework - Crack the Final Transmission using slicing, methods, and formatting

raw_transmission = "  2026-09-15,GHOST,THE-EAGLE-LANDS-AT-DAWN,87  "

# TODO 1: Use .strip() to remove the outer whitespace, then .split(",") to break it into fields.
#         Print segments. Since this is a real CSV row (no leading/trailing marker), there
#         should be exactly 4 fields and no stray empty strings.
segments = raw_transmission.strip().split(",")
print("Segments:", segments)

# TODO 2: Use indexing on segments to pull out the date, agent, message code, and confidence.
date = segments[0]
agent = segments[1]
message_code = segments[2]
confidence_str = segments[3]
print("Date:", date, "| Agent:", agent, "| Message code:", message_code, "| Confidence:", confidence_str)

# TODO 3: Use SLICING (not a method) on date to confirm it starts with the year "2026".
year_check = date[0:4]
print("Year check:", year_check)

# TODO 4: Use .replace() to turn the dashes in message_code into spaces. Save it as message.
message = message_code.replace("-", " ")

# TODO 5: Use .lower() and the `in` operator to check whether "dawn" appears anywhere in message.
found_dawn = "dawn" in message.lower()
print("Found 'dawn':", found_dawn)

# TODO 6: Use an f-string with "|" separators to build a Markdown table row of the decoded fields,
#         in the form:
#         "| <date> | <agent> | <message> | <confidence_str>% |"
markdown_row = f"| {date} | {agent} | {message} | {confidence_str}% |"
print("Markdown row:", markdown_row)

# TODO 7: Use an f-string to build a JSON-style line for the same fields, mixing "{", "}", ":", and ","
#         the way real systems pass structured data. Use str(found_dawn).lower() for the boolean, since
#         JSON uses lowercase true/false instead of Python's True/False. Form:
#         {"date": "<date>", "agent": "<agent>", "message": "<message>", "confidence": <confidence_str>, "contains_dawn": <found_dawn lowercase>}
open_brace = "{"
close_brace = "}"
json_line = f'{open_brace}"date": "{date}", "agent": "{agent}", "message": "{message}", "confidence": {confidence_str}, "contains_dawn": {str(found_dawn).lower()}{close_brace}'
print("JSON line:", json_line)

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...