Bob approaches fixes (#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
This commit is contained in:
Bob Hoeppner
2022-11-17 04:05:24 -06:00
committed by GitHub
parent 1ce3e3b1a4
commit a91777d7d8
8 changed files with 70 additions and 64 deletions

View File

@@ -1,7 +1,7 @@
# Answer list
```python
_ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
"Calm down, I know what I'm doing!"]
@@ -9,9 +9,9 @@ def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
isShout = (2 if hey_bob.isupper() else 0)
isQuestion = (1 if hey_bob.endswith('?') else 0)
return _ANSWERS[isShout + isQuestion]
is_shout = 2 if hey_bob.isupper() else 0
is_question = 1 if hey_bob.endswith('?') else 0
return ANSWERS[is_shout + is_question]
```
@@ -19,11 +19,15 @@ In this approach you define a [list][list] that contains Bobs answers, and ea
The correct answer is selected from the list by using the score as the list index.
Python doesn't _enforce_ having real constant values,
but the `_ANSWERS` list is defined with all uppercase letters, which is the naming convention for a Python [constant][const].
but the `ANSWERS` list is defined with all uppercase letters, which is the naming convention for a Python [constant][const].
It indicates that the value is not intended to be changed.
Python also does not have real [private][private] variables,
but a leading underscore is the naming convention for indicating that a variable is not meant to be part of the public API.
It should be considered an implementation detail and subject to change without notice.
```exercism/note
`ANSWERS` could prevent item reassignment by being defined as a [tuple](https://realpython.com/python-lists-tuples/#python-tuples) instead of a list.
The items in a tuple cannot be changed, and the performance between a tuple and a list here is equivalent.
The entire `ANSWERS` tuple could still be reassigned to another tuple,
so uppercase letters would still be used to indicate that the `ANSWERS` tuple should not be changed.
```
The [`rstrip`][rstrip] method is applied to the input to eliminate any whitespace at the end of the input.
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
`a` and `z` are cased characters, since their lowercase form changes to `A` and ` Z` when uppercase.
```
If `isupper` is `True`, then `isShout` is given the value of `2`; otherwise, it is given the value of `0`.
If `isupper` is `True`, then `is_shout` is given the value of `2`; otherwise, it is given the value of `0`.
The [`endswith`][endswith] method is used to determine if the input ends with a question mark.
If the test for `endswith('?')` is `True`, then `isQuestion` is given the value of `1`; otherwise it is given the value of `0`.
If the test for `endswith('?')` is `True`, then `is_question` is given the value of `1`; otherwise it is given the value of `0`.
The response is selected from the list by the index like so
| isShout | isQuestion | Index | Answer |
| ------- | ---------- | --------- | ------------------------------------- |
| `false` | `false` | 0 + 0 = 0 | `"Whatever."` |
| `false` | `true` | 0 + 1 = 1 | `"Sure."` |
| `true` | `false` | 2 + 0 = 2 | `"Whoa, chill out!"` |
| `true` | `true` | 2 + 1 = 3 | `"Calm down, I know what I'm doing!"` |
| is_shout | is_question | Index | Answer |
| -------- | ----------- | --------- | ------------------------------------- |
| `false` | `false` | 0 + 0 = 0 | `"Whatever."` |
| `false` | `true` | 0 + 1 = 1 | `"Sure."` |
| `true` | `false` | 2 + 0 = 2 | `"Whoa, chill out!"` |
| `true` | `true` | 2 + 1 = 3 | `"Calm down, I know what I'm doing!"` |
[list]: https://docs.python.org/3/library/stdtypes.html?highlight=list#list
[const]: https://realpython.com/python-constants/
[private]: https://docs.python.org/3/tutorial/classes.html#private-variables
[rstrip]: https://docs.python.org/3/library/stdtypes.html?highlight=rstrip#str.rstrip
[falsiness]: https://www.pythontutorial.net/python-basics/python-boolean/
[not]: https://docs.python.org/3/reference/expressions.html#not

View File

@@ -1,6 +1,6 @@
_ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
"Calm down, I know what I'm doing!"]
# code snipped
isShout = (2 if hey_bob.isupper() else 0)
isQuestion = (1 if hey_bob.endswith('?') else 0)
return _ANSWERS[isShout + isQuestion]
is_shout = 2 if hey_bob.isupper() else 0
is_question = 1 if hey_bob.endswith('?') else 0
return ANSWERS[is_shout + is_question]

View File

@@ -5,14 +5,14 @@ def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
isShout = hey_bob.isupper()
isQuestion = hey_bob.endswith('?')
if isShout:
if isQuestion:
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout:
if is_question:
return "Calm down, I know what I'm doing!"
else:
return 'Whoa, chill out!'
if isQuestion:
if is_question:
return 'Sure.'
return 'Whatever.'

View File

@@ -1,8 +1,8 @@
if isShout:
if isQuestion:
if is_shout:
if is_question:
return "Calm down, I know what I'm doing!"
else:
return 'Whoa, chill out!'
if isQuestion:
if is_question:
return 'Sure.'
return 'Whatever.'

View File

@@ -5,13 +5,13 @@ def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
isShout = hey_bob.isupper()
isQuestion = hey_bob.endswith('?')
if isShout and isQuestion:
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout and is_question:
return "Calm down, I know what I'm doing!"
if isShout:
if is_shout:
return 'Whoa, chill out!'
if isQuestion:
if is_question:
return 'Sure.'
return 'Whatever.'

View File

@@ -1,7 +1,7 @@
if isShout and isQuestion:
if is_shout and is_question:
return "Calm down, I know what I'm doing!"
if isShout:
if is_shout:
return 'Whoa, chill out!'
if isQuestion:
if is_question:
return 'Sure.'
return 'Whatever.'

View File

@@ -17,7 +17,7 @@ Regardless of the approach used, some things you could look out for include
Combine the two determinations instead of copying them.
Not duplicating the code will keep the code [DRY][dry].
- Perhaps consider making `IsQuestion` and `IsShout` values set once instead of functions that are possibly called twice.
- Perhaps consider making `is_question` and `is_shout` values set once instead of functions that are possibly called twice.
- If an `if` statement can return, then an `elif` or `else` is not needed.
Execution will either return or will continue to the next statement anyway.
@@ -29,13 +29,13 @@ def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
isShout = hey_bob.isupper()
isQuestion = hey_bob.endswith('?')
if isShout and isQuestion:
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout and is_question:
return "Calm down, I know what I'm doing!"
if isShout:
if is_shout:
return 'Whoa, chill out!'
if isQuestion:
if is_question:
return 'Sure.'
return 'Whatever.'
@@ -50,14 +50,14 @@ def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
isShout = hey_bob.isupper()
isQuestion = hey_bob.endswith('?')
if isShout:
if isQuestion:
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout:
if is_question:
return "Calm down, I know what I'm doing!"
else:
return 'Whoa, chill out!'
if isQuestion:
if is_question:
return 'Sure.'
return 'Whatever.'

View File

@@ -8,13 +8,13 @@ def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
isShout = hey_bob.isupper()
isQuestion = hey_bob.endswith('?')
if isShout and isQuestion:
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout and is_question:
return "Calm down, I know what I'm doing!"
if isShout:
if is_shout:
return 'Whoa, chill out!'
if isQuestion:
if is_question:
return 'Sure.'
return 'Whatever.'
@@ -29,14 +29,14 @@ def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
isShout = hey_bob.isupper()
isQuestion = hey_bob.endswith('?')
if isShout:
if isQuestion:
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout:
if is_question:
return "Calm down, I know what I'm doing!"
else:
return 'Whoa, chill out!'
if isQuestion:
return 'Whoa, chill out!'
if is_question:
return 'Sure.'
return 'Whatever.'
@@ -47,14 +47,17 @@ print(f"if statements nested: {val}")
val = timeit.timeit("""response("I really don't have anything to say.")""",
"""
_ANSWERS = ["Whatever.", "Sure.", "Whoa, chill out!", "Calm down, I know what I'm doing!"]
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
"Calm down, I know what I'm doing!"]
def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob: return 'Fine. Be that way!'
isShout = 2 if hey_bob.isupper() else 0
isQuestion = 1 if hey_bob.endswith('?') else 0
return _ANSWERS[isShout + isQuestion]
if not hey_bob:
return 'Fine. Be that way!'
is_shout = 2 if hey_bob.isupper() else 0
is_question = 1 if hey_bob.endswith('?') else 0
return ANSWERS[is_shout + is_question]
""", number=loops) / loops