๐Ÿ”ด LESSON 06 ยท FULLY INDEPENDENT

The Final Challenge

No starter code. No fill-in-the-gaps. Just you and Python. Design, write, test, and evaluate three complete programs from scratch.

๐Ÿ”ด No code provided โ€” full independence required

Unit Complete!

You've completed all 6 lessons on String Manipulation. You can now read files, process strings using the Edexcel PLS functions, and write results back โ€” from scratch!

๐Ÿ“Š Your Progress This Lesson

Challenge 1
Challenge 2
Challenge 3 โญ
Challenge 4 โญโญ
๐Ÿ“ Text File for the Challenges SETUP
Create input.txt: Open Notepad, paste the text below, and save as input.txt in the same folder as your Python files.
TEXT โ€” input.txt
The island of Jersey is located in the English Channel
Python was created by Guido van Rossum in 1991
GCSE Computer Science covers topics like algorithms
My phone number is 07700 900456
The population of Jersey is approximately 103000
Programming helps develop logical thinking skills
There are 26 letters in the English alphabet
Haute Vallee School is in Jersey
The exam code for this subject is 1CP2
I scored 87 percent on my last mock exam
the quick brown fox jumps over the lazy dog
TESTING ALL UPPERCASE LETTERS HERE
mixed CaSe WiTh 42 NuMbErS and spaces
abc123def456ghi789
โš ๏ธ Rules for this lesson:
โ€ข No code is provided โ€” you write everything from scratch in IDLE
โ€ข Use the Edexcel PLS string functions: upper(), lower(), len(), find(), isalpha(), isdigit(), isspace()
โ€ข Mark schemes are available โ€” but try each challenge fully before looking
โ€ข Tick off each requirement as you complete it

๐Ÿ… Challenges WRITE FROM SCRATCH
CHALLENGE 01
โ˜…โ˜…โ˜…

The Text Transformer

Write a program that reads input.txt line by line. For each line, it should: strip whitespace, convert to uppercase, find the length, get the first and last characters, and check if the whole line (after stripping) is alphabetical. Write a formatted summary of each line to output.txt.

# Challenge 1 โ€” Mark Scheme (with subroutine)

def analyse_line(line):
    """Subroutine to analyse a single line and return a report string"""
    upper_ver  = line.upper()
    length     = len(line)
    first      = line[0] if line else "-"
    last       = line[-1] if line else "-"
    all_alpha  = line.replace(" ", "").isalpha()
    report = f"Original : {line}\n"
    report += f"Upper    : {upper_ver}\n"
    report += f"Length   : {length} | First: {first} | Last: {last}\n"
    report += f"Alpha?   : {all_alpha}\n---\n"
    return report

# Main program
infile  = open("input.txt", "r")
outfile = open("output.txt", "w")

for line in infile:
    line = line.strip()
    outfile.write(analyse_line(line))

infile.close()
outfile.close()
CHALLENGE 02
โ˜…โ˜…โ˜…

The Smart Filter

Write a program that: asks the user for a search keyword, reads input.txt, and writes lines containing that keyword (case-insensitive) to output.txt in uppercase. Also display: how many lines matched, the total character count of matching lines, and the position of the keyword in the first matching line.

# Challenge 2 โ€” Mark Scheme (with subroutine)

def search_line(line, keyword):
    """Subroutine that searches for a keyword in a line (case-insensitive)"""
    line_lower = line.lower()
    key_lower  = keyword.lower()
    if key_lower in line_lower:
        position = line_lower.find(key_lower)
        return True, position
    return False, -1

# Main program
keyword    = input("Search for: ")
infile     = open("input.txt", "r")
outfile    = open("output.txt", "w")
count      = 0
total_chars= 0
first_pos  = -1

for line in infile:
    line = line.strip()
    found, pos = search_line(line, keyword)
    if found:
        count += 1
        total_chars += len(line)
        if count == 1:
            first_pos = pos
        outfile.write(line.upper() + "\n")

infile.close()
outfile.close()

if count == 0:
    print("No matches found.")
else:
    print(f"Matches: {count}")
    print(f"Total chars in matches: {total_chars}")
    print(f"Keyword first found at index: {first_pos}")
CHALLENGE 03 ยท EXTENSION โญ
โ˜…โ˜…โ˜…

The Full Analyser

Write a complete text analysis program. It should read input.txt and produce a detailed report in output.txt containing: for each line โ€” its length, letter/digit/space counts, first 5 characters, whether it contains a number. At the end โ€” totals for the whole file, the longest line, and a count of lines that are purely alphabetical.

