Skip to content

Commit a91777d

Browse files
authored
Bob approaches fixes (exercism#3201)
Final isShout and isQuestion remnants changed to is_shout and is_question. * Update introduction.md * Update snippet.txt * Update content.md * Update snippet.txt * Update content.md * Update snippet.txt * Update content.md * Update content.md * Update Benchmark.py * Update content.md
1 parent 1ce3e3b commit a91777d

File tree

8 files changed

+70
-64
lines changed

8 files changed

+70
-64
lines changed

exercises/practice/bob/.approaches/answer-list/content.md

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
# Answer list
22

33
```python
4-
_ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
4+
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
55
"Calm down, I know what I'm doing!"]
66

77

88
def response(hey_bob):
99
hey_bob = hey_bob.rstrip()
1010
if not hey_bob:
1111
return 'Fine. Be that way!'
12-
isShout = (2 if hey_bob.isupper() else 0)
13-
isQuestion = (1 if hey_bob.endswith('?') else 0)
14-
return _ANSWERS[isShout + isQuestion]
12+
is_shout = 2 if hey_bob.isupper() else 0
13+
is_question = 1 if hey_bob.endswith('?') else 0
14+
return ANSWERS[is_shout + is_question]
1515

1616
```
1717

1818
In this approach you define a [list][list] that contains Bob’s answers, and each condition is given a score.
1919
The correct answer is selected from the list by using the score as the list index.
2020

2121
Python doesn't _enforce_ having real constant values,
22-
but the `_ANSWERS` list is defined with all uppercase letters, which is the naming convention for a Python [constant][const].
22+
but the `ANSWERS` list is defined with all uppercase letters, which is the naming convention for a Python [constant][const].
2323
It indicates that the value is not intended to be changed.
24-
Python also does not have real [private][private] variables,
25-
but a leading underscore is the naming convention for indicating that a variable is not meant to be part of the public API.
26-
It should be considered an implementation detail and subject to change without notice.
24+
25+
```exercism/note
26+
`ANSWERS` could prevent item reassignment by being defined as a [tuple](https://realpython.com/python-lists-tuples/#python-tuples) instead of a list.
27+
The items in a tuple cannot be changed, and the performance between a tuple and a list here is equivalent.
28+
The entire `ANSWERS` tuple could still be reassigned to another tuple,
29+
so uppercase letters would still be used to indicate that the `ANSWERS` tuple should not be changed.
30+
```
2731

2832
The [`rstrip`][rstrip] method is applied to the input to eliminate any whitespace at the end of the input.
2933
If the input has no characters left, it uses the [falsiness][falsiness] of an empty string with the [`not`][not] operator to return the response for saying nothing.
@@ -39,25 +43,24 @@ For example, `?` and `3` are not cased characters, as they do not change between
3943
`a` and `z` are cased characters, since their lowercase form changes to `A` and ` Z` when uppercase.
4044
```
4145

42-
If `isupper` is `True`, then `isShout` is given the value of `2`; otherwise, it is given the value of `0`.
46+
If `isupper` is `True`, then `is_shout` is given the value of `2`; otherwise, it is given the value of `0`.
4347

4448
The [`endswith`][endswith] method is used to determine if the input ends with a question mark.
45-
If the test for `endswith('?')` is `True`, then `isQuestion` is given the value of `1`; otherwise it is given the value of `0`.
49+
If the test for `endswith('?')` is `True`, then `is_question` is given the value of `1`; otherwise it is given the value of `0`.
4650

4751

4852
The response is selected from the list by the index like so
4953

50-
| isShout | isQuestion | Index | Answer |
51-
| ------- | ---------- | --------- | ------------------------------------- |
52-
| `false` | `false` | 0 + 0 = 0 | `"Whatever."` |
53-
| `false` | `true` | 0 + 1 = 1 | `"Sure."` |
54-
| `true` | `false` | 2 + 0 = 2 | `"Whoa, chill out!"` |
55-
| `true` | `true` | 2 + 1 = 3 | `"Calm down, I know what I'm doing!"` |
54+
| is_shout | is_question | Index | Answer |
55+
| -------- | ----------- | --------- | ------------------------------------- |
56+
| `false` | `false` | 0 + 0 = 0 | `"Whatever."` |
57+
| `false` | `true` | 0 + 1 = 1 | `"Sure."` |
58+
| `true` | `false` | 2 + 0 = 2 | `"Whoa, chill out!"` |
59+
| `true` | `true` | 2 + 1 = 3 | `"Calm down, I know what I'm doing!"` |
5660

5761

5862
[list]: https://docs.python.org/3/library/stdtypes.html?highlight=list#list
5963
[const]: https://realpython.com/python-constants/
60-
[private]: https://docs.python.org/3/tutorial/classes.html#private-variables
6164
[rstrip]: https://docs.python.org/3/library/stdtypes.html?highlight=rstrip#str.rstrip
6265
[falsiness]: https://www.pythontutorial.net/python-basics/python-boolean/
6366
[not]: https://docs.python.org/3/reference/expressions.html#not
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
_ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
1+
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
22
"Calm down, I know what I'm doing!"]
33
# code snipped
4-
isShout = (2 if hey_bob.isupper() else 0)
5-
isQuestion = (1 if hey_bob.endswith('?') else 0)
6-
return _ANSWERS[isShout + isQuestion]
4+
is_shout = 2 if hey_bob.isupper() else 0
5+
is_question = 1 if hey_bob.endswith('?') else 0
6+
return ANSWERS[is_shout + is_question]

exercises/practice/bob/.approaches/if-statements-nested/content.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ def response(hey_bob):
55
hey_bob = hey_bob.rstrip()
66
if not hey_bob:
77
return 'Fine. Be that way!'
8-
isShout = hey_bob.isupper()
9-
isQuestion = hey_bob.endswith('?')
10-
if isShout:
11-
if isQuestion:
8+
is_shout = hey_bob.isupper()
9+
is_question = hey_bob.endswith('?')
10+
if is_shout:
11+
if is_question:
1212
return "Calm down, I know what I'm doing!"
1313
else:
1414
return 'Whoa, chill out!'
15-
if isQuestion:
15+
if is_question:
1616
return 'Sure.'
1717
return 'Whatever.'
1818

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
if isShout:
2-
if isQuestion:
1+
if is_shout:
2+
if is_question:
33
return "Calm down, I know what I'm doing!"
44
else:
55
return 'Whoa, chill out!'
6-
if isQuestion:
6+
if is_question:
77
return 'Sure.'
88
return 'Whatever.'

exercises/practice/bob/.approaches/if-statements/content.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ def response(hey_bob):
55
hey_bob = hey_bob.rstrip()
66
if not hey_bob:
77
return 'Fine. Be that way!'
8-
isShout = hey_bob.isupper()
9-
isQuestion = hey_bob.endswith('?')
10-
if isShout and isQuestion:
8+
is_shout = hey_bob.isupper()
9+
is_question = hey_bob.endswith('?')
10+
if is_shout and is_question:
1111
return "Calm down, I know what I'm doing!"
12-
if isShout:
12+
if is_shout:
1313
return 'Whoa, chill out!'
14-
if isQuestion:
14+
if is_question:
1515
return 'Sure.'
1616
return 'Whatever.'
1717

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
if isShout and isQuestion:
1+
if is_shout and is_question:
22
return "Calm down, I know what I'm doing!"
3-
if isShout:
3+
if is_shout:
44
return 'Whoa, chill out!'
5-
if isQuestion:
5+
if is_question:
66
return 'Sure.'
77
return 'Whatever.'

exercises/practice/bob/.approaches/introduction.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Regardless of the approach used, some things you could look out for include
1717
Combine the two determinations instead of copying them.
1818
Not duplicating the code will keep the code [DRY][dry].
1919

20-
- Perhaps consider making `IsQuestion` and `IsShout` values set once instead of functions that are possibly called twice.
20+
- Perhaps consider making `is_question` and `is_shout` values set once instead of functions that are possibly called twice.
2121

2222
- If an `if` statement can return, then an `elif` or `else` is not needed.
2323
Execution will either return or will continue to the next statement anyway.
@@ -29,13 +29,13 @@ def response(hey_bob):
2929
hey_bob = hey_bob.rstrip()
3030
if not hey_bob:
3131
return 'Fine. Be that way!'
32-
isShout = hey_bob.isupper()
33-
isQuestion = hey_bob.endswith('?')
34-
if isShout and isQuestion:
32+
is_shout = hey_bob.isupper()
33+
is_question = hey_bob.endswith('?')
34+
if is_shout and is_question:
3535
return "Calm down, I know what I'm doing!"
36-
if isShout:
36+
if is_shout:
3737
return 'Whoa, chill out!'
38-
if isQuestion:
38+
if is_question:
3939
return 'Sure.'
4040
return 'Whatever.'
4141

@@ -50,14 +50,14 @@ def response(hey_bob):
5050
hey_bob = hey_bob.rstrip()
5151
if not hey_bob:
5252
return 'Fine. Be that way!'
53-
isShout = hey_bob.isupper()
54-
isQuestion = hey_bob.endswith('?')
55-
if isShout:
56-
if isQuestion:
53+
is_shout = hey_bob.isupper()
54+
is_question = hey_bob.endswith('?')
55+
if is_shout:
56+
if is_question:
5757
return "Calm down, I know what I'm doing!"
5858
else:
5959
return 'Whoa, chill out!'
60-
if isQuestion:
60+
if is_question:
6161
return 'Sure.'
6262
return 'Whatever.'
6363

exercises/practice/bob/.articles/performance/code/Benchmark.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ def response(hey_bob):
88
hey_bob = hey_bob.rstrip()
99
if not hey_bob:
1010
return 'Fine. Be that way!'
11-
isShout = hey_bob.isupper()
12-
isQuestion = hey_bob.endswith('?')
13-
if isShout and isQuestion:
11+
is_shout = hey_bob.isupper()
12+
is_question = hey_bob.endswith('?')
13+
if is_shout and is_question:
1414
return "Calm down, I know what I'm doing!"
15-
if isShout:
15+
if is_shout:
1616
return 'Whoa, chill out!'
17-
if isQuestion:
17+
if is_question:
1818
return 'Sure.'
1919
return 'Whatever.'
2020
@@ -29,14 +29,14 @@ def response(hey_bob):
2929
hey_bob = hey_bob.rstrip()
3030
if not hey_bob:
3131
return 'Fine. Be that way!'
32-
isShout = hey_bob.isupper()
33-
isQuestion = hey_bob.endswith('?')
34-
if isShout:
35-
if isQuestion:
32+
is_shout = hey_bob.isupper()
33+
is_question = hey_bob.endswith('?')
34+
if is_shout:
35+
if is_question:
3636
return "Calm down, I know what I'm doing!"
3737
else:
38-
return 'Whoa, chill out!'
39-
if isQuestion:
38+
return 'Whoa, chill out!'
39+
if is_question:
4040
return 'Sure.'
4141
return 'Whatever.'
4242
@@ -47,14 +47,17 @@ def response(hey_bob):
4747
val = timeit.timeit("""response("I really don't have anything to say.")""",
4848
"""
4949
50-
_ANSWERS = ["Whatever.", "Sure.", "Whoa, chill out!", "Calm down, I know what I'm doing!"]
50+
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
51+
"Calm down, I know what I'm doing!"]
52+
5153
5254
def response(hey_bob):
5355
hey_bob = hey_bob.rstrip()
54-
if not hey_bob: return 'Fine. Be that way!'
55-
isShout = 2 if hey_bob.isupper() else 0
56-
isQuestion = 1 if hey_bob.endswith('?') else 0
57-
return _ANSWERS[isShout + isQuestion]
56+
if not hey_bob:
57+
return 'Fine. Be that way!'
58+
is_shout = 2 if hey_bob.isupper() else 0
59+
is_question = 1 if hey_bob.endswith('?') else 0
60+
return ANSWERS[is_shout + is_question]
5861
5962
6063
""", number=loops) / loops

0 commit comments

Comments
 (0)