|
| 1 | +# Little Sister's Essay |
| 2 | + |
| 3 | +Welcome to Little Sister's Essay on Exercism's Python Track. |
| 4 | +If you need help running the tests or submitting your code, check out `HELP.md`. |
| 5 | +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +The `str` class offers [many useful methods][str methods] for working with and composing strings. |
| 10 | +These include searching, cleaning, splitting, transforming, translating, and many other techniques. |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +Strings can be iterated through using `for item in <str>` or `for index, item in enumerate(<str>)` syntax. |
| 15 | +They can be concatenated using the `+` operator or via `<string>.join(<iterable>)` and implement all [common sequence operations][common sequence operations]. |
| 16 | + |
| 17 | +Strings are _immutable_, meaning the value of a `str` object in memory cannot change. |
| 18 | +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`. |
| 19 | + |
| 20 | +Following is a small selection of Python string methods. |
| 21 | +For a complete list, see the [str class][str methods] in the Python docs. |
| 22 | + |
| 23 | + |
| 24 | +[`<str>.title()`][str-title] parses a string and capitalizes the first "character" of each "word" found. |
| 25 | +In Python, this is very dependent on the [language codec][codecs] used and how the particular language represents words and characters. |
| 26 | +There may also be [locale][locale] rules in place for a language or character set. |
| 27 | + |
| 28 | + |
| 29 | +```python |
| 30 | +man_in_hat_th = 'ผู้ชายใส่หมวก' |
| 31 | +man_in_hat_ru = 'мужчина в шляпе' |
| 32 | +man_in_hat_ko = '모자를 쓴 남자' |
| 33 | +man_in_hat_en = 'the man in the hat.' |
| 34 | + |
| 35 | +>>> man_in_hat_th.title() |
| 36 | +'ผู้ชายใส่หมวก' |
| 37 | + |
| 38 | +>>> man_in_hat_ru.title() |
| 39 | +'Мужчина В Шляпе' |
| 40 | + |
| 41 | +>>> man_in_hat_ko.title() |
| 42 | +'모자를 쓴 남자' |
| 43 | + |
| 44 | +>> man_in_hat_en.title() |
| 45 | +'The Man In The Hat.' |
| 46 | +``` |
| 47 | + |
| 48 | +[`<str>.endswith(<suffix>)`][str-endswith] returns `True` if the string ends with `<suffix>`, `False` otherwise. |
| 49 | + |
| 50 | + |
| 51 | +```python |
| 52 | +>>> 'My heart breaks. 💔'.endswith('💔') |
| 53 | +True |
| 54 | + |
| 55 | +>>> 'cheerfulness'.endswith('ness') |
| 56 | +True |
| 57 | + |
| 58 | +# Punctuation is part of the string, so needs to be included in any endswith match. |
| 59 | +>>> 'Do you want to 💃?'.endswith('💃') |
| 60 | +False |
| 61 | + |
| 62 | +>> 'The quick brown fox jumped over the lazy dog.'.endswith('dog') |
| 63 | +False |
| 64 | +``` |
| 65 | + |
| 66 | +[`<str>.strip(<chars>)`][str-strip] returns a copy of the `str` with leading and trailing `<chars>` removed. |
| 67 | +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. |
| 68 | + If nothing is specified for `<chars>`, all combinations of whitespace code points will be removed. |
| 69 | + |
| 70 | + |
| 71 | + ```python |
| 72 | +# This will remove "https://", because it can be formed from "/stph:". |
| 73 | +>>> 'https://unicode.org/emoji/'.strip('/stph:') |
| 74 | +'unicode.org/emoji' |
| 75 | + |
| 76 | +# Removal of all whitespace from both ends of the str. |
| 77 | +>>> ' 🐪🐪🐪🌟🐪🐪🐪 '.strip() |
| 78 | +'🐪🐪🐪🌟🐪🐪🐪' |
| 79 | + |
| 80 | +>>> justification = 'оправдание' |
| 81 | +>>> justification.strip('еина') |
| 82 | +'оправд' |
| 83 | + |
| 84 | +# Prefix and suffix in one step. |
| 85 | +>>> 'unaddressed'.strip('dnue') |
| 86 | +'address' |
| 87 | + |
| 88 | +>>> ' unaddressed '.strip('dnue ') |
| 89 | +'address' |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +[`<str>.replace(<substring>, <replacement substring>)`][str-replace] returns a copy of the string with all occurrences of `<substring>` replaced with `<replacement substring>`. |
| 94 | + |
| 95 | +The quote used below is from [The Hunting of the Snark][The Hunting of the Snark] by [Lewis Carroll][Lewis Carroll] |
| 96 | + |
| 97 | +```python |
| 98 | +# The Hunting of the Snark, by Lewis Carroll |
| 99 | +>>> quote = ''' |
| 100 | +"Just the place for a Snark!" the Bellman cried, |
| 101 | + As he landed his crew with care; |
| 102 | +Supporting each man on the top of the tide |
| 103 | + By a finger entwined in his hair. |
| 104 | +
|
| 105 | +"Just the place for a Snark! I have said it twice: |
| 106 | + That alone should encourage the crew. |
| 107 | +Just the place for a Snark! I have said it thrice: |
| 108 | + What I tell you three times is true." |
| 109 | +''' |
| 110 | + |
| 111 | +>>> quote.replace('Snark', '🐲') |
| 112 | +... |
| 113 | +'\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' |
| 114 | + |
| 115 | +>>> 'bookkeeper'.replace('kk', 'k k') |
| 116 | +'book keeper' |
| 117 | +``` |
| 118 | + |
| 119 | +[Lewis Carroll]: https://www.poetryfoundation.org/poets/lewis-carroll |
| 120 | +[The Hunting of the Snark]: https://www.poetryfoundation.org/poems/43909/the-hunting-of-the-snark |
| 121 | +[codecs]: https://docs.python.org/3/library/codecs.html |
| 122 | +[common sequence operations]: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str |
| 123 | +[locale]: https://docs.python.org/3/library/locale.html#module-locale |
| 124 | +[str methods]: https://docs.python.org/3/library/stdtypes.html#string-methods |
| 125 | +[str-endswith]: https://docs.python.org/3/library/stdtypes.html#str.endswith |
| 126 | +[str-replace]: https://docs.python.org/3/library/stdtypes.html#str.replace |
| 127 | +[str-strip]: https://docs.python.org/3/library/stdtypes.html#str.strip |
| 128 | +[str-title]: https://docs.python.org/3/library/stdtypes.html#str.title |
| 129 | +[text sequence]: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str |
| 130 | +[unicode code points]: https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme |
| 131 | + |
| 132 | +## Instructions |
| 133 | + |
| 134 | +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. |
| 135 | + |
| 136 | +You have four tasks to clean up and modify strings. |
| 137 | + |
| 138 | +## 1. Capitalize the title of the paper |
| 139 | + |
| 140 | +Any good paper needs a properly formatted title. |
| 141 | +Implement the function `capitalize_title(<title>)` which takes a title `str` as a parameter and capitalizes the first letter of each word. |
| 142 | +This function should return a `str` in title case. |
| 143 | + |
| 144 | + |
| 145 | +```python |
| 146 | +>>> capitalize_title("my hobbies") |
| 147 | +"My Hobbies" |
| 148 | +``` |
| 149 | + |
| 150 | +## 2. Check if each sentence ends with a period |
| 151 | + |
| 152 | +You want to make sure that the punctuation in the paper is perfect. |
| 153 | +Implement the function `check_sentence_ending()` that takes `sentence` as a parameter. This function should return a `bool`. |
| 154 | + |
| 155 | + |
| 156 | +```python |
| 157 | +>>> check_sentence_ending("I like to hike, bake, and read.") |
| 158 | +True |
| 159 | +``` |
| 160 | + |
| 161 | +## 3. Clean up spacing |
| 162 | + |
| 163 | +To make the paper look professional, unnecessary spacing needs to be removed. |
| 164 | +Implement the function `clean_up_spacing()` that takes `sentence` as a parameter. |
| 165 | +The function should remove extra whitespace at both the beginning and the end of the sentence, returning a new, updated sentence `str`. |
| 166 | + |
| 167 | + |
| 168 | +```python |
| 169 | +>>> clean_up_spacing(" I like to go on hikes with my dog. ") |
| 170 | +"I like to go on hikes with my dog." |
| 171 | +``` |
| 172 | + |
| 173 | +## 4. Replace words with a synonym |
| 174 | + |
| 175 | +To make the paper _even better_, you can replace some of the adjectives with their synonyms. |
| 176 | +Write the function `replace_word_choice()` that takes `sentence`, `old_word`, and `new_word` as parameters. |
| 177 | +This function should replace all instances of the `old_word` with the `new_word`, and return a new `str` with the updated sentence. |
| 178 | + |
| 179 | + |
| 180 | +```python |
| 181 | +>>> replace_word_choice("I bake good cakes.", "good", "amazing") |
| 182 | +"I bake amazing cakes." |
| 183 | +``` |
| 184 | + |
| 185 | +## Source |
| 186 | + |
| 187 | +### Created by |
| 188 | + |
| 189 | +- @kimolivia |
| 190 | + |
| 191 | +### Contributed to by |
| 192 | + |
| 193 | +- @valentin-p |
| 194 | +- @BethanyG |
0 commit comments