# Challenge 3 โ€” Mark Scheme (with subroutines)

def count_chars(line):
    """Counts letters, digits, and spaces in a line"""
    letters = 0
    digits  = 0
    spaces  = 0
    for ch in line:
        if ch.isalpha(): letters += 1
        elif ch.isdigit(): digits += 1
        elif ch.isspace(): spaces += 1
    return letters, digits, spaces

def write_line_report(outfile, num, line, letters, digits, spaces):
    """Writes a formatted report for one line"""
    outfile.write(f"Line {num}: {line}\n")
    outfile.write(f"  Len:{len(line)} | Letters:{letters} | Digits:{digits} | Spaces:{spaces}\n")
    outfile.write(f"  First 5: {line[0:5]} | Has digits: {digits > 0}\n\n")

def write_summary(outfile, line_count, total_letters, total_digits, alpha_lines, longest):
    """Writes the final summary section"""
    outfile.write("=== SUMMARY ===\n")
    outfile.write(f"Total lines    : {line_count}\n")
    outfile.write(f"Total letters  : {total_letters}\n")
    outfile.write(f"Total digits   : {total_digits}\n")
    outfile.write(f"Alpha-only lines: {alpha_lines}\n")
    outfile.write(f"Longest line   : {longest}\n")

# Main program
infile    = open("input.txt", "r")
outfile   = open("output.txt", "w")
longest   = ""
alpha_lines= 0
total_letters = 0
total_digits  = 0
line_count    = 0

for line in infile:
    line    = line.strip()
    line_count += 1
    letters, digits, spaces = count_chars(line)
    total_letters += letters
    total_digits  += digits
    if digits == 0: alpha_lines += 1
    if len(line) > len(longest): longest = line
    write_line_report(outfile, line_count, line, letters, digits, spaces)

write_summary(outfile, line_count, total_letters, total_digits, alpha_lines, longest)
infile.close()
outfile.close()
CHALLENGE 04 ยท SUPER EXTENSION
โ˜…โ˜…โ˜…

The Menu-Driven Text Tool

Write a program with a menu that lets the user choose different text operations. Use subroutines for each operation. The program should loop until the user chooses to quit.

# Challenge 4 โ€” Mark Scheme

def uppercase_all():
    infile = open("input.txt", "r")
    outfile = open("output.txt", "w")
    for line in infile:
        outfile.write(line.strip().upper() + "\n")
    infile.close()
    outfile.close()
    print("Done! All lines converted to uppercase.")

def count_words():
    infile = open("input.txt", "r")
    total = 0
    for line in infile:
        words = line.strip().split()
        total += len(words)
        print(f"{line.strip()} โ†’ {len(words)} words")
    infile.close()
    print(f"Total words: {total}")

def find_digits():
    infile = open("input.txt", "r")
    for line in infile:
        line = line.strip()
        count = 0
        for ch in line:
            if ch.isdigit(): count += 1
        if count > 0:
            print(f"{line} โ†’ {count} digits")
    infile.close()

def keyword_search():
    keyword = input("Enter keyword: ")
    infile = open("input.txt", "r")
    count = 0
    for line in infile:
        if keyword.lower() in line.lower():
            print(f"Found: {line.strip()}")
            count += 1
    infile.close()
    print(f"Total matches: {count}")

# Main menu loop
running = True
while running:
    print("\n=== Text Tool Menu ===")
    print("1. Convert all to UPPERCASE")
    print("2. Count words per line")
    print("3. Find lines with digits")
    print("4. Search for a keyword")
    print("5. Quit")

    choice = input("Choose (1-5): ")

    if choice == "1": uppercase_all()
    elif choice == "2": count_words()
    elif choice == "3": find_digits()
    elif choice == "4": keyword_search()
    elif choice == "5": running = False
    else: print("Invalid choice. Try again.")

๐Ÿค Peer Review Checklist OPTIONAL

Rate a classmate's code on each criterion:

Files opened and closed correctly
โ˜…โ˜…โ˜…
String functions used correctly
โ˜…โ˜…โ˜…
Output is clear and formatted well
โ˜…โ˜…โ˜…
No unnecessary code or errors
โ˜…โ˜…โ˜…
๐ŸŽ“ You've completed the String Manipulation unit!

You can now: open files, loop through lines, apply upper(), lower(), len(), find(), isalpha(), isdigit(), isspace(), use indexing and slicing, and write results back to files. These skills are all in the Edexcel 1CP2 specification. ๐Ÿ
โ† Lesson 5 โ†ฉ Back to All Lessons