Avatar of shmibs

shmibs's solution

to Bob in the D Track

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

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 says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

Getting Started

Make sure you have read D page on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

static if (all_tests_enabled)

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard D facilities in preference to writing your own low-level algorithms or facilities by hand. DRefLanguage and DReference are references to the D language and D standard library.

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.d

module bob;


unittest
{
const int allTestsEnabled = 0;

	assert(hey("Tom-ay-to, tom-aaaah-to.") == "Whatever.");
static if (allTestsEnabled)
{
	assert(hey("WATCH OUT!") == "Whoa, chill out!");
	assert(hey("Does this cryogenic chamber make me look fat?") == "Sure.");
	assert(hey("Let's go make out behind the gym!") == "Whatever.");
	assert(hey("It's OK if you don't want to go to the DMV.") == "Whatever.");
	assert(hey("WHAT THE HELL WERE YOU THINKING?") == "Whoa, chill out!");
	assert(hey("1, 2, 3 GO!") == "Whoa, chill out!");
	assert(hey("1, 2, 3") == "Whatever.");
	assert(hey("4?") == "Sure.");
	assert(hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!") == "Whoa, chill out!");
	assert(hey("I HATE YOU") == "Whoa, chill out!");
	assert(hey("Ending with a ? means a question.") == "Whatever.");
	assert(hey("Wait! Hang on.  Are you going to be OK?") == "Sure.");
	assert(hey("Are you ok? ") == "Sure.");
	assert(hey("") == "Fine. Be that way!");
	assert(hey("   ") == "Fine. Be that way!");
	assert(hey(" A bit of silence can be nice.  ") == "Whatever.");
}

}
module bob;

import std.algorithm : equal;
import std.regex : matchFirst;
import std.uni : asUpperCase, asLowerCase;

unittest {
	assert(hey("Tom-ay-to, tom-aaaah-to.") == "Whatever.");
	assert(hey("WATCH OUT!") == "Whoa, chill out!");
	assert(hey("Does this cryogenic chamber make me look fat?") == "Sure.");
	assert(hey("Let's go make out behind the gym!") == "Whatever.");
	assert(hey("It's OK if you don't want to go to the DMV.") == "Whatever.");
	assert(hey("WHAT THE HELL WERE YOU THINKING?") == "Whoa, chill out!");
	assert(hey("1, 2, 3 GO!") == "Whoa, chill out!");
	assert(hey("1, 2, 3") == "Whatever.");
	assert(hey("4?") == "Sure.");
	assert(hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!") == "Whoa, chill out!");
	assert(hey("I HATE YOU") == "Whoa, chill out!");
	assert(hey("Ending with a ? means a question.") == "Whatever.");
	assert(hey("Wait! Hang on.  Are you going to be OK?") == "Sure.");
	assert(hey("Are you ok? ") == "Sure.");
	assert(hey("") == "Fine. Be that way!");
	assert(hey("   ") == "Fine. Be that way!");
	assert(hey(" A bit of silence can be nice.  ") == "Whatever.");
}

string hey(const string s)
{
	if( matchFirst(s, "^[\\s]*$") )
		return "Fine. Be that way!";

	if(s.asUpperCase.equal(s) && !s.asLowerCase.equal(s))
		return "Whoa, chill out!";

	if( matchFirst(s, "\\?[\\s]*$") )
		return "Sure.";

	return "Whatever.";
}

Community comments

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

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?