🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of pfertyk

pfertyk's solution

to Bob in the Python Track

Published at Jul 13 2018 · 4 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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:

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):

  • 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 the help page.

Source

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. 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.

bob_test.py

import unittest

import bob


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0

class BobTest(unittest.TestCase):
    def test_stating_something(self):
        self.assertEqual(bob.hey("Tom-ay-to, tom-aaaah-to."), "Whatever.")

    def test_shouting(self):
        self.assertEqual(bob.hey("WATCH OUT!"), "Whoa, chill out!")

    def test_shouting_gibberish(self):
        self.assertEqual(bob.hey("FCECDFCAAB"), "Whoa, chill out!")

    def test_asking_a_question(self):
        self.assertEqual(
            bob.hey("Does this cryogenic chamber make me look fat?"), "Sure.")

    def test_asking_a_numeric_question(self):
        self.assertEqual(bob.hey("You are, what, like 15?"), "Sure.")

    def test_asking_gibberish(self):
        self.assertEqual(bob.hey("fffbbcbeab?"), "Sure.")

    def test_talking_forcefully(self):
        self.assertEqual(
            bob.hey("Let's go make out behind the gym!"), "Whatever.")

    def test_using_acronyms_in_regular_speech(self):
        self.assertEqual(
            bob.hey("It's OK if you don't want to go to the DMV."),
            "Whatever.")

    def test_forceful_question(self):
        self.assertEqual(
            bob.hey("WHAT THE HELL WERE YOU THINKING?"),
            "Calm down, I know what I'm doing!"
        )

    def test_shouting_numbers(self):
        self.assertEqual(bob.hey("1, 2, 3 GO!"), "Whoa, chill out!")

    def test_only_numbers(self):
        self.assertEqual(bob.hey("1, 2, 3"), "Whatever.")

    def test_question_with_only_numbers(self):
        self.assertEqual(bob.hey("4?"), "Sure.")

    def test_shouting_with_special_characters(self):
        self.assertEqual(
            bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"),
            "Whoa, chill out!")

    def test_shouting_with_no_exclamation_mark(self):
        self.assertEqual(bob.hey("I HATE YOU"), "Whoa, chill out!")

    def test_statement_containing_question_mark(self):
        self.assertEqual(
            bob.hey("Ending with ? means a question."), "Whatever.")

    def test_non_letters_with_question(self):
        self.assertEqual(bob.hey(":) ?"), "Sure.")

    def test_prattling_on(self):
        self.assertEqual(
            bob.hey("Wait! Hang on. Are you going to be OK?"), "Sure.")

    def test_silence(self):
        self.assertEqual(bob.hey(""), "Fine. Be that way!")

    def test_prolonged_silence(self):
        self.assertEqual(bob.hey("          "), "Fine. Be that way!")

    def test_alternate_silence(self):
        self.assertEqual(bob.hey("\t\t\t\t\t\t\t\t\t\t"), "Fine. Be that way!")

    def test_multiple_line_question(self):
        self.assertEqual(
            bob.hey("\nDoes this cryogenic chamber make me look fat?\nno"),
            "Whatever.")

    def test_starting_with_whitespace(self):
        self.assertEqual(bob.hey("         hmmmmmmm..."), "Whatever.")

    def test_ending_with_whitespace(self):
        self.assertEqual(
            bob.hey("Okay if like my  spacebar  quite a bit?   "), "Sure.")

    def test_other_whitespace(self):
        self.assertEqual(bob.hey("\n\r \t"), "Fine. Be that way!")

    def test_non_question_ending_with_whitespace(self):
        self.assertEqual(
            bob.hey("This is a statement ending with whitespace      "),
            "Whatever.")


if __name__ == '__main__':
    unittest.main()
class Utterance:
    def __init__(self, text):
        self.text = text

    def is_silence(self):
        return not self.text.strip()

    def is_yelling(self):
        return self.text.isupper()

    def is_question(self):
        return self.text.strip().endswith('?')

def hey(text):
    utterance = Utterance(text)
    if utterance.is_silence():
        return 'Fine. Be that way!'
    if utterance.is_yelling():
        return 'Whoa, chill out!'
    if utterance.is_question():
        return 'Sure.'
    return 'Whatever.'

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of pfertyk

This solution is strongly inspired by Katrina Owen's Overkill talk. I moved the methods for interpreting input into a class. I am not happy about the fact that this code does not mention Bob at all (apart from the file's name).

Avatar of JamesBTaylor

As mentioned before - its important to take on board this part of her presentation: https://www.youtube.com/watch?v=GWEEPt8VvmU#t=31m22s

Avatar of pfertyk

I agree: Bob can do quite well without object-oriented solution. I just like that naming methods like this improves readability a lot (is_question() is more understandable than endswith('?') in my opinion). And since the 3 methods seem naturally connected with each other, I put them in a class. The class is a bit of an 'overkill', that's true, but even without it I would still keep the well-named methods.

Avatar of JamesBTaylor

Yeah - fair enough - readability is an art form, but the volume of code carries a cognitive load as well.

You didn't inherit Utterance from String - that made me happy :)

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?