Recent

Author Topic: What do you think of this possible code contest?  (Read 684 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1275
  • Professional amateur ;-P
What do you think of this possible code contest?
« on: June 11, 2025, 06:17:33 am »
Hey Y'All,

Has anyone ever stumbled on the "Hello World - Enterprise Edition"?
I've stumbled upon it on the Java side.
If so, what do you guys think about getting it done in Object Pascal?!
Kinda like the `1BRC` type of challenge?

Some useful links:
https://github.com/Hello-World-EE/Java-Hello-World-Enterprise-Edition
https://leo88.medium.com/hello-world-enterprise-edition-c3d1697b78a7

I've also mentioned this on the Discord server.

Cheers,
Gus
« Last Edit: June 11, 2025, 06:24:12 am by Gustavo 'Gus' Carreno »
Lazarus 4.99(main) FPC 3.3.1(main) Ubuntu 25.04 64b Dark Theme
http://github.com/gcarreno

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: What do you think of this possible code contest?
« Reply #1 on: June 11, 2025, 10:21:13 pm »
I have one in C on my webiste.
You need to scroll about half way down, to the "C versus Pascal" section.

Bart

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1275
  • Professional amateur ;-P
Re: What do you think of this possible code contest?
« Reply #2 on: June 11, 2025, 10:24:16 pm »
Hey Y'All,

Over on the Discord side, someone decided to port the Java one to Object Pascal:
https://github.com/pascalecu/BorlandHelloWorld

There's also one that I would recommend, just for the README:
https://github.com/ikelaiah/fp-hello-world

Cheers,
Gus
Lazarus 4.99(main) FPC 3.3.1(main) Ubuntu 25.04 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1275
  • Professional amateur ;-P
Re: What do you think of this possible code contest?
« Reply #3 on: June 11, 2025, 10:28:14 pm »
Hey Bart,

I have one in C on my webiste.
You need to scroll about half way down, to the "C versus Pascal" section.

Thanks!! That's lovely!!

Cheers,
Gus
Lazarus 4.99(main) FPC 3.3.1(main) Ubuntu 25.04 64b Dark Theme
http://github.com/gcarreno

TBMan

  • Full Member
  • ***
  • Posts: 198
Re: What do you think of this possible code contest?
« Reply #4 on: June 11, 2025, 10:46:08 pm »
I have one in C on my webiste.
You need to scroll about half way down, to the "C versus Pascal" section.

Bart

LOL, good one.

LV

  • Sr. Member
  • ****
  • Posts: 303
Re: What do you think of this possible code contest?
« Reply #5 on: June 12, 2025, 12:20:16 am »
Yes, learning a programming language starts with "Hello, World!". Here's a working Free Pascal program.  ;)

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$APPTYPE CONSOLE}
  5. {$MODESWITCH ADVANCEDRECORDS}
  6. {$OPTIMIZATION LEVEL4}
  7. {$ASSERTIONS ON}
  8.  
  9. uses
  10.   SysUtils,
  11.   Classes,
  12.   Generics.Collections,
  13.   Math,
  14.   DateUtils,
  15.   SyncObjs,
  16.   base64;
  17.  
  18. type
  19.   IGreetingStrategy = interface
  20.     ['{8D3F9E14-2C47-4F4D-AD01-3A5B6C7D8E9F}']
  21.     function GenerateGreeting: string;
  22.   end;
  23.  
  24.   TBase64Strategy = class(TInterfacedObject, IGreetingStrategy)
  25.   private
  26.     const
  27.       EncodedMessage = 'SGVsbG8sIFdvcmxkIQ==';
  28.     function GenerateGreeting: string;
  29.   end;
  30.  
  31.   TFibonacciStrategy = class(TInterfacedObject, IGreetingStrategy)
  32.   private
  33.     function GenerateGreeting: string;
  34.   end;
  35.  
  36.   TGreetingStrategyFactory = class
  37.   public
  38.     class function CreateStrategy(StrategyType: Integer): IGreetingStrategy;
  39.   end;
  40.  
  41.   TGreetingThread = class(TThread)
  42.   private
  43.     FStrategy: IGreetingStrategy;
  44.     FEvent: TEvent;
  45.     FGenerated: string;
  46.   protected
  47.     procedure Execute; override;
  48.   public
  49.     constructor Create(Strategy: IGreetingStrategy);
  50.     destructor Destroy; override;
  51.     procedure WaitForCompletion;
  52.     property Generated: string read FGenerated;
  53.   end;
  54.  
  55.   TGreetingValidator = class
  56.   public
  57.     class function ValidateGreeting(const Greeting: string): Boolean;
  58.   end;
  59.  
  60.   TGreetingLogger = class
  61.   private
  62.     class var FInstance: TGreetingLogger;
  63.     FLog: TStringList;
  64.     FLock: TCriticalSection;
  65.     constructor CreatePrivate;
  66.   public
  67.     class function GetInstance: TGreetingLogger;
  68.     procedure Log(const Msg: string);
  69.     destructor Destroy; override;
  70.   end;
  71.  
  72. { TBase64Strategy }
  73.  
  74. function TBase64Strategy.GenerateGreeting: string;
  75. begin
  76.   Result := DecodeStringBase64(EncodedMessage);
  77. end;
  78.  
  79. { TFibonacciStrategy }
  80.  
  81. function TFibonacciStrategy.GenerateGreeting: string;
  82. const
  83.   FibValues: array[0..12] of Integer = (
  84.     72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33
  85.   );
  86. var
  87.   i: Integer;
  88. begin
  89.   Result := '';
  90.   for i := Low(FibValues) to High(FibValues) do
  91.     Result := Result + Chr(FibValues[i]);
  92. end;
  93.  
  94. { TGreetingStrategyFactory }
  95.  
  96. class function TGreetingStrategyFactory.CreateStrategy(StrategyType: Integer): IGreetingStrategy;
  97. begin
  98.   case StrategyType of
  99.     0: Result := TBase64Strategy.Create;
  100.     1: Result := TFibonacciStrategy.Create;
  101.   else
  102.     raise Exception.Create('Invalid strategy type');
  103.   end;
  104. end;
  105.  
  106. { TGreetingThread }
  107.  
  108. constructor TGreetingThread.Create(Strategy: IGreetingStrategy);
  109. begin
  110.   inherited Create(True);
  111.   FreeOnTerminate := False;
  112.   FStrategy := Strategy;
  113.   FEvent := TEvent.Create(nil, True, False, '');
  114. end;
  115.  
  116. destructor TGreetingThread.Destroy;
  117. begin
  118.   FEvent.Free;
  119.   inherited Destroy;
  120. end;
  121.  
  122. procedure TGreetingThread.Execute;
  123. begin
  124.   try
  125.     FGenerated := FStrategy.GenerateGreeting;
  126.   except
  127.     on E: Exception do
  128.       FGenerated := 'Error: ' + E.Message;
  129.   end;
  130.   FEvent.SetEvent;
  131. end;
  132.  
  133. procedure TGreetingThread.WaitForCompletion;
  134. begin
  135.   FEvent.WaitFor(INFINITE);
  136. end;
  137.  
  138. { TGreetingValidator }
  139.  
  140. class function TGreetingValidator.ValidateGreeting(const Greeting: string): Boolean;
  141. begin
  142.   Result := Greeting = 'Hello, World!';
  143. end;
  144.  
  145. { TGreetingLogger }
  146.  
  147. constructor TGreetingLogger.CreatePrivate;
  148. begin
  149.   FLog := TStringList.Create;
  150.   FLock := TCriticalSection.Create;
  151. end;
  152.  
  153. class function TGreetingLogger.GetInstance: TGreetingLogger;
  154. begin
  155.   if not Assigned(FInstance) then
  156.     FInstance := TGreetingLogger.CreatePrivate;
  157.   Result := FInstance;
  158. end;
  159.  
  160. procedure TGreetingLogger.Log(const Msg: string);
  161. begin
  162.   FLock.Acquire;
  163.   try
  164.     FLog.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now) + ' - ' + Msg);
  165.     if FLog.Count > 10 then
  166.       FLog.SaveToFile('greeting_log.txt');
  167.   finally
  168.     FLock.Release;
  169.   end;
  170. end;
  171.  
  172. destructor TGreetingLogger.Destroy;
  173. begin
  174.   FLog.SaveToFile('greeting_log.txt');
  175.   FLog.Free;
  176.   FLock.Free;
  177.   inherited Destroy;
  178. end;
  179.  
  180. procedure RunApplication;
  181. var
  182.   Strategy: IGreetingStrategy;
  183.   Thread: TGreetingThread;
  184.   StartTime: TDateTime;
  185.   Elapsed: Double;
  186.   i: Integer;
  187.   StrategyNames: array[0..1] of string = ('Base64', 'Fibonacci');
  188. begin
  189.   StartTime := Now;
  190.   TGreetingLogger.GetInstance.Log('Application started');
  191.  
  192.   try
  193.     WriteLn('Testing greeting strategies:');
  194.  
  195.     for i := 0 to 1 do
  196.     begin
  197.       Write(StrategyNames[i], ' strategy: ');
  198.  
  199.       Strategy := TGreetingStrategyFactory.CreateStrategy(i);
  200.       Thread := TGreetingThread.Create(Strategy);
  201.       try
  202.         Thread.Start;
  203.         Thread.WaitForCompletion;
  204.  
  205.         Write('"', Thread.Generated, '"');
  206.  
  207.         if TGreetingValidator.ValidateGreeting(Thread.Generated) then
  208.         begin
  209.           WriteLn(' - OK');
  210.           TGreetingLogger.GetInstance.Log(Format('Strategy %d: Validation OK', [i]));
  211.         end
  212.         else
  213.         begin
  214.           WriteLn(' - FAILED');
  215.           TGreetingLogger.GetInstance.Log(Format('Strategy %d: Validation FAILED', [i]));
  216.         end;
  217.       finally
  218.         Thread.Free;
  219.       end;
  220.     end;
  221.  
  222.     Elapsed := MilliSecondsBetween(Now, StartTime);
  223.     TGreetingLogger.GetInstance.Log(Format('Application finished in %.2f ms', [Elapsed]));
  224.     WriteLn(Format('Total execution time: %.2f ms', [Elapsed]));
  225.  
  226.   except
  227.     on E: Exception do
  228.     begin
  229.       TGreetingLogger.GetInstance.Log('Error: ' + E.Message);
  230.       WriteLn('Critical error: ', E.Message);
  231.     end;
  232.   end;
  233. end;
  234.  
  235. begin
  236.   WriteLn('=== Hello World ===');
  237.   WriteLn('Running with Base64 and Fibonacci strategies...');
  238.   try
  239.     RunApplication;
  240.   finally
  241.     if Assigned(TGreetingLogger.FInstance) then
  242.       TGreetingLogger.FInstance.Free;
  243.   end;
  244.   WriteLn('Program completed. Press Enter to exit.');
  245.   ReadLn;
  246. end.
  247.  

Output:

Code: Text  [Select][+][-]
  1. === Hello World ===
  2. Running with Base64 and Fibonacci strategies...
  3. Testing greeting strategies:
  4. Base64 strategy: "Hello, World!" - OK
  5. Fibonacci strategy: "Hello, World!" - OK
  6. Total execution time: 3.00 ms
  7. Program completed. Press Enter to exit.
  8.  
:D

 

TinyPortal © 2005-2018