Playing With Strings
Thomas J. Kennedy
1 The Problem
If you are anything like me… you often end up teaching a friend’s child, parent’s friend’s child, younger cousin, or neighbor’s child something about…
- retro videogames (e.g., SNES, N64, or GameCube)
- video scaling (the methods used to fit an image to a 4K screen or enlarge an image)
- mathematics
- language (e.g., English)
- science (e.g., Biology or Physics)
(I am looking forward to teaching these topics to my daughter in a couple years.)
1.1 Brainstorming
Suppose that we need to help our young charge (primary school age) prepare for an English quiz. Our charge is learning the basics of sentence structure. They have asked you…
Is it possible to have the computer check my sentences?
Of course… we know that we can write a Python program, and, more importantly, use this as the start of both an English and Computer Science combo lesson! (See… there is a reason we are not using something along the lines of Grammarly.)
1.2 Our Solution
Our goal is twofold…
-
Help our young charge with their grammar skills
-
Set up a Python lesson
At the end of the day… we want our program to do the following…
-
Check that the first letter of a sentence is capitalized.
-
Check that a sentence ends with a period, question mark, or exclamation point.
-
Check for quotes, commas, semicolons, colons, dashes, and ellipses.
Our young charge has started raiding their parents’ bookshelves… and Isaac Asimov and J.R.R. Tolkien are grammitical artists.
If these symbols show up in a sentence we want to remind our young charge to practice the not fun stuff first.
1.3 Input
This subsection will describe the input content, format, and overall structure.
Input to this program will be interactive (i.e., a prompt will be displayed and the program will wait for keyboard input). User input will be a sentence.
1.4 Output
A single execution of the program will be similiar to…
Enter a sentence: Hello, my name is Tom.
Analysis:
1. Sentence starts with 'H' (captital letter)
2. Sentence ends with a/an '.' (punctuation)
3. Sentence does not include *advanced* puntuation
Would you like to check another sentence? (yes/no): no
2 Your Tasks
You work will focus on three functions. You will need to mkae use of Python’s built-in str
complete functions to…
-
Check that the first letter of a sentence is capitalized.
def first_letter_is_capitalized(sentence: str) -> bool: """ T.B.W. """
-
Check that a sentence ends with a period, question mark, or exclamation point.
def ends_with_punctuation(sentence: str) -> bool: """ T.B.W. """
-
Check for quotes, commas, semicolons, colons, dashes, and ellipses.
def includes_advanced_punctuation(sentence: str) -> bool: """ T.B.W. """
You will notice that each function has a placeholder for pydoc documentation…
"""
T.B.W.
"""
3 Mechanics
You can run the main program with…
python3.11 check_sentence.py
The test code can be run with…
python3.11 -m pytest -v tests
4 Files
There are three (3) files in this assignment:
check_problems.py
examine_sentence.py
test/test_examine_sentence.py
You will be working in examine_sentence.py
. Do not modify the other files.
You can access the starting files on GitHub. You may either clone the starting repository (through Git) or download a zip file.
5 Grading
Your grade will be based on the following criteria…
- test 000 - code passes
test_first_letter_is_capitalized
- test 001 - code passes
test_ends_with_punctuation
- test 002 - code passes
test_includes_advanced_punctuation
6 Submitting
You will submit your completed copy of examine_sentence.py
as a file upload through Canvas.
Do not alter any of the other source code files, nor change the module interface.