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

Sarahhuik's solution

to Bob in the Delphi Pascal Track

Published at Aug 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
type
  TBob = Class(TObject)
    private

      FResponse : string;
      Class function Capital(input : string) : boolean;
      Class function Question(input : string) : boolean;
      Class function Silence(input : string) : boolean;
    public
      FInput : string;
      Class function Response(input : string) : string;
  End;

implementation

uses
  System.SysUtils;

{ TBob }

Class function TBob.Capital(input: string): boolean;
var
  lChars : char;
begin
  Result := false;
  if input = AnsiUpperCase(input) then
  begin
    for lChars in input do
      if (lChars>= 'A') and (lChars<='Z') then
        Result := true;
  end;


end;

CLass function TBob.Question(input: string): boolean;
begin
  Result := false;
  if AnsiLastChar(input) = '?' then
    Result := True

end;

Class function TBob.Response(input: string): string;
var
  I : Integer;
begin
  input := trim(input);
  if Capital(input)then
  begin
    if Question(input) then
      Result := 'Calm down, I know what I''m doing!'
    else
      Result := 'Whoa, chill out!'
  end
  else if Question(input) then
    Result := 'Sure.'
  else if Silence(input) then
    Result := 'Fine. Be that way!'
  else
    Result := 'Whatever.'
end;

Class function TBob.Silence(input: string): boolean;
begin
  Result := false;
  if input = string.Empty then
    Result := True;
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?