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

dgeiger's solution

to Bob in the Delphi Pascal Track

Published at Sep 24 2020 · 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, such as "How are you?".

He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals).

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.

Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.

Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

Loading Exercises into Delphi

If Delphi is properly installed, and *.dpr file types have been associated with Delphi, then double clicking the supplied *.dpr file will start Delphi and load the exercise/project. control + F9 is the keyboard shortcut to compile the project or pressing F9 will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the File drop down menu.

When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the exercism/delphi/<exerciseName> directory.

For example, if you're submitting ubob.pas for the Bob exercise, the submit command would be something like exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas.

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 may request help from a mentor.

uBobTests.pas

unit uBobTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.4.0';

type

  [TestFixture]
  BobTests = class(TObject)
  public
    [Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure Stating_something;

    [Test]
    [Ignore]
    procedure Shouting;

    [Test]
    [Ignore]
    procedure Shouting_gibberish;

    [Test]
    [Ignore]
    procedure Asking_a_question;

    [Test]
    [Ignore]
    procedure Asking_a_numeric_question;

    [Test]
    [Ignore]
    procedure Asking_gibberish;

    [Test]
    [Ignore]
    procedure Talking_forcefully;

    [Test]
    [Ignore]
    procedure Using_acronyms_in_regular_speech;

    [Test]
    [Ignore]
    procedure Forceful_questions;

    [Test]
    [Ignore]
    procedure Shouting_numbers;

    [Test]
    [Ignore]
    procedure No_letters;

    [Test]
    [Ignore]
    procedure Question_with_no_letters;

    [Test]
    [Ignore]
    procedure Shouting_with_special_characters;

    [Test]
    [Ignore]
    procedure Shouting_with_no_exclamation_mark;

    [Test]
    [Ignore]
    procedure Statement_containing_question_mark;

    [Test]
    [Ignore]
    procedure non_letters_with_question;

    [Test]
    [Ignore]
    procedure Prattling_on;

    [Test]
    [Ignore]
    procedure Silence;

    [Test]
    [Ignore]
    procedure Prolonged_silence;

    [Test]
    [Ignore]
    procedure Alternate_silence;

    [Test]
    [Ignore]
    procedure Multiple_line_question;

    [Test]
    [Ignore]
    procedure Starting_with_whitespace;

    [Test]
    [Ignore]
    procedure Ending_with_whitespace;

    [Test]
    [Ignore]
    procedure Other_whitespace;

    [Test]
    [Ignore]
    procedure Non_question_ending_with_whitespace;
  end;

implementation
uses uBob;

procedure BobTests.Stating_something;
begin
  Assert.AreEqual('Whatever.', TBob.Response('Tom-ay-to, tom-aaaah-to.'));
end;

procedure BobTests.Shouting;
begin
  Assert.AreEqual('Whoa, chill out!', TBob.Response('WATCH OUT!'));
end;

procedure BobTests.Asking_a_question;
begin
  Assert.AreEqual('Sure.', TBob.Response('Does this cryogenic chamber make me look fat?'));
end;

procedure BobTests.Asking_gibberish;
begin
  Assert.AreEqual('Sure.',TBob.Response('fffbbcbeab?'));
end;

procedure BobTests.Ending_with_whitespace;
begin
  Assert.AreEqual('Sure.', TBob.Response('Okay if like my  spacebar  quite a bit?   '));
end;

procedure BobTests.Alternate_silence;
begin
  Assert.AreEqual('Fine. Be that way!', TBob.Response(#9#9#9#9#9#9#9#9#9#9));
end;

procedure BobTests.Asking_a_numeric_question;
begin
  Assert.AreEqual('Sure.', TBob.Response('You are, what, like 15?'));
end;

procedure BobTests.Talking_forcefully;
begin
  Assert.AreEqual('Whatever.', TBob.Response('Let''s go make out behind the gym!'));
end;

procedure BobTests.Using_acronyms_in_regular_speech;
begin
  Assert.AreEqual('Whatever.', TBob.Response('It''s OK if you don''t want to go to the DMV.'));
end;

procedure BobTests.Forceful_questions;
begin
  Assert.AreEqual('Calm down, I know what I''m doing!', TBob.Response('WHAT THE HELL WERE YOU THINKING?'));
end;

procedure BobTests.Shouting_gibberish;
begin
  Assert.AreEqual('Whoa, chill out!', TBob.Response('FCECDFCAAB'));
end;

procedure BobTests.Shouting_numbers;
begin
  Assert.AreEqual('Whoa, chill out!', TBob.Response('1, 2, 3 GO!'));
end;

procedure BobTests.No_letters;
begin
  Assert.AreEqual('Whatever.', TBob.Response('1, 2, 3'));
end;

procedure BobTests.Other_whitespace;
begin
  Assert.AreEqual('Fine. Be that way!', TBob.Response(#13#10 + ' ' + #9));
end;

procedure BobTests.Question_with_no_letters;
begin
  Assert.AreEqual('Sure.', TBob.Response('4?'));
end;

procedure BobTests.Shouting_with_special_characters;
begin
  Assert.AreEqual('Whoa, chill out!', TBob.Response('ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!'));
end;

procedure BobTests.Shouting_with_no_exclamation_mark;
begin
  Assert.AreEqual('Whoa, chill out!', TBob.Response('I HATE THE DMV'));
end;

procedure BobTests.Starting_with_whitespace;
begin
  Assert.AreEqual('Whatever.', TBob.Response('         hmmmmmmm...'));
end;

procedure BobTests.Statement_containing_question_mark;
begin
  Assert.AreEqual('Whatever.', TBob.Response('Ending with ? means a question.'));
end;

procedure BobTests.Prattling_on;
begin
  Assert.AreEqual('Sure.', TBob.Response('Wait! Hang on. Are you going to be OK?'));
end;

procedure BobTests.Silence;
begin
  Assert.AreEqual('Fine. Be that way!', TBob.Response(''));
end;

procedure BobTests.Prolonged_silence;
begin
  Assert.AreEqual('Fine. Be that way!', TBob.Response('    '));
end;

procedure BobTests.Multiple_line_question;
begin
  Assert.AreEqual('Whatever.', TBob.Response(#13#10 + 'Does this cryogenic chamber make me look fat?' + #13#10 + 'No.'));
end;

procedure BobTests.non_letters_with_question;
begin
  Assert.AreEqual('Sure.', TBob.Response(':) ?'));
end;

procedure BobTests.Non_question_ending_with_whitespace;
begin
  Assert.AreEqual('Whatever.', TBob.Response('This is a statement ending with whitespace      '));
end;

initialization
  TDUnitX.RegisterTestFixture(BobTests);
end.
unit uBob;

interface

uses
  System.SysUtils;

type
  TBob = class
    private
      class var FLetters: Boolean;
      class var FQuestion: Boolean;
      class var FSentence: String;
      class var FYelling: Boolean;

    public
      class function Response(Sentence: string): string;

  end;

implementation

{ TBob }

class function TBob.Response(Sentence: string): string;
var
  Index: Integer;
begin
  // Start with an empty string
  FSentence := '';

  // that has no letters.
  FLetters := False;

  // Scan through the passed string
  for Index := 1 to Length(Sentence) do
    // looking for digits, letters, punctuation, and spaces.
    if CharInSet(Sentence[Index], [' ', 'a'..'z', 'A'..'Z', '0'..'9', '?', ',', '.', '!']) then
      begin
        // If the current character is a digit, letter, punctuation mark, or space,
        // add it to our sentence.
        FSentence := FSentence + Sentence[Index];

        // If the current character is a letter, show that the sentence has
        // at least one letter
        if CharInSet(Sentence[Index], ['a'..'z', 'A'..'Z']) then
          FLetters := True;
      end;

  // Trim off the trailing spaces
  FSentence := Trim(FSentence);

  // Get the length of our sentence
  Index := Length(FSentence);

  // Is the length zero?
  if Index = 0 then
    // Yes, so we treat it as silence.
    Result := 'Fine. Be that way!'
  else
    // No, so now we have to see if it's a question or yelling
    begin
      // A question ends with a question mark
      FQuestion := FSentence[Index] = '?';

      // Yelling is when there's at least one letter and all letters are
      // uppercase
      FYelling := FLetters and (FSentence = UpperCase(FSentence));

      // Was there yelling?
      if FYelling then
        // Yes. Was this a question?
        if FQuestion then
          // Yes. Give the yelling question response
          Result := 'Calm down, I know what I''m doing!'
        else
          // No. Give the non-yelling question response
          Result := 'Whoa, chill out!'
      else
        // No yelling. Was this a question?
        if FQuestion then
          // Yes. Give the question response.
          Result := 'Sure.'
        else
          // No. Give the non-question response
          Result := 'Whatever.'
    end;
end;

end.

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?