i'm on a Stmartass mode awaiting other attempts!
Just sayin'
I worked on this logical 'basic' (LOL) problems from 1987 to 1996
so I'm pretty sure to never eat a shoe
Yeah, and if somebody does do it, you'll say the function Length() are loops too
(Who is the smartass now)
... the function actually calculates the length of the null-terminated string, and its execution time is proportional to the string length because the terminating null character is searched through a linear scan.
Edit: Ah, you already said so.
In that case, i am allowed to cheat as well, or am i not ?
program challenge_take2;
{$MODE OBJFPC}{$H+}
// Challenge: remove all characters that have dups from a string using a single loop
function removeDups(Source: shortstring): string;
Type
TStatus = (stCount, stCopy, stDone);
var
Counts : array[#0..#255] of integer;
CurrentChar : char;
Source_index : integer = 1;
Finished : boolean = false;
Status : TStatus;
Len : Byte absolute source[0];
begin
// yes, another loop. Blame the compiler as i'm too lazy
for CurrentChar := Low(Counts) to High(Counts) do
Counts[CurrentChar] := 0;
Result := '';
Status := stCount;
Source_index := 1;
while not finished do
begin
// WriteLn('Status = ', ord(Status), 'len = ', len, ' source_index = ', source_index);
if (Status = stCount) then
begin
if (Source_index <= len) then
begin
inc(Counts[Source[Source_index]], 1);
inc(Source_index);
end
else
begin
Source_index := 1;
Status := stCopy;
end;
end
else
if (Status = stCopy) then
begin
if (Source_index <= len) then
begin
if Counts[Source[Source_index]] = 1
then Result := Result + Source[source_index];
inc(Source_index);
end
else Status := stDone;
end
else
if (Status = stDone) then
begin
Finished := true;
end
end;
end;
begin
WriteLn(RemoveDups('hello my friend hello something'));
WriteLn(RemoveDups('aahelloaa'));
end.
Over-simplified version, so that anyone can follow it. Using one single While loop... now, where are those shoes.... Oh wait. i feel another restriction coming up
edit: pasted wrong version, corrected.