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

acolburn's solution

to Bob in the Delphi Pascal Track

Published at Feb 02 2021 · 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
  private
    class function askingQuestion(aStatement: string): boolean;
    class function yelling (aStatement: string): boolean;
    class function notSayingAnything (aStatement: string): boolean;
  public
    class function Response(aStatement: string): string;
  end;

implementation

uses
  System.SysUtils, System.StrUtils, System.Character;

{ TBob }

class function TBob.notSayingAnything(aStatement: string): boolean;
var
  i:integer;
begin
  result:=true;
  for i := 1 to length(aStatement) do
  begin
    if IsLetterorDigit(aStatement[i]) then
//    if IsLetter(aStatement[i]) then
    begin
      Exit(false);
    end; // end if
  end; // end for
end;

class function TBob.askingQuestion(aStatement: string): boolean;
begin
    if RightStr(Trim(aStatement), 1) = '?' then
    result := true else result:=false;
end;

class function TBob.Response(aStatement: string): string;
var
  isQuestion: Boolean;
  isYelling: Boolean;
  isNothing: Boolean;
begin
  isQuestion:=askingQuestion(aStatement);
  isYelling:=yelling(aStatement);
  isNothing:=notSayingAnything(aStatement);



  if isQuestion and isNothing then
    result := 'Sure.'
  //He says 'Fine. Be that way!' if you address him w/o actually saying anything.
  else if isNothing then
    result := 'Fine. Be that way!'
  //He answers 'Calm down, I know what I'm doing!' if you yell a question at him.
  else if isQuestion and isYelling then
    result := 'Calm down, I know what I''m doing!'
  //Bob answers 'Sure.' if you ask him a question, such as "How are you?".
  else if isQuestion then
    result := 'Sure.'
  //He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals).
  else if isYelling then
    result := 'Whoa, chill out!'
  else
  //He answers 'Whatever.' to anything else.
    result := 'Whatever.';

end;

class function TBob.yelling(aStatement: string): boolean;
var
  ch:char;
begin
  Result := False;
  for ch in aStatement do begin
    if (CharInSet(ch, ['A'..'Z'])) then  // all letters (uppercase) returns True
      Result := True
    else if (CharInSet(ch, ['a'..'z'])) then begin  // only one letter lowercase return False (and exit)
      Exit(False);
    end;
  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?