From a91777d7d860cd8c356780bba5aaab33b20aab3a Mon Sep 17 00:00:00 2001 From: Bob Hoeppner <32035397+bobahop@users.noreply.github.com> Date: Thu, 17 Nov 2022 04:05:24 -0600 Subject: [PATCH] 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 --- .../bob/.approaches/answer-list/content.md | 37 ++++++++++--------- .../bob/.approaches/answer-list/snippet.txt | 8 ++-- .../if-statements-nested/content.md | 10 ++--- .../if-statements-nested/snippet.txt | 6 +-- .../bob/.approaches/if-statements/content.md | 10 ++--- .../bob/.approaches/if-statements/snippet.txt | 6 +-- .../practice/bob/.approaches/introduction.md | 22 +++++------ .../.articles/performance/code/Benchmark.py | 35 ++++++++++-------- 8 files changed, 70 insertions(+), 64 deletions(-) diff --git a/exercises/practice/bob/.approaches/answer-list/content.md b/exercises/practice/bob/.approaches/answer-list/content.md index de2049a7..b3894780 100644 --- a/exercises/practice/bob/.approaches/answer-list/content.md +++ b/exercises/practice/bob/.approaches/answer-list/content.md @@ -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 Bob’s 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 diff --git a/exercises/practice/bob/.approaches/answer-list/snippet.txt b/exercises/practice/bob/.approaches/answer-list/snippet.txt index 0d834fa2..b1ce322f 100644 --- a/exercises/practice/bob/.approaches/answer-list/snippet.txt +++ b/exercises/practice/bob/.approaches/answer-list/snippet.txt @@ -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] diff --git a/exercises/practice/bob/.approaches/if-statements-nested/content.md b/exercises/practice/bob/.approaches/if-statements-nested/content.md index e0702169..06b5ed52 100644 --- a/exercises/practice/bob/.approaches/if-statements-nested/content.md +++ b/exercises/practice/bob/.approaches/if-statements-nested/content.md @@ -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.' diff --git a/exercises/practice/bob/.approaches/if-statements-nested/snippet.txt b/exercises/practice/bob/.approaches/if-statements-nested/snippet.txt index c86b57e6..0df6c2fe 100644 --- a/exercises/practice/bob/.approaches/if-statements-nested/snippet.txt +++ b/exercises/practice/bob/.approaches/if-statements-nested/snippet.txt @@ -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.' diff --git a/exercises/practice/bob/.approaches/if-statements/content.md b/exercises/practice/bob/.approaches/if-statements/content.md index 8bae5116..7d4d84e3 100644 --- a/exercises/practice/bob/.approaches/if-statements/content.md +++ b/exercises/practice/bob/.approaches/if-statements/content.md @@ -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.' diff --git a/exercises/practice/bob/.approaches/if-statements/snippet.txt b/exercises/practice/bob/.approaches/if-statements/snippet.txt index 0e166bed..1ffdaf77 100644 --- a/exercises/practice/bob/.approaches/if-statements/snippet.txt +++ b/exercises/practice/bob/.approaches/if-statements/snippet.txt @@ -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.' diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index 173bb46f..ccdfa93f 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -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.' diff --git a/exercises/practice/bob/.articles/performance/code/Benchmark.py b/exercises/practice/bob/.articles/performance/code/Benchmark.py index b176dc90..e726ed19 100644 --- a/exercises/practice/bob/.articles/performance/code/Benchmark.py +++ b/exercises/practice/bob/.articles/performance/code/Benchmark.py @@ -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