From 7621525406671bc03088fc9c8faea70b3906bb2d Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 14 Jul 2019 04:05:00 -0500 Subject: [PATCH] bob exercise --- python/bob/.exercism/metadata.json | 1 + python/bob/README.md | 63 ++++++++++++++++++ python/bob/bob.py | 9 +++ python/bob/bob_test.py | 103 +++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 python/bob/.exercism/metadata.json create mode 100644 python/bob/README.md create mode 100644 python/bob/bob.py create mode 100644 python/bob/bob_test.py diff --git a/python/bob/.exercism/metadata.json b/python/bob/.exercism/metadata.json new file mode 100644 index 0000000..a6d5aa2 --- /dev/null +++ b/python/bob/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"python","exercise":"bob","id":"7961709ebcf94c47b27720337d4a1515","url":"https://exercism.io/my/solutions/7961709ebcf94c47b27720337d4a1515","handle":"Xevion","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/python/bob/README.md b/python/bob/README.md new file mode 100644 index 0000000..9595765 --- /dev/null +++ b/python/bob/README.md @@ -0,0 +1,63 @@ +# Bob + +Bob is a lackadaisical teenager. In conversation, his responses are very limited. + +Bob answers 'Sure.' if you ask him a question. + +He answers 'Whoa, chill out!' if you yell at him. + +He answers 'Calm down, I know what I'm doing!' if you yell a question at him. + +He says 'Fine. Be that way!' if you address him without actually saying +anything. + +He answers 'Whatever.' to anything else. + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to +indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you should write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + +## Running the tests + +To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)): + +- Python 2.7: `py.test bob_test.py` +- Python 3.4+: `pytest bob_test.py` + +Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): +`python -m pytest bob_test.py` + +### Common `pytest` options + +- `-v` : enable verbose output +- `-x` : stop running tests on first failure +- `--ff` : run failures from previous test before running other test cases + +For other options, see `python -m pytest -h` + +## Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/bob` directory. + +You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. + +For more detailed information about running tests, code style and linting, +please see [Running the Tests](http://exercism.io/tracks/python/tests). + +## Source + +Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=06](http://pine.fm/LearnToProgram/?Chapter=06) + +## Submitting Incomplete Solutions + +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/python/bob/bob.py b/python/bob/bob.py new file mode 100644 index 0000000..fd4b074 --- /dev/null +++ b/python/bob/bob.py @@ -0,0 +1,9 @@ +def response(hey_bob): + if hey_bob.strip() == '': + return "Fine. Be that way!" + elif hey_bob.upper() == hey_bob and ''.join(x.upper() for x in hey_bob if x.upper() in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') != '': + return "Calm down, I know what I'm doing!" if hey_bob.strip().endswith('?') else "Whoa, chill out!" + elif hey_bob.strip().endswith('?'): + return "Sure." + else: + return "Whatever." \ No newline at end of file diff --git a/python/bob/bob_test.py b/python/bob/bob_test.py new file mode 100644 index 0000000..6c032c6 --- /dev/null +++ b/python/bob/bob_test.py @@ -0,0 +1,103 @@ +import unittest + +from bob import response + + +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.4.0 + +class BobTest(unittest.TestCase): + def test_stating_something(self): + self.assertEqual(response("Tom-ay-to, tom-aaaah-to."), "Whatever.") + + def test_shouting(self): + self.assertEqual(response("WATCH OUT!"), "Whoa, chill out!") + + def test_shouting_gibberish(self): + self.assertEqual(response("FCECDFCAAB"), "Whoa, chill out!") + + def test_asking_a_question(self): + self.assertEqual( + response("Does this cryogenic chamber make me look fat?"), "Sure.") + + def test_asking_a_numeric_question(self): + self.assertEqual(response("You are, what, like 15?"), "Sure.") + + def test_asking_gibberish(self): + self.assertEqual(response("fffbbcbeab?"), "Sure.") + + def test_talking_forcefully(self): + self.assertEqual( + response("Let's go make out behind the gym!"), "Whatever.") + + def test_using_acronyms_in_regular_speech(self): + self.assertEqual( + response("It's OK if you don't want to go to the DMV."), + "Whatever.") + + def test_forceful_question(self): + self.assertEqual( + response("WHAT THE HELL WERE YOU THINKING?"), + "Calm down, I know what I'm doing!" + ) + + def test_shouting_numbers(self): + self.assertEqual(response("1, 2, 3 GO!"), "Whoa, chill out!") + + def test_no_letters(self): + self.assertEqual(response("1, 2, 3"), "Whatever.") + + def test_question_with_no_letters(self): + self.assertEqual(response("4?"), "Sure.") + + def test_shouting_with_special_characters(self): + self.assertEqual( + response("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"), + "Whoa, chill out!") + + def test_shouting_with_no_exclamation_mark(self): + self.assertEqual(response("I HATE THE DMV"), "Whoa, chill out!") + + def test_statement_containing_question_mark(self): + self.assertEqual( + response("Ending with ? means a question."), "Whatever.") + + def test_non_letters_with_question(self): + self.assertEqual(response(":) ?"), "Sure.") + + def test_prattling_on(self): + self.assertEqual( + response("Wait! Hang on. Are you going to be OK?"), "Sure.") + + def test_silence(self): + self.assertEqual(response(""), "Fine. Be that way!") + + def test_prolonged_silence(self): + self.assertEqual(response(" "), "Fine. Be that way!") + + def test_alternate_silence(self): + self.assertEqual(response("\t\t\t\t\t\t\t\t\t\t"), + "Fine. Be that way!") + + def test_multiple_line_question(self): + self.assertEqual( + response("\nDoes this cryogenic chamber make me look fat?\n" + "No."), "Whatever.") + + def test_starting_with_whitespace(self): + self.assertEqual(response(" hmmmmmmm..."), "Whatever.") + + def test_ending_with_whitespace(self): + self.assertEqual( + response("Okay if like my spacebar quite a bit? "), "Sure.") + + def test_other_whitespace(self): + self.assertEqual(response("\n\r \t"), "Fine. Be that way!") + + def test_non_question_ending_with_whitespace(self): + self.assertEqual( + response("This is a statement ending with whitespace "), + "Whatever.") + + +if __name__ == '__main__': + unittest.main()