|
| 1 | +# Introduction |
| 2 | + |
| 3 | +There are various idiomatic approaches to solve Bob. |
| 4 | +A basic approach can use a series of `if` statements to test the conditions. |
| 5 | +The `if` statements could also be nested. |
| 6 | +A list can contain answers from which the right response is selected by an index calculated from scores given to the conditions. |
| 7 | + |
| 8 | +## General guidance |
| 9 | + |
| 10 | +Regardless of the approach used, some things you could look out for include |
| 11 | + |
| 12 | +- If the input is stripped, [`rstrip`][rstrip] only once. |
| 13 | + |
| 14 | +- Use the [`endswith`][endswith] method instead of checking the last character by index for `?`. |
| 15 | + |
| 16 | +- Don't copy/paste the logic for determining a shout and for determing a question into determing a shouted question. |
| 17 | + Combine the two determinations instead of copying them. |
| 18 | + Not duplicating the code will keep the code [DRY][dry]. |
| 19 | + |
| 20 | +- Perhaps consider making `IsQuestion` and `IsShout` values set once instead of functions that are possibly called twice. |
| 21 | + |
| 22 | +- If an `if` statement can return, then an `elif` or `else` is not needed. |
| 23 | + Execution will either return or will continue to the next statement anyway. |
| 24 | + |
| 25 | +## Approach: `if` statements |
| 26 | + |
| 27 | +```python |
| 28 | +def response(hey_bob): |
| 29 | + hey_bob = hey_bob.rstrip() |
| 30 | + if not hey_bob: |
| 31 | + return 'Fine. Be that way!' |
| 32 | + isShout = hey_bob.isupper() |
| 33 | + isQuestion = hey_bob.endswith('?') |
| 34 | + if isShout and isQuestion: |
| 35 | + return "Calm down, I know what I'm doing!" |
| 36 | + if isShout: |
| 37 | + return 'Whoa, chill out!' |
| 38 | + if isQuestion: |
| 39 | + return 'Sure.' |
| 40 | + return 'Whatever.' |
| 41 | + |
| 42 | +``` |
| 43 | + |
| 44 | +For more information, check the [`if` statements approach][approach-if]. |
| 45 | + |
| 46 | +## Approach: `if` statements nested |
| 47 | + |
| 48 | +```python |
| 49 | +def response(hey_bob): |
| 50 | + hey_bob = hey_bob.rstrip() |
| 51 | + if not hey_bob: |
| 52 | + return 'Fine. Be that way!' |
| 53 | + isShout = hey_bob.isupper() |
| 54 | + isQuestion = hey_bob.endswith('?') |
| 55 | + if isShout: |
| 56 | + if isQuestion: |
| 57 | + return "Calm down, I know what I'm doing!" |
| 58 | + else: |
| 59 | + return 'Whoa, chill out!' |
| 60 | + if isQuestion: |
| 61 | + return 'Sure.' |
| 62 | + return 'Whatever.' |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +For more information, check the [`if` statements nested approach][approach-if-nested]. |
| 67 | + |
| 68 | +## Other approaches |
| 69 | + |
| 70 | +Besides the aforementioned, idiomatic approaches, you could also approach the exercise as follows: |
| 71 | + |
| 72 | +### Other approach: answer list |
| 73 | + |
| 74 | +A list can be defined that contains Bob’s answers, and each condition is given a score. |
| 75 | +The correct answer is selected from the list by using the score as the list index. |
| 76 | +For more information, check the [answer list approach][approach-answer-list]. |
| 77 | + |
| 78 | +## Which approach to use? |
| 79 | + |
| 80 | +The nested `if` approach is fastest, but some may consider it a bit less readable than the unnested `if` statements. |
| 81 | +The answer list approach is slowest, but some may prefer doing away with the chain of `if` statements. |
| 82 | +Since the difference between them is a few nanoseconds, which one is used may be a matter of stylistic preference. |
| 83 | + |
| 84 | +- To compare the performance of the approaches, check out the [Performance article][article-performance]. |
| 85 | + |
| 86 | +[rstrip]: https://docs.python.org/3/library/stdtypes.html?highlight=rstrip#str.rstrip |
| 87 | +[endswith]: https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.endswith |
| 88 | +[dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself |
| 89 | +[approach-if]: https://exercism.org/tracks/python/exercises/bob/approaches/if-statements |
| 90 | +[approach-if-nested]: https://exercism.org/tracks/python/exercises/bob/approaches/if-statements-nested |
| 91 | +[approach-answer-list]: https://exercism.org/tracks/python/exercises/bob/approaches/answer-list |
| 92 | +[article-performance]: https://exercism.org/tracks/python/exercises/bob/articles/performance |
0 commit comments