Avatar of 4d47
0
0
Genius
0
1

4d47's solution

to Bob in the PHP Track

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

The commented tests at the bottom of the bob_test.php are Stretch Goals, they are optional. They may be easier to solve if you are using the mb_string functions, which aren't installed by default with every version of PHP.

Running the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
    
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
    
  3. Execute the tests:

     % ./phpunit.phar bob/bob_test.php
    

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

<?php
require_once "bob.php";

class BobTest extends PHPUnit\Framework\TestCase
{

    /**
     * @var \Bob
     */
    private $bob;

    public function setUp()
    {
        $this->bob = new Bob;
    }

    public function testStatingSomething()
    {
        $this->assertEquals("Whatever.", $this->bob->respondTo("Tom-ay-to, tom-aaaah-to."));
    }

    public function testShouting()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whoa, chill out!", $this->bob->respondTo("WATCH OUT!"));
    }

    public function testShoutingGibberish()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whoa, chill out!", $this->bob->respondTo("FCECDFCAAB"));
    }

    public function testAskingAQuestion()
    {
        $this->markTestSkipped();

        $this->assertEquals("Sure.", $this->bob->respondTo("Does this cryogenic chamber make me look fat?"));
    }

    public function testAskingANumericQuestion()
    {
        $this->markTestSkipped();

        $this->assertEquals("Sure.", $this->bob->respondTo("You are, what, like 15?"));
    }

    public function testAskingGibberish()
    {
        $this->markTestSkipped();

        $this->assertEquals("Sure.", $this->bob->respondTo("fffbbcbeab?"));
    }

    public function testTalkingForcefully()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whatever.", $this->bob->respondTo("Let's go make out behind the gym!"));
    }

    public function testUsingAcronymsInRegularSpeech()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whatever.", $this->bob->respondTo("It's OK if you don't want to go to the DMV."));
    }

    public function testForcefulQuestion()
    {
        $this->markTestSkipped();

        $this->assertEquals(
            "Calm down, I know what I'm doing!",
            $this->bob->respondTo("WHAT THE HELL WERE YOU THINKING?")
        );
    }

    public function testShoutingNumbers()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whoa, chill out!", $this->bob->respondTo("1, 2, 3 GO!"));
    }

    public function testOnlyNumbers()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whatever.", $this->bob->respondTo("1, 2, 3"));
    }

    public function testQuestionWithOnlyNumbers()
    {
        $this->markTestSkipped();

        $this->assertEquals("Sure.", $this->bob->respondTo("4?"));
    }

    public function testShoutingWithSpecialCharacters()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whoa, chill out!", $this->bob->respondTo("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"));
    }


    public function testShoutingWithNoExclamationMark()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whoa, chill out!", $this->bob->respondTo("I HATE YOU"));
    }

    public function testStatementContainingQuestionMark()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whatever.", $this->bob->respondTo("Ending with ? means a question."));
    }

    public function testNonLettersWithQuestion()
    {
        $this->markTestSkipped();

        $this->assertEquals("Sure.", $this->bob->respondTo(":) ?"));
    }

    public function testPrattlingOn()
    {
        $this->markTestSkipped();

        $this->assertEquals("Sure.", $this->bob->respondTo("Wait! Hang on. Are you going to be OK?"));
    }

    public function testSilence()
    {
        $this->markTestSkipped();

        $this->assertEquals("Fine. Be that way!", $this->bob->respondTo(""));
    }

    public function testProlongedSilence()
    {
        $this->markTestSkipped();

        $this->assertEquals("Fine. Be that way!", $this->bob->respondTo("         "));
    }

    public function testAlternateSilence()
    {
        $this->markTestSkipped();

        $this->assertEquals("Fine. Be that way!", $this->bob->respondTo("\t\t\t\t\t\t\t\t\t\t"));
    }

    public function testMultipleLineQuestion()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whatever.", $this->bob->respondTo("\nDoes this cryogenic chamber make me look fat?\nno"));
    }

    public function testStartingWithWhitespace()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whatever.", $this->bob->respondTo("        hmmmmmmm..."));
    }

    public function testEndingWithWhitespace()
    {
        $this->markTestSkipped();

        $this->assertEquals("Sure.", $this->bob->respondTo("Okay if like my  spacebar  quite a bit?   "));
    }


    public function testNonQuestionEndingWithWhitespace()
    {
        $this->markTestSkipped();

        $this->assertEquals("Whatever.", $this->bob->respondTo("This is a statement ending with whitespace      "));
    }

//    public function testOtherWhitespace()
//    {
//        $this->markTestSkipped();
//
//        $this->assertEquals("Fine. Be that way!", $this->bob->respondTo("\n\r \t\u{000b}\u{00a0}\u{2002}"));
//    }
//
//    public function testShoutingWithUmlauts()
//    {
//        $this->markTestSkipped();
//
//        $this->assertEquals("Whoa, chill out!", $this->bob->respondTo("ÜMLÄÜTS!"));
//    }
//
//    public function testCalmlySpeakingWithUmlauts()
//    {
//        $this->markTestSkipped();
//
//        $this->assertEquals("Whatever.", $this->bob->respondTo("ÜMLäÜTS!"));
//    }
}
<?php

class Bob {
    public function respondTo($phrase) {
        $phrase = self::trim($phrase);
        if (empty($phrase)) {
            return 'Fine. Be that way!';
        }
        if (self::hasLetter($phrase) && self::isUpperCase($phrase)) {
            return 'Whoa, chill out!';
        }
        if (mb_substr($phrase, -1) === '?') {
            return 'Sure.';
        }
        return 'Whatever.';
    }

    private static function trim($str) {
        return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $str);
    }

    private static function hasLetter($str) {
        return preg_match('/\pL/u', $str) === 1;
    }

    private static function isUpperCase($str) {
        return $str === mb_strtoupper($str);
    }
}

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?

Community comments

See what others have said about this solution
almost 2 years ago
4d47 says

Oh geez been a while, forgot it was already submitted :)