-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Strings Concept Exercise Re-Write #2433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
45aed7b
Renamed processing-logs to little-sisiters-vocab.
BethanyG 6fc2715
Rewrote hints.md, instructions.md, and introduction.md.
BethanyG 4120d11
Rewrote config.json, design.md, and exemplar.py.
BethanyG 88b7c2d
Rewrote stub file and strings_test.py
BethanyG 1194ffb
Corrected malformed JSON.
BethanyG 27f283b
Added rewritten exercise to config.json.
BethanyG 5c2839a
Update exercises/concept/little-sisters-vocab/.docs/introduction.md
BethanyG 985934c
Update exercises/concept/little-sisters-vocab/.docs/introduction.md
BethanyG 5bd3eab
Update exercises/concept/little-sisters-vocab/.docs/hints.md
BethanyG d3f7063
Added task annotations to test file and added newline to stub and exe…
BethanyG 437bb8a
Update exercises/concept/little-sisters-vocab/.docs/instructions.md
BethanyG 316f53b
Update config.json
BethanyG 1c629bc
Update exercises/concept/little-sisters-vocab/.docs/hints.md
BethanyG 324d7f7
Apply suggestions from code review
BethanyG 30f1372
Update exercises/concept/little-sisters-vocab/.docs/introduction.md
BethanyG 9593a8f
Update exercises/concept/little-sisters-vocab/.docs/introduction.md
BethanyG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Hints | ||
|
||
## General | ||
|
||
- The [Python Docs Tutorial for strings][python-str-doc] has an overview of the Python `str` type. | ||
- String methods [<str>.join()][str-join] and [<str>.split()][str-split] ar very helpful when processing strings. | ||
- The [Python Docs on Sequence Types][common sequence operations] has a rundown of operations common to all sequences, including `strings`, `lists`, `tuples`, and `ranges`. | ||
|
||
There's four activities in the assignment, each with a set of text or words to work with. | ||
|
||
## 1. Add a prefix to a word | ||
|
||
- Small strings can be concatenated with the `+` operator. | ||
|
||
## 2. Add prefixes to word groups | ||
|
||
- Believe it or not, `<str>.join()` is all you need. | ||
- Like `<str>.split()`, `<str>.join()` can take an arbitrary-length string, made up of any unicode code points. | ||
|
||
## 3. Remove a suffix from a word | ||
|
||
- Strings can be both indexed and sliced from either the left (starting at 0) or the right (starting at -1). | ||
- If you want the last code point of an arbitrary-length string, you can use [-1]. | ||
- The last three letters in a string can be "sliced off" using a negative index. e.g. 'beautiful'[:-3] == 'beauti' | ||
|
||
## 4. Extract and transform a word | ||
|
||
- Using `<str>.split()` returns a list of strings broken on white space. | ||
- `lists` are sequences, and can be indexed. | ||
- `<str>.split()` can be direcly indexed. e.g. `'Exercism rocks!'.split()[0] == 'Exercism'` | ||
- Be careful of punctuation! Periods can be removed via slice: `'dark.'[:-1] == 'dark'` | ||
|
||
[python-str-doc]: https://docs.python.org/3/tutorial/introduction.html#strings | ||
|
||
[common sequence operations]: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str | ||
[str-join]: https://docs.python.org/3/library/stdtypes.html#str.join | ||
[str-split]: https://docs.python.org/3/library/stdtypes.html#str.split |
69 changes: 69 additions & 0 deletions
69
exercises/concept/little-sisters-vocab/.docs/instructions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Instructions | ||
|
||
You're helping your younger sister with her English vocabulary homework, which she's finding very tedious. Her class is learning to create new words by adding _prefixes_ and _suffixes_. Given a set of words, the teacher is looking for correctly transformed words with correct spelling by adding the prefix to the beginning or the suffix to the ending. | ||
|
||
There's four activities in the assignment, each with a set of text or words to work with. | ||
|
||
## 1. Add a prefix to a word | ||
|
||
One of the most common prefixes in English is `un`, meaning "not". In this activity, your sister needs to make negative, or "not" words by adding `un` to them. | ||
|
||
Implement the `add_prefix_un()` function that takes `word` as a parameter and returns a new `un` prefixed word: | ||
|
||
```python | ||
>>> add_prefix_un("happy") | ||
'unhappy' | ||
|
||
>>> add_prefix_un("manageable") | ||
'unmanagable' | ||
``` | ||
|
||
## 2. Add prefixes to word groups | ||
|
||
There are four more common prefixes that your sister's class is studying: `en` (_meaning to 'put into' or 'cover with'_), `pre` (_meaning 'before' or 'forward'_), `auto` (_meaning 'self' or 'same'_), and `inter` (_meaning 'between' or 'among'_). In this exercise, the class is creating groups of vocabulary words using these prefixes, so they can be studied together. Each prefix comes in a list with common words it's used with. The students need to apply the prefix and produce a string that shows the prefix applied to all of the words. | ||
|
||
Implement the `make_word_groups()` function that takes a `vocab_words` as a parameter in the following form: [`<prefix>`, `word_1`, `word_2` .... `word_n`], and returns a string with the prefix applied to each word that looks like `'<prefix> :: <prefix><word_1> :: <prefix><word_2> :: <prefix><word_n>'`. | ||
|
||
```python | ||
>>> make_word_groups(['en', 'close', 'joy', 'lighten']) | ||
'en :: enclose :: enjoy :: enlighten' | ||
|
||
>>> make_word_groups(['pre', 'serve', 'dispose', 'position']) | ||
'pre :: preserve :: predispose :: preposition' | ||
|
||
>> make_word_groups(['auto', 'didactic', 'graph', 'mate']) | ||
'auto :: autodidactic :: autograph :: automate' | ||
|
||
>>> make_word_groups(['inter', 'twine', 'connected', 'dependant']) | ||
'inter :: intertwine :: interconnected :: interdependant' | ||
``` | ||
|
||
## 3. Remove a suffix from a word | ||
|
||
`ness` is a common suffix that means _'state of being'_. In this activity, your sister needs to find the original root word by removing the `ness` suffix. But of course there are pesky spelling rules: If the root word originally ended in a consonant followed by a 'y', then the 'y' was changed to to 'i'. Removing 'ness' needs to restore the 'y' in those root words. e.g. `happiness` --> `happi` --> `happy`. | ||
|
||
Implement the `remove_suffix_ness()` function that takes in a word and returns the root word without the `ness` suffix. | ||
|
||
```python | ||
>>> remove_suffix_ness("heaviness") | ||
'heavy' | ||
|
||
>>> remove_suffix_ness("sadness") | ||
'sad' | ||
``` | ||
|
||
## 4. Extract and transform a word | ||
|
||
Suffixes are often used to change the part of speech a word has. A common practice in English is "verbing" or "verbifying" -- where a adjective _becomes_ a verb by adding an `en` suffix. | ||
|
||
In this task, your sister is going to practice "verbing" words by extracting an adjective from a sentence and turning it into a verb. Fortunately, all the words that need to be transformed here are "regular" - they don't need spelling changes to add the suffix. | ||
|
||
Implement the `noun_to_verb()` function that takes two parameters. A `sentence` using the vocabulary word, and the `index` of the word, once that sentence is split apart. The function should return the extracted adjective as a verb. | ||
|
||
```python | ||
>>> noun_to_verb('I need to make that bright.', -1 ) | ||
'brighten' | ||
|
||
>>> noun_to_verb('It got dark as the sun set.', 2) | ||
'darken' | ||
``` |
215 changes: 215 additions & 0 deletions
215
exercises/concept/little-sisters-vocab/.docs/introduction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
# Introduction | ||
|
||
A `str` in Python is an [immutable sequence][text sequence] of [Unicode code points][unicode code points]. | ||
These could include letters, diacritical marks, positioning characters, numbers, currecy symbols, emoji, punctuation, space and line break characters, and more. | ||
Being immutable, a `str` object's value in memory doesn't change; methods that appear to modify a string return a new copy or instance of `str`. | ||
|
||
A `str` literal can be declared via single `'` or double `"` quotes. The escape `\` character is available as needed. | ||
|
||
```python | ||
|
||
>>> single_quoted = 'These allow "double quoting" without "escape" characters.' | ||
|
||
>>> double_quoted = "These allow embedded 'single quoting', so you don't have to use an 'escape' character". | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> escapes = 'If needed, a \'slash\' can be used as an escape character within a string when switching quote styles won\'t work.' | ||
``` | ||
|
||
Multi-line strings are declared with `'''` or `"""`. | ||
|
||
```python | ||
>>> triple_quoted = '''Three single quotes or "double quotes" in a row allow for multi-line string literals. | ||
Line break characters, tabs and other whitespace are fully supported. | ||
|
||
You\'ll most often encounter these as "doc strings" or "doc tests" written just below the first line of a function or class definition. | ||
They\'re often used with auto documentation ✍ tools. | ||
''' | ||
``` | ||
|
||
Strings can be concatenated using the `+` operator. | ||
This method should be used sparingly, as it is not very performant or easily maintained. | ||
|
||
```python | ||
language = "Ukrainian" | ||
number = "nine" | ||
word = "девять" | ||
|
||
sentence = word + " " + "means" + " " + number + " in " + language + "." | ||
|
||
>>> print(sentence) | ||
... | ||
"девять means nine in Ukrainian." | ||
``` | ||
|
||
If a `list`, `tuple`, `set` or other collection of individual strings needs to be combined into a single `str`, [`<str>.join(<iterable>)`][str-join], is a better option: | ||
|
||
```python | ||
# str.join() makes a new string from the iterables elements. | ||
>>> chickens = ["hen", "egg", "rooster"] | ||
>>> ' '.join(chickens) | ||
'hen egg rooster' | ||
|
||
# Any string can be used as the joining element. | ||
>>> ' :: '.join(chickens) | ||
'hen :: egg :: rooster' | ||
|
||
>>> ' 🌿 '.join(chickens) | ||
'hen 🌿 egg 🌿 rooster' | ||
``` | ||
|
||
Code points within a `str` can be referenced by 0-based index number from the left: | ||
|
||
```python | ||
creative = '창의적인' | ||
|
||
>>> creative[0] | ||
'창' | ||
|
||
>>> creative[2] | ||
'적' | ||
|
||
>>> creative[3] | ||
'인' | ||
``` | ||
|
||
Indexing also works from the right, starting with `-1`: | ||
|
||
```python | ||
creative = '창의적인' | ||
|
||
>>> creative[-4] | ||
'창' | ||
|
||
>>> creative[-2] | ||
'적' | ||
|
||
>>> creative[-1] | ||
'인' | ||
|
||
``` | ||
|
||
There is no separate “character” or "rune" type in Python, so indexing a string produces a new `str` of length 1: | ||
|
||
```python | ||
|
||
>>> website = "exercism" | ||
>>> type(website[0]) | ||
<class 'str'> | ||
|
||
>>> len(website[0]) | ||
1 | ||
|
||
>>> website[0] == website[0:1] == 'e' | ||
True | ||
``` | ||
|
||
Substrings can be selected via _slice notation_, using [`<str>[<start>:stop:<step>]`][common sequence operations] to produce a new string. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Results exclude the `stop` index. | ||
If no `start` is given, the starting index will be 0. | ||
If no `stop` is given, the `stop` index will be the end of the string. | ||
|
||
```python | ||
moon_and_stars = '🌟🌟🌙🌟🌟⭐' | ||
sun_and_moon = sun_and_moon = '🌞🌙🌞🌙🌞🌙🌞🌙🌞' | ||
|
||
>>> moon_and_stars[1:4] | ||
'🌟🌙🌟' | ||
|
||
>>> moon_and_stars[:3] | ||
'🌟🌟🌙' | ||
|
||
>>> moon_and_stars[3:] | ||
'🌟🌟⭐' | ||
|
||
>>> moon_and_stars[:-1] | ||
'🌟🌟🌙🌟🌟' | ||
|
||
>>> moon_and_stars[:-3] | ||
'🌟🌟🌙' | ||
|
||
>>> sun_and_moon[::2] | ||
'🌞🌞🌞🌞🌞' | ||
|
||
>>> sun_and_moon[:-2:2] | ||
'🌞🌞🌞🌞' | ||
|
||
>>> sun_and_moon[1:-1:2] | ||
'🌙🌙🌙🌙' | ||
|
||
|
||
Strings can also be broken into smaller strings via [`<str>.split(<separator>)`][str-split], which will return a `list` of substrings. | ||
The list can then be further indexed or split, if needed. | ||
Using `<str>.split()` without any arguments will split the string on whitespace. | ||
|
||
```python | ||
>>> cat_ipsum = "Destroy house in 5 seconds mock the hooman." | ||
>>> cat_ipsum.split() | ||
... | ||
['Destroy', 'house', 'in', '5', 'seconds', 'mock', 'the', 'hooman.'] | ||
|
||
|
||
>>> cat_ipsum.split()[-1] | ||
'hooman.' | ||
|
||
|
||
>>> cat_words = "feline, four-footed, ferocious, furry" | ||
>>> cat_words.split(',') | ||
... | ||
['feline, four-footed, ferocious, furry'] | ||
``` | ||
|
||
Seperators for `<str>.split()` can be more than one character. The whole string will be used for matching. | ||
|
||
```python | ||
|
||
>>> colors = """red, | ||
orange, | ||
green, | ||
purple, | ||
yellow""" | ||
|
||
>>> colors.split(',\n') | ||
['red', 'orange', 'green', 'purple', 'yellow'] | ||
``` | ||
|
||
Strings support all [common sequence operations][common sequence operations]. | ||
Individual code points can be iterated through in a loop via `for item in <str>`. | ||
Indexes _with_ items can be iterated through in a loop via `for index, item in enumerate(<str>)` | ||
|
||
```python | ||
|
||
>>> exercise = 'လေ့ကျင့်' | ||
|
||
# Note that there are more code points than percieved glyphs or characters | ||
>>> for code_point in exercise: | ||
... print(code_point) | ||
... | ||
လ | ||
ေ | ||
့ | ||
က | ||
ျ | ||
င | ||
် | ||
့ | ||
|
||
# Using enumerate will give both the value and index position of each element. | ||
>>> for index, code_point in enumerate(exercise): | ||
... print(index, ": ", code_point) | ||
... | ||
0 : လ | ||
1 : ေ | ||
2 : ့ | ||
3 : က | ||
4 : ျ | ||
5 : င | ||
6 : ် | ||
7 : ့ | ||
``` | ||
|
||
[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 | ||
[common sequence operations]: https://docs.python.org/3/library/stdtypes.html#common-sequence-operations | ||
[str-split]: https://docs.python.org/3/library/stdtypes.html#str.split | ||
[str-join]: https://docs.python.org/3/library/stdtypes.html#str.join | ||
[str-constructor]: https://docs.python.org/3/library/stdtypes.html#str |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"blurb": "Learn about strings by helping your little sister with her vocabulary homework.", | ||
"icon": "two-fer", | ||
"authors": ["aldraco", "bethanyg"], | ||
"files": { | ||
"solution": ["strings.py"], | ||
"test": ["strings_test.py"], | ||
"exemplar": [".meta/exemplar.py"] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.