Python: completed Little Sister's Essay

This commit is contained in:
Andrew Scott 2024-09-28 12:12:02 -04:00
parent f6384f3c7a
commit 9750e8d9ef
Signed by: a
GPG key ID: 7CD5A5977E4931C1
7 changed files with 517 additions and 0 deletions

View file

@ -0,0 +1,22 @@
{
"authors": [
"kimolivia"
],
"contributors": [
"valentin-p",
"BethanyG"
],
"files": {
"solution": [
"string_methods.py"
],
"test": [
"string_methods_test.py"
],
"exemplar": [
".meta/exemplar.py"
]
},
"icon": "anagrams",
"blurb": "Learn about string methods while improving your little sister's school essay."
}

View file

@ -0,0 +1 @@
{"track":"python","exercise":"little-sisters-essay","id":"68b15f3f00a4425db54965e6136896c1","url":"https://exercism.org/tracks/python/exercises/little-sisters-essay","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View file

@ -0,0 +1,130 @@
# Help
## Running the tests
We use [pytest][pytest: Getting Started Guide] as our website test runner.
You will need to install `pytest` on your development machine if you want to run tests for the Python track locally.
You should also install the following `pytest` plugins:
- [pytest-cache][pytest-cache]
- [pytest-subtests][pytest-subtests]
Extended information can be found in our website [Python testing guide][Python track tests page].
### Running Tests
To run the included tests, navigate to the folder where the exercise is stored using `cd` in your terminal (_replace `{exercise-folder-location}` below with your path_).
Test files usually end in `_test.py`, and are the same tests that run on the website when a solution is uploaded.
Linux/MacOS
```bash
$ cd {path/to/exercise-folder-location}
```
Windows
```powershell
PS C:\Users\foobar> cd {path\to\exercise-folder-location}
```
<br>
Next, run the `pytest` command in your terminal, replacing `{exercise_test.py}` with the name of the test file:
Linux/MacOS
```bash
$ python3 -m pytest -o markers=task {exercise_test.py}
==================== 7 passed in 0.08s ====================
```
Windows
```powershell
PS C:\Users\foobar> py -m pytest -o markers=task {exercise_test.py}
==================== 7 passed in 0.08s ====================
```
### Common options
- `-o` : override default `pytest.ini` (_you can use this to avoid marker warnings_)
- `-v` : enable verbose output.
- `-x` : stop running tests on first failure.
- `--ff` : run failures from previous test before running other test cases.
For additional options, use `python3 -m pytest -h` or `py -m pytest -h`.
### Fixing warnings
If you do not use `pytest -o markers=task` when invoking `pytest`, you might receive a `PytestUnknownMarkWarning` for tests that use our new syntax:
```bash
PytestUnknownMarkWarning: Unknown pytest.mark.task - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
```
To avoid typing `pytest -o markers=task` for every test you run, you can use a `pytest.ini` configuration file.
We have made one that can be downloaded from the top level of the Python track directory: [pytest.ini][pytest.ini].
You can also create your own `pytest.ini` file with the following content:
```ini
[pytest]
markers =
task: A concept exercise task.
```
Placing the `pytest.ini` file in the _root_ or _working_ directory for your Python track exercises will register the marks and stop the warnings.
More information on pytest marks can be found in the `pytest` documentation on [marking test functions][pytest: marking test functions with attributes] and the `pytest` documentation on [working with custom markers][pytest: working with custom markers].
Information on customizing pytest configurations can be found in the `pytest` documentation on [configuration file formats][pytest: configuration file formats].
### Extending your IDE or Code Editor
Many IDEs and code editors have built-in support for using `pytest` and other code quality tools.
Some community-sourced options can be found on our [Python track tools page][Python track tools page].
[Pytest: Getting Started Guide]: https://docs.pytest.org/en/latest/getting-started.html
[Python track tools page]: https://exercism.org/docs/tracks/python/tools
[Python track tests page]: https://exercism.org/docs/tracks/python/tests
[pytest-cache]:http://pythonhosted.org/pytest-cache/
[pytest-subtests]:https://github.com/pytest-dev/pytest-subtests
[pytest.ini]: https://github.com/exercism/python/blob/main/pytest.ini
[pytest: configuration file formats]: https://docs.pytest.org/en/6.2.x/customize.html#configuration-file-formats
[pytest: marking test functions with attributes]: https://docs.pytest.org/en/6.2.x/mark.html#raising-errors-on-unknown-marks
[pytest: working with custom markers]: https://docs.pytest.org/en/6.2.x/example/markers.html#working-with-custom-markers
## Submitting your solution
You can submit your solution using the `exercism submit string_methods.py` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Python track's documentation](https://exercism.org/docs/tracks/python)
- The [Python track's programming category on the forum](https://forum.exercism.org/c/programming/python)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
Below are some resources for getting help if you run into trouble:
- [The PSF](https://www.python.org) hosts Python downloads, documentation, and community resources.
- [The Exercism Community on Discord](https://exercism.org/r/discord)
- [Python Community on Discord](https://pythondiscord.com/) is a very helpful and active community.
- [/r/learnpython/](https://www.reddit.com/r/learnpython/) is a subreddit designed for Python learners.
- [#python on Libera.chat](https://www.python.org/community/irc/) this is where the core developers for the language hang out and get work done.
- [Python Community Forums](https://discuss.python.org/)
- [Free Code Camp Community Forums](https://forum.freecodecamp.org/)
- [CodeNewbie Community Help Tag](https://community.codenewbie.org/t/help)
- [Pythontutor](http://pythontutor.com/) for stepping through small code snippets visually.
Additionally, [StackOverflow](http://stackoverflow.com/questions/tagged/python) is a good spot to search for your problem/question to see if it has been answered already.
If not - you can always [ask](https://stackoverflow.com/help/how-to-ask) or [answer](https://stackoverflow.com/help/how-to-answer) someone else's question.

View file

@ -0,0 +1,29 @@
# Hints
## General
- [Python Documentation: String Methods][string-method-docs]
- [Python Documentation Tutorial: Text][tutorial-strings]
## 1. Capitalize the title of the paper
- You can use [string methods][title-method-docs] to capitalize the title properly.
## 2. Check if each sentence ends with a period
- You can use [string methods][endswith-method-docs] to check the ending of a string.
## 3. Clean up spacing
- You can use [string methods][strip-method-docs] to remove whitespace.
## 4. Replace words with a synonym
- You can use [string methods][replace-method-docs] to replace words.
[endswith-method-docs]: https://docs.python.org/3/library/stdtypes.html#str.endswith
[replace-method-docs]: https://docs.python.org/3/library/stdtypes.html#str.replace
[string-method-docs]: https://docs.python.org/3/library/stdtypes.html#string-methods
[strip-method-docs]: https://docs.python.org/3/library/stdtypes.html#str.strip
[title-method-docs]: https://docs.python.org/3/library/stdtypes.html#str.title
[tutorial-strings]: https://docs.python.org/3/tutorial/introduction.html#text

View file

@ -0,0 +1,194 @@
# Little Sister's Essay
Welcome to Little Sister's Essay on Exercism's Python Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
The `str` class offers [many useful methods][str methods] for working with and composing strings.
These include searching, cleaning, splitting, transforming, translating, and many other techniques.
Strings are [immutable sequences][text sequence] of [Unicode code points][unicode code points] -- individual "characters" or code points (_strings of length 1_) can be referenced by `0-based index` number from the left, or `-1-based index` number from the right.
Strings can be iterated through using `for item in <str>` or `for index, item in enumerate(<str>)` syntax.
They can be concatenated using the `+` operator or via `<string>.join(<iterable>)` and implement all [common sequence operations][common sequence operations].
Strings are _immutable_, meaning the value of a `str` object in memory cannot change.
Functions or methods that operate on a `str` (_like the ones we are learning about here_) will return a new `instance` of that `str` object instead of modifying the original `str`.
Following is a small selection of Python string methods.
For a complete list, see the [str class][str methods] in the Python docs.
[`<str>.title()`][str-title] parses a string and capitalizes the first "character" of each "word" found.
In Python, this is very dependent on the [language codec][codecs] used and how the particular language represents words and characters.
There may also be [locale][locale] rules in place for a language or character set.
```python
man_in_hat_th = 'ผู้ชายใส่หมวก'
man_in_hat_ru = 'мужчина в шляпе'
man_in_hat_ko = '모자를 쓴 남자'
man_in_hat_en = 'the man in the hat.'
>>> man_in_hat_th.title()
'ผู้ชายใส่หมวก'
>>> man_in_hat_ru.title()
'Мужчина В Шляпе'
>>> man_in_hat_ko.title()
'모자를 쓴 남자'
>> man_in_hat_en.title()
'The Man In The Hat.'
```
[`<str>.endswith(<suffix>)`][str-endswith] returns `True` if the string ends with `<suffix>`, `False` otherwise.
```python
>>> 'My heart breaks. 💔'.endswith('💔')
True
>>> 'cheerfulness'.endswith('ness')
True
# Punctuation is part of the string, so needs to be included in any endswith match.
>>> 'Do you want to 💃?'.endswith('💃')
False
>> 'The quick brown fox jumped over the lazy dog.'.endswith('dog')
False
```
[`<str>.strip(<chars>)`][str-strip] returns a copy of the `str` with leading and trailing `<chars>` removed.
The code points specified in `<chars>` are not a prefix or suffix - **all combinations** of the code points will be removed starting from **both ends** of the string.
If nothing is specified for `<chars>`, all combinations of whitespace code points will be removed.
```python
# This will remove "https://", because it can be formed from "/stph:".
>>> 'https://unicode.org/emoji/'.strip('/stph:')
'unicode.org/emoji'
# Removal of all whitespace from both ends of the str.
>>> ' 🐪🐪🐪🌟🐪🐪🐪 '.strip()
'🐪🐪🐪🌟🐪🐪🐪'
>>> justification = 'оправдание'
>>> justification.strip('еина')
'оправд'
# Prefix and suffix in one step.
>>> 'unaddressed'.strip('dnue')
'address'
>>> ' unaddressed '.strip('dnue ')
'address'
```
[`<str>.replace(<substring>, <replacement substring>)`][str-replace] returns a copy of the string with all occurrences of `<substring>` replaced with `<replacement substring>`.
The quote used below is from [The Hunting of the Snark][The Hunting of the Snark] by [Lewis Carroll][Lewis Carroll]
```python
# The Hunting of the Snark, by Lewis Carroll
>>> quote = '''
"Just the place for a Snark!" the Bellman cried,
As he landed his crew with care;
Supporting each man on the top of the tide
By a finger entwined in his hair.
"Just the place for a Snark! I have said it twice:
That alone should encourage the crew.
Just the place for a Snark! I have said it thrice:
What I tell you three times is true."
'''
>>> quote.replace('Snark', '🐲')
...
'\n"Just the place for a 🐲!" the Bellman cried,\n As he landed his crew with care;\nSupporting each man on the top of the tide\n By a finger entwined in his hair.\n\n"Just the place for a 🐲! I have said it twice:\n That alone should encourage the crew.\nJust the place for a 🐲! I have said it thrice:\n What I tell you three times is true."\n'
>>> 'bookkeeper'.replace('kk', 'k k')
'book keeper'
```
[Lewis Carroll]: https://www.poetryfoundation.org/poets/lewis-carroll
[The Hunting of the Snark]: https://www.poetryfoundation.org/poems/43909/the-hunting-of-the-snark
[codecs]: https://docs.python.org/3/library/codecs.html
[common sequence operations]: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
[locale]: https://docs.python.org/3/library/locale.html#module-locale
[str methods]: https://docs.python.org/3/library/stdtypes.html#string-methods
[str-endswith]: https://docs.python.org/3/library/stdtypes.html#str.endswith
[str-replace]: https://docs.python.org/3/library/stdtypes.html#str.replace
[str-strip]: https://docs.python.org/3/library/stdtypes.html#str.strip
[str-title]: https://docs.python.org/3/library/stdtypes.html#str.title
[text sequence]: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
[unicode code points]: https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme
## Instructions
In this exercise you are helping your younger sister edit her paper for school. The teacher is looking for correct punctuation, grammar, and excellent word choice.
You have four tasks to clean up and modify strings.
## 1. Capitalize the title of the paper
Any good paper needs a properly formatted title.
Implement the function `capitalize_title(<title>)` which takes a title `str` as a parameter and capitalizes the first letter of each word.
This function should return a `str` in title case.
```python
>>> capitalize_title("my hobbies")
"My Hobbies"
```
## 2. Check if each sentence ends with a period
You want to make sure that the punctuation in the paper is perfect.
Implement the function `check_sentence_ending()` that takes `sentence` as a parameter. This function should return a `bool`.
```python
>>> check_sentence_ending("I like to hike, bake, and read.")
True
```
## 3. Clean up spacing
To make the paper look professional, unnecessary spacing needs to be removed.
Implement the function `clean_up_spacing()` that takes `sentence` as a parameter.
The function should remove extra whitespace at both the beginning and the end of the sentence, returning a new, updated sentence `str`.
```python
>>> clean_up_spacing(" I like to go on hikes with my dog. ")
"I like to go on hikes with my dog."
```
## 4. Replace words with a synonym
To make the paper _even better_, you can replace some of the adjectives with their synonyms.
Write the function `replace_word_choice()` that takes `sentence`, `old_word`, and `new_word` as parameters.
This function should replace all instances of the `old_word` with the `new_word`, and return a new `str` with the updated sentence.
```python
>>> replace_word_choice("I bake good cakes.", "good", "amazing")
"I bake amazing cakes."
```
## Source
### Created by
- @kimolivia
### Contributed to by
- @valentin-p
- @BethanyG

View file

@ -0,0 +1,43 @@
"""Functions to help edit essay homework using string manipulation."""
def capitalize_title(title):
"""Convert the first letter of each word in the title to uppercase if needed.
:param title: str - title string that needs title casing.
:return: str - title string in title case (first letters capitalized).
"""
return title.title()
def check_sentence_ending(sentence):
"""Check the ending of the sentence to verify that a period is present.
:param sentence: str - a sentence to check.
:return: bool - return True if punctuated correctly with period, False otherwise.
"""
return sentence.endswith(".")
def clean_up_spacing(sentence):
"""Verify that there isn't any whitespace at the start and end of the sentence.
:param sentence: str - a sentence to clean of leading and trailing space characters.
:return: str - a sentence that has been cleaned of leading and trailing space characters.
"""
return sentence.strip()
def replace_word_choice(sentence, old_word, new_word):
"""Replace a word in the provided sentence with a new one.
:param sentence: str - a sentence to replace words in.
:param old_word: str - word to replace.
:param new_word: str - replacement word.
:return: str - input sentence with new words in place of old words.
"""
return sentence.replace(old_word, new_word)

View file

@ -0,0 +1,98 @@
import unittest
import pytest
from string_methods import (capitalize_title,
check_sentence_ending,
clean_up_spacing,
replace_word_choice)
class LittleSistersEssayTest(unittest.TestCase):
@pytest.mark.task(taskno=1)
def test_capitalize_word(self):
actual_result = capitalize_title("canopy")
expected = "Canopy"
error_message = (f'Called capitalize_title("canopy"). '
f'The function returned "{actual_result}", '
f'but the tests expected "{expected}" for the title.')
self.assertEqual(actual_result, expected, msg=error_message)
@pytest.mark.task(taskno=1)
def test_capitalize_title(self):
actual_result = capitalize_title("fish are cold blooded")
expected = "Fish Are Cold Blooded"
error_message = (f'Called capitalize_title("fish are cold blooded"). '
f'The function returned "{actual_result}", '
f'but the tests expected "{expected}" for the title.')
self.assertEqual(actual_result, expected, msg=error_message)
@pytest.mark.task(taskno=2)
def test_sentence_ending(self):
actual_result = check_sentence_ending("Snails can sleep for 3 years.")
expected = True
error_message = (f'Called check_sentence_ending("Snails can sleep for 3 years."). '
f'The function returned {actual_result}, '
f'but the tests expected {expected} for a period ending.')
self.assertEqual(actual_result, expected, msg=error_message)
@pytest.mark.task(taskno=2)
def test_sentence_ending_without_period(self):
actual_result = check_sentence_ending("Fittonia are nice")
expected = False
error_message = (f'Called check_sentence_ending("Fittonia are nice"). '
f'The function returned {actual_result}, '
f'but the tests expected {expected} for a period ending.')
self.assertEqual(actual_result, expected, msg=error_message)
@pytest.mark.task(taskno=3)
def test_remove_extra_spaces_only_start(self):
actual_result = clean_up_spacing(" A rolling stone gathers no moss")
expected = "A rolling stone gathers no moss"
error_message = (f'Called clean_up_spacing(" A rolling stone gathers no moss"). '
f'The function returned "{actual_result}", '
f'but the tests expected "{expected}" as a cleaned string.')
self.assertEqual(actual_result, expected, msg=error_message)
@pytest.mark.task(taskno=3)
def test_remove_extra_spaces(self):
actual_result = clean_up_spacing(" Elephants can't jump. ")
expected = "Elephants can't jump."
error_message = ("Called clean_up_spacing(\" Elephants can't jump. \")"
f'The function returned "{actual_result}", '
f'but the tests expected "{expected}" as a cleaned string.')
self.assertEqual(actual_result, expected, msg=error_message)
@pytest.mark.task(taskno=4)
def test_replace_word_choice(self):
actual_result = replace_word_choice("Animals are cool.", "cool", "awesome")
expected = "Animals are awesome."
error_message = ('Called replace_word_choice("Animals are cool.", "cool", "awesome"). '
f'The function returned "{actual_result}", '
f'but the tests expected "{expected}" after the word replacement.')
self.assertEqual(actual_result, expected, msg=error_message)
@pytest.mark.task(taskno=4)
def test_replace_word_not_exist(self):
actual_result = replace_word_choice("Animals are cool.", "small", "tiny")
expected = "Animals are cool."
error_message = ('Called replace_word_choice("Animals are cool.", "small", "tiny"). '
f'The function returned "{actual_result}", '
f'but the tests expected "{expected}", because the word '
'to be replaced is not in the sentence.')
self.assertEqual(actual_result, expected, msg=error_message)