Skip to content

Commit f8dd8a9

Browse files
committed
Cleaned up typos and changed toss to throw.
1 parent d54b2d1 commit f8dd8a9

File tree

7 files changed

+52
-52
lines changed

7 files changed

+52
-52
lines changed

exercises/practice/darts/.approaches/booleans-as-ints/content.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def score(x_coord, y_coord):
99

1010

1111
In Python, the [Boolean values `True` and `False` are _subclasses_ of `int`][bools-as-ints] and can be interpreted as `0` (False) and `1` (True) in a mathematical context.
12-
This approach leverages that interpretation by checking which areas the toss falls into and multiplying each Boolean `int` by a scoring multiple.
13-
For example, a toss that lands on the 25 (_or 5 if using `math.sqrt(x**2 + y**2)`_) circle should have a score of 5:
12+
This approach leverages that interpretation by checking which areas the throw falls into and multiplying each Boolean `int` by a scoring multiple.
13+
For example, a throw that lands on the 25 (_or 5 if using `math.sqrt(x**2 + y**2)`_) circle should have a score of 5:
1414

1515
```python
1616
>>> (False)*5 + (True)*4 + (True)*1
@@ -30,7 +30,7 @@ def score(x_coord, y_coord):
3030
```
3131

3232
Beyond that recommendation, the terseness of this approach might be harder to reason about or decode — especially if a programmer is coming from a programming langauge that does not treat Boolean values as `ints`.
33-
Despite the "radius" variable name, is also more difficult to relate the scoring "rings" of the Dartboard to the values being checked and calculated in the `return` statement.
33+
Despite the "radius" variable name, it is also more difficult to relate the scoring "rings" of the Dartboard to the values being checked and calculated in the `return` statement.
3434
If using this code in a larger program, it would be strongly recommended that a docstring be provided to explain the Dartboard rings, scoring rules, and the corresponding scores.
3535

3636
[bools-as-ints]: https://docs.python.org/3/library/stdtypes.html#boolean-type-bool

