Avatar of 4d47

4d47's solution

to Bob in the Emacs Lisp Track

Test suite

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.


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.el --- ERT tests for Bob (exercism)

;;; Commentary:
;; Common test data version: 1.2.0 6dc2014

;;; Code:

(load-file "bob.el")

(ert-deftest responds-to-stating-something ()
  (should (string= "Whatever." (response-for "Tom-ay-to, tom-aaaah-to."))))

(ert-deftest responds-to-shouting ()
   (string= "Whoa, chill out!" (response-for "WATCH OUT!"))))

(ert-deftest responds-to-shouting-gibberish ()
   (string= "Whoa, chill out!" (response-for "FCECDFCAAB"))))

(ert-deftest responds-to-asking-a-question ()
   (string= "Sure." (response-for "Does this cryogenic chamber make me look fat?"))))

(ert-deftest responds-to-asking-a-numeric-question ()
   (string= "Sure." (response-for "You are, what, like 15?"))))

(ert-deftest responds-to-asking-gibberish ()
   (string= "Sure." (response-for "fffbbcbeab?"))))

(ert-deftest responds-to-talking-forcefully ()
   (string= "Whatever." (response-for "Let's go make out behind the gym!"))))

(ert-deftest responds-to-using-acronyms-in-regular-speech ()
   (string= "Whatever." (response-for "It's OK if you don't want to go to the DMV."))))

(ert-deftest responds-to-forceful-question ()
   (string= "Calm down, I know what I'm doing!" (response-for "WHAT THE HELL WERE YOU THINKING?"))))

(ert-deftest responds-to-shouting-numbers ()
   (string= "Whoa, chill out!" (response-for "1, 2, 3, GO!"))))

(ert-deftest responds-to-only-numbers ()
   (string= "Whatever." (response-for "1, 2, 3"))))

(ert-deftest responds-to-questions-with-only-numbers ()
   (string= "Sure." (response-for "4?"))))

(ert-deftest responds-to-shouting-with-special-chars ()
   (string= "Whoa, chill out!" (response-for "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"))))

(ert-deftest responds-to-shouting-with-no-exclamation-mark ()
   (string= "Whoa, chill out!" (response-for "I HATE YOU"))))

(ert-deftest responds-to-statement-containing-question-mark ()
   (string= "Whatever." (response-for "Ending with ? means a question."))))

(ert-deftest responds-to-non-letters-with-question ()
   (string= "Sure." (response-for ":) ?"))))

(ert-deftest responds-to-prattling-on ()
   (string= "Sure." (response-for "Wait! Hang on. Are you going to be OK?"))))

(ert-deftest responds-to-silence ()
   (string= "Fine. Be that way!" (response-for ""))))

(ert-deftest responds-to-prolonged-silence ()
   (string= "Fine. Be that way!" (response-for "    "))))

(ert-deftest responds-to-alternate-silence ()
   (string= "Fine. Be that way!" (response-for "\t\t\t\t\t\t\t\t\t\t"))))

(ert-deftest responds-to-multiple-line-question ()
   (string= "Whatever." (response-for "\nDoes this cryogenic chamber make me look fat?\nno"))))

(ert-deftest responds-to-starting-with-whitespace ()
   (string= "Whatever." (response-for "         hmmmmmmm..."))))

(ert-deftest responds-to-ending-with-whitespace ()
   (string= "Sure." (response-for "Okay if like my  spacebar  quite a bit?   "))))

(ert-deftest responds-to-other-whitespace ()
   (string= "Fine. Be that way!" (response-for "\n\r \t"))))

(ert-deftest responds-to-non-question-ending-with-whitespace ()
   (string= "Whatever." (response-for "This is a statement ending with whitespace      "))))

(provide 'bob-test)
;;;bob-test.el ends here
;;; bob.el --- Bob exercise (exercism)

;;; Commentary:

;;; Code:

(defun response-for (input)
  "Bob's limited response for given INPUT."
  (let ((s (string-trim input)))
     ((string-shouting-p s)
      "Whoa, chill out!")
     ((string-suffix-p "?" s)
     ((string-empty-p s)
      "Fine. Be that way!")

(defun string-shouting-p (string)
  "Return non-nil if STRING is all uppercase letters."
   (string= string (upcase string))
   (string-match-p "[[:alpha:]]" string)))

(provide 'bob)

;;; bob.el ends here

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?