Skip to content

Commit b3f23ce

Browse files
meatball133BethanyG
authored andcommitted
Fixed spacing
1 parent 906676a commit b3f23ce

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

exercises/practice/scrabble-score/.approaches/dictionary/content.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ This code starts with defining a constant LETTER_SCORES as a dictionary ([concep
1616
Then the `score` function is defined, which takes a `<word>` as an argument.
1717

1818
The function returns the total score for the word using the built-in function [`sum`][sum].
19-
Sum is passed a [generator expression][generator-expression] that iterates over the letters in the word, looking up each score in LETTER_SCORES.
19+
Sum is passed a [generator expression][generator-expression] that iterates over the letters in the word, looking up each score in LETTER_SCORES.
2020
The generator expression produces the score values on the fly.
2121
This means that it doesn't use memory to store all the values from LETTER_SCORES.
22-
Instead, each value is looked up as needed by `sum`.
22+
Instead, each value is looked up as needed by `sum`.
2323

2424
Within the generator expression, the word is converted from lower to uppercase.
2525
Each letter of the word is looked up in LETTER_SCORES, and the score value is yielded to `sum` as `sum` iterates over the expression.
@@ -42,11 +42,10 @@ LETTER_SCORES = {
4242

4343
def score(word):
4444
return sum(next(score for score, letters in LETTER_SCORES.items() if character in letters) for character in word.upper())
45-
45+
```
4646

4747
However, transposing the dictionary so that the keys are the score and the values are the letters requires more computational calculation (_a loop within a loop_) and is harder to read.
48-
Therefore, arranging the dictionary by letter is both more efficient and easier to understand.
49-
48+
Therefore, arranging the dictionary by letter is both more efficient and easier to understand.
5049

5150
[dictionary]: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
5251
[generator-expression]: https://peps.python.org/pep-0289/

exercises/practice/scrabble-score/.approaches/enum/content.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Scrabble(IntEnum):
1414

1515
def score(word):
1616
return sum(Scrabble[character] for character in word.upper())
17+
```
1718

1819
This approach uses an [`Enum`][enum] to define the score of each letter.
1920
An [`Enum`][enum] (_also known as an **enumeration**_) is an object with named attributes assigned unique values.
@@ -24,7 +25,7 @@ Values can be accessed via index syntax using the member name (_similar to how a
2425
The `enum` module was added to python standard library (_also known as stdlib_) in Python 3.4.
2526

2627
This approach uses an [`IntEnum`][int-enum].
27-
An `IntEnum` is very similar to an `Enum`, but restricts assigned values to `int`s.
28+
An `IntEnum` is very similar to an `Enum`, but restricts assigned values to `int`s.
2829
This allows the `IntEnum` to act as a collection of integers.
2930
In fact, `IntEnum`s are considered subclasses of `int`s.
3031

@@ -35,15 +36,16 @@ The `IntEnum` subclass is defined by using the [`class`][classes] keyword, follo
3536

3637
```python
3738
class ClassName(IntEnum):
39+
```
3840

3941
Member names are declared as constants (ALL CAPS) and assigned values using the `=` operator.
4042

41-
This approach works by creating all the uppercase letters as members with their values being the score.
43+
This approach works by creating all the uppercase letters as members with their values being the score.
4244
After the `IntEnum` is defined, the `score` function is defined.
4345

4446
The `score` function takes a word as an argument.
4547
The `score` function uses the same [generator expression][generator-expression] as the [dictionary approach][dictionary-approach], but with a slight modification.
46-
Instead of looking up the value in a _dictionary_, it looks up the `InEnum` class member value.
48+
Instead of looking up the value in a _dictionary_, it looks up the `InEnum` class member value.
4749

4850
[classes]: https://docs.python.org/3/tutorial/classes.html
4951
[enum]: https://docs.python.org/3/library/enum.html

exercises/practice/scrabble-score/.approaches/introduction.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ LETTER_SCORES = {
2525

2626
def score(word):
2727
return sum(LETTER_SCORES[letter] for letter in word.upper())
28+
```
2829

2930
For more information, check the [Dictionary Approach][dictionary-approach].
3031

@@ -38,15 +39,15 @@ KEYS = "AEIOULNRSTDGBCMPFHVWYKJXQZ"
3839
SCORES = [1] * 10 + [2] * 2 + [3] * 4 + [4] * 5 + [5] * 1 + [8] * 2 +[10] * 2
3940

4041
def score(word):
41-
return sum(SCORES[KEYS.index(letter] for letter in word.upper())
42+
return sum(SCORES[KEYS.index(letter)] for letter in word.upper())
4243
```
4344

4445
For more information, check the [Two Sequences Approach][two-sequences-approach].
4546

4647
## Approach: Enum
4748

4849
Using an `Enum` is is short and easy to read.
49-
Although creating an `Enum` can be more complicated since it uses OOP (object oriented programming).
50+
Although creating an `Enum` can be more complicated since it uses OOP (object oriented programming).
5051

5152
```python
5253
from enum import IntEnum
@@ -84,8 +85,9 @@ LETTERS_OF_SCORE = (
8485
)
8586

8687
def score(word):
87-
return sum(score for character in word.upper() for
88-
letters, score in LETTERS_OF_SCORE if character in letters)
88+
return sum(score for character in word.upper() for
89+
letters, score in LETTERS_OF_SCORE if character in letters)
90+
```
8991

9092
For more information, check the [Nested Tuple Approach][nested-tuple-approach].
9193

exercises/practice/scrabble-score/.approaches/nested-tuple/content.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ LETTERS_OF_SCORE = (
1212
)
1313

1414
def score(word):
15-
return sum(score for character in word.upper() for
15+
return sum(score for character in word.upper() for
1616
letters, score in LETTERS_OF_SCORE if character in letters)
17+
```
1718

18-
The code starts with defining a constant, LETTERS_OF_SCORE as a [`tuple`][tuple] of tuples (_also known as a nested tuple_).
19+
The code starts with defining a constant, `LETTERS_OF_SCORE` as a [`tuple`][tuple] of tuples (_also known as a nested tuple_).
1920
Inside of the inner tuples are 2 values, the first value is a string of letters and the second value is the score for those letters.
2021

2122
Next, the `score` function is defined, taking a word as an argument.
@@ -29,6 +30,6 @@ You can read more about unpacking in the [concept:python/unpacking-and-multiple-
2930

3031
Then the code checks if the character is in the unpacked letters and if it is we return its score.
3132

32-
[tuple]: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
3333
[generator-expression]: https://peps.python.org/pep-0289/
3434
[for-loop]: https://realpython.com/python-for-loop/
35+
[tuple]: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

exercises/practice/scrabble-score/.approaches/two-sequences/content.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SCORES = [1] * 10 + [2] * 2 + [3] * 4 + [4] * 5 + [5] * 1 + [8] * 2 + [10] * 2
66

77
def score(word):
88
return sum(SCORES[KEYS.index(letter)] for letter in word.upper())
9+
```
910

1011
This approach uses a string and a [list][list], both of which are [sequence][sequence] types.
1112
The code begins by defining a string constant with letters.

0 commit comments

Comments
 (0)