exercises/practice/darts/.approaches/config.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@
88
"uuid": "7d78f598-8b4c-4f7f-89e1-e8644e934a4c",
99
"slug": "if-statements",
1010
"title": "Use If Statements",
11-
"blurb": "Use if-statements to check scoring boundaries for a dart toss.",
11+
"blurb": "Use if-statements to check scoring boundaries for a dart throw.",
1212
"authors": ["bethanyg"]
1313
},
1414
{
1515
"uuid": "f8f5533a-09d2-4b7b-9dec-90f268bfc03b",
1616
"slug": "tuple-and-loop",
1717
"title": "Use a Tuple & Loop through Scores",
18-
"blurb": "Score the Dart toss by looping through a tuple of scores.",
18+
"blurb": "Score the Dart throw by looping through a tuple of scores.",
1919
"authors": ["bethanyg"]
2020
},
2121
{
2222
"uuid": "a324f99e-15bb-43e0-9181-c1652094bc4f",
2323
"slug": "match-case",
2424
"title": "Use Structural Pattern Matching ('Match-Case')",
25-
"blurb": "Use a Match-Case (Structural Pattern Matching) to score the dart toss.)",
25+
"blurb": "Use a Match-Case (Structural Pattern Matching) to score the dart throw.)",
2626
"authors": ["bethanyg"]
2727
},
2828
{
2929
"uuid": "966bd2dd-c4fd-430b-ad77-3a304dedd82e",
3030
"slug": "dict-and-generator",
3131
"title": "Use a Dictionary with a Generator Expression",
32-
"blurb": "Use a generator expression looping over a scoring dictionary, getting the max score for the dart toss.",
32+
"blurb": "Use a generator expression looping over a scoring dictionary, getting the max score for the dart throw.",
3333
"authors": ["bethanyg"]
3434
},
3535
{
3636
"uuid": "5b087f50-31c5-4b84-9116-baafd3a30ed6",
3737
"slug": "booleans-as-ints",
3838
"title": "Use Boolean Values as Integers",
39-
"blurb": "Use True and False as integer values to calculate the score of the dart toss.",
39+
"blurb": "Use True and False as integer values to calculate the score of the dart throw.",
4040
"authors": ["bethanyg"]
4141
},
4242
{

exercises/practice/darts/.approaches/dict-and-dict-get/content.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ However, this approach is **not** interpreting Booleans as integers and is inste
2222
3. Duplicate keys _overwrite_ existing keys.
2323
If the first key is `True` and the third key is `True`, the _value_ from the third key will overwrite the value from the first key.
2424

25-
Finally, the `return` line uses [`dict.get()`][dict-get] to `return` a default value of 0 when a toss is outside the existing circle radii.
25+
Finally, the `return` line uses [`dict.get()`][dict-get] to `return` a default value of 0 when a throw is outside the existing circle radii.
2626
To see this in action, you can view this code on [Python Tutor][dict-get-python-tutor].
2727

2828

exercises/practice/darts/.approaches/dict-and-generator/content.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,50 @@
22

33
```python
44
def score(x_coord, y_coord):
5-
toss = x_coord**2 + y_coord**2
5+
throw = x_coord**2 + y_coord**2
66
rules = {1: 10, 25: 5, 100: 1, 200: 0}
77

88
return max(point for distance, point in
9-
rules.items() if toss <= distance)
9+
rules.items() if throw <= distance)
1010
```
1111

1212

1313
This approach is very similar to the [tuple and loop][approach-tuple-and-loop] approach, but iterates over [`dict.items()`][dict-items] and writes the `loop` as a [`generator-expression`][generator-expression] inside `max()`.
14-
In cases where the scoring circles overlap, `max()` will return the maximum score available for the toss.
14+
In cases where the scoring circles overlap, `max()` will return the maximum score available for the throw.
1515
The generator expression inside `max()` is the equivalent of using a `for-loop` and a variable to determine the max score:
1616

1717

1818
```python
1919
def score(x_coord, y_coord):
20-
toss = x_coord**2 + y_coord**2
20+
throw = x_coord**2 + y_coord**2
2121
rules = {1: 10, 25: 5, 100: 1}
2222
max_score = 0
2323

2424
for distance, point in rules.items():
25-
if toss <= distance and point > max_score:
25+
if throw <= distance and point > max_score:
2626
max_score = point
2727
return max_score
2828
```
2929

3030

31-
A `list` or `tuple` can also be used in place of `max()`, but then requires and index to return the max score:
31+
A `list` or `tuple` can also be used in place of `max()`, but then requires an index to return the max score:
3232

3333
```python
3434
def score(x_coord, y_coord):
35-
toss = x_coord**2 + y_coord**2
35+
throw = x_coord**2 + y_coord**2
3636
rules = {1: 10, 25: 5, 100: 1, 200: 0}
3737

3838
return [point for distance, point in
39-
rules.items() if toss <= distance][0] #<-- have to specify index 0.
39+
rules.items() if throw <= distance][0] #<-- have to specify index 0.
4040

4141
#OR#
4242

4343
def score(x_coord, y_coord):
44-
toss = x_coord**2 + y_coord**2
44+
throw = x_coord**2 + y_coord**2
4545
rules = {1: 10, 25: 5, 100: 1, 200: 0}
4646

4747
return tuple(point for distance, point in
48-
rules.items() if toss <= distance)[0]
48+
rules.items() if throw <= distance)[0]
4949
```
5050

5151

@@ -59,7 +59,7 @@ def score(x_coord, y_coord):
5959
(x_coord**2 + y_coord**2) <= distance)
6060
```
6161

62-
While all of these variations do pass the tests, they suffer from even more over-engineering/performance caution than the earlier tuple and loop approach.
62+
While all of these variations do pass the tests, they suffer from even more over-engineering/performance caution than the earlier tuple and loop approach (_although for the data in this problem, the performance hit is slight_).
6363
Additionally, the dictionary will take much more space in memory than using a `tuple` of tuples to hold scoring values.
6464
In some circumstances, these variations might also be harder to reason about for those not familiar with `generator-expressions` or `list comprehensions`.
6565

exercises/practice/darts/.approaches/introduction.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Among them are:
1515

1616
## General guidance
1717

18-
The goal of the Darts exercise is to score a single toss in a Darts game.
19-
The scoring areas are _concentric circles_, so boundary values need to be checked in order to properly score a toss.
18+
The goal of the Darts exercise is to score a single throw in a Darts game.
19+
The scoring areas are _concentric circles_, so boundary values need to be checked in order to properly score a throw.
2020
The key is to determine how far from the center the dart lands (_by calculating sqrt(x**2 + y**2), or a variation_) and then determine what scoring ring it falls into.
2121

2222

@@ -53,11 +53,11 @@ For more details, see the [if statements][approach-if-statements] approach.
5353

5454
```python
5555
def score(x_coord, y_coord):
56-
toss = x_coord**2 + y_coord**2
56+
throw = x_coord**2 + y_coord**2
5757
rules = (1, 10), (25, 5), (100, 1), (200, 0)
5858

5959
for distance, points in rules:
60-
if toss <= distance:
60+
if throw <= distance:
6161
return points
6262
```
6363

@@ -70,11 +70,11 @@ For more details, see the [tuple and loop][approach-tuple-and-loop] approach.
7070

7171
```python
7272
def score(x_coord, y_coord):
73-
toss = x_coord**2 + y_coord**2
73+
throw = x_coord**2 + y_coord**2
7474
rules = {1: 10, 25: 5, 100: 1, 200: 0}
7575

7676
return max(point for distance, point in
77-
rules.items() if toss <= distance)
77+
rules.items() if throw <= distance)
7878
```
7979

8080
This approach is very similar to the [tuple and loop][approach-tuple-and-loop] approach, but iterates over [`dict.items()`][dict-items].

exercises/practice/darts/.approaches/match-case/content.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ from math import hypot, ceil
66

77

88
def score(x, y):
9-
toss = ceil(hypot(x, y))
9+
throw = ceil(hypot(x, y))
1010

11-
match toss:
11+
match throw:
1212
case 0 | 1: return 10
1313
case 2 | 3 | 4 | 5: return 5
1414
case 6 | 7 | 8 | 9 | 10: return 1
@@ -27,19 +27,19 @@ def score(x, y):
2727
This approach uses `Python 3.10`'s [`structural pattern matching`][structural-pattern-matching] with `return` values on the same line as `case`.
2828
Because the match is numeric, each case explicitly lists allowed values using the `|` (OR) operator.
2929
A fallthrough case (`_`) is used if the dart throw is greater than 10 (_the outer circle radius of the target_).
30-
This is equivalent to using `if-statements` to check toss values although some might argue it is clearer to read.
30+
This is equivalent to using `if-statements` to check throw values although some might argue it is clearer to read.
3131
An `if-statement` equivalent would be:
3232

3333
```python
3434
from math import hypot, ceil
3535

3636

3737
def score(x, y):
38-
toss = ceil(hypot(x, y))
38+
throw = ceil(hypot(x, y))
3939

40-
if toss in (0, 1): return 10
41-
if toss in (2, 3, 4, 5): return 5
42-
if toss in (6, 7, 8, 9, 10): return 1
40+
if throw in (0, 1): return 10
41+
if throw in (2, 3, 4, 5): return 5
42+
if throw in (6, 7, 8, 9, 10): return 1
4343

4444
return 0
4545
```
@@ -52,28 +52,28 @@ from math import hypot, ceil
5252

5353

5454
def score(x, y):
55-
toss = ceil(hypot(x, y))
55+
throw = ceil(hypot(x, y))
5656

57-
match toss:
58-
case toss if toss <= 1: return 10
59-
case toss if toss <= 5: return 5
60-
case toss if toss <= 10: return 1
57+
match throw:
58+
case throw if throw <= 1: return 10
59+
case throw if throw <= 5: return 5
60+
case throw if throw <= 10: return 1
6161
case _: return 0
6262
```
6363

6464

65-
Finally, one can use an [assignment expression][assignment-expression] or [walrus operator][walrus] to calculate the toss value rather than calculating and assigning a variable on a separate line.
65+
Finally, one can use an [assignment expression][assignment-expression] or [walrus operator][walrus] to calculate the throw value rather than calculating and assigning a variable on a separate line.
6666
This isn't necessary (_the first variations shows this clearly_) and might be harder to reason about/understand for some programmers:
6767

6868

6969
```python
7070
from math import hypot, ceil
7171

7272
def score(x, y):
73-
match toss := ceil(hypot(x, y)):
74-
case toss if toss <= 1: return 10
75-
case toss if toss <=5: return 5
76-
case toss if toss <=10: return 1
73+
match throw := ceil(hypot(x, y)):
74+
case throw if throw <= 1: return 10
75+
case throw if throw <=5: return 5
76+
case throw if throw <=10: return 1
7777
case _: return 0
7878
```
7979

exercises/practice/darts/.approaches/tuple-and-loop/content.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@
22

33
```python
44
def score(x_coord, y_coord):
5-
toss = x_coord**2 + y_coord**2
5+
throw = x_coord**2 + y_coord**2
66
rules = (1, 10), (25, 5), (100, 1), (200, 0)
77

88
for distance, points in rules:
9-
if toss <= distance:
9+
if throw <= distance:
1010
return points
1111
```
1212

1313
This approach uses a loop to iterate through the _rules_ `tuple`, unpacking each (`distance`, `points`) pair (_For a little more on unpacking, see [Tuple Unpacking Improves Python Code Readability][tuple-unpacking]_).
14-
If the calculated distance of the toss is less than or equal to a given distance, the score for that region is returned.
14+
If the calculated distance of the throw is less than or equal to a given distance, the score for that region is returned.
1515
A `list` of `lists`, a `list` of `tuples`, or a dictionary could be used here to the same effect:
1616

1717
```python
1818
def score(x_coord, y_coord):
19-
toss = x_coord**2 + y_coord**2
19+
throw = x_coord**2 + y_coord**2
2020
rules = [[1, 10], [25, 5], [100, 1]]
2121

2222
for distance, points in rules:
23-
if toss <= distance:
23+
if throw <= distance:
2424
return points
2525

2626
return 0
2727

2828
#OR#
2929

3030
def score(x_coord, y_coord):
31-
toss = x_coord**2 + y_coord**2
31+
throw = x_coord**2 + y_coord**2
3232
rules = [(1, 10), (25, 5), (100, 1), (200, 0)]
3333

3434
for distance, points in rules:
35-
if toss <= distance:
35+
if throw <= distance:
3636
return points
3737

3838
#OR#
3939

4040
def score(x_coord, y_coord):
41-
toss = x_coord**2 + y_coord**2
41+
throw = x_coord**2 + y_coord**2
4242
rules = {1: 10, 25: 5, 100: 1}
4343

4444
for distance, points in rules.items():
45-
if toss <= distance:
45+
if throw <= distance:
4646
return points
4747

4848
return 0

0 commit comments

Comments
 (0)