Recent

Author Topic: Help me make a program please..  (Read 18307 times)

sam707

  • Guest
Re: Help me make a program please..
« Reply #15 on: July 19, 2017, 10:16:58 pm »
hehehehehe @Bart  :D

+10 @Martin_fr you just saved a poor french stomach

it seems Bart did not have enough salt for me to eat my shoes  8-)

anyone esle? hahahahahahha
« Last Edit: July 19, 2017, 10:28:05 pm by sam707 »

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Help me make a program please..
« Reply #16 on: July 19, 2017, 10:28:21 pm »
it seems Bart did not have enough salt for me to eat my shoes  8-)
anyone esle? hahahahahahha
Ok, will this do?
No Pos() (but a recursive function for seeking a character and that's NOT a loop).

(slightly revised version from Bart's)

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$Mode ObjFpc}
  3.  
  4. function SeekCharacter(C: Char; I: Integer; S: String): Integer;
  5. begin
  6.   Result := -1;
  7.   if I > Length(S) then exit;
  8.   if S[I] = C then Result := I;
  9.   if Result = -1 then Result := SeekCharacter(C, I + 1, S);
  10. end;
  11.  
  12. const
  13.   S = 'the quick brown fox jumped over the lazy dog';
  14. var
  15.   I: Integer;
  16.   Eliminated: String;
  17.   Res: String;
  18.   C: Char;
  19. begin
  20.   Eliminated := '';
  21.   Res := '';
  22.   for I := 1 to length(S) do
  23.   begin
  24.     C := S[I];
  25.     if (SeekCharacter(C, I + 1, S) = -1) and (SeekCharacter(C, 1, Eliminated) = -1) then
  26.       Res := Res + C
  27.     else Eliminated := Eliminated + C;
  28.   end;
  29.   writeln('Input:  ',S);
  30.   writeln('Output: ',Res);
  31.   writeln('Eliminated: ', Eliminated);
  32.   //Readln;
  33. end.
« Last Edit: July 19, 2017, 10:30:10 pm by rvk »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #17 on: July 19, 2017, 10:49:09 pm »
Unless i misunderstood (how to let someone eat a shoe  :P), perhaps this ?
Code: Pascal  [Select][+][-]
  1. program challenge;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. // Challenge: remove duplicated characters from string using a single loop
  6.  
  7. function removeDups(Source: string): string;
  8. var
  9.   Present: set of char = [];
  10.   CurrentChar : char;
  11. begin
  12.   Result := '';
  13.  
  14.   for CurrentChar in Source do
  15.   begin
  16.     if not (CurrentChar in Present) then
  17.     begin
  18.       include(Present, CurrentChar);
  19.       Result := Result + CurrentChar;
  20.     end;
  21.   end;
  22. end;
  23.  
  24. begin
  25.   WriteLn(RemoveDups('hello my friend hello something'));
  26. end.
  27.  

In case the in operator is also 'considered' a loop then that can be solved as well. It just makes the code more ugly.
« Last Edit: July 19, 2017, 10:51:12 pm by molly »

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Help me make a program please..
« Reply #18 on: July 19, 2017, 10:53:34 pm »
Unless i misunderstood (how to let someone eat a shoe  :P), perhaps this ?
No, the characters that have duplicates need to be completely eliminated.
In your code only the second and following duplicates are removed, not the first.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #19 on: July 19, 2017, 11:01:41 pm »
No, the characters that have duplicates need to be completely eliminated.
In your code only the second and following duplicates are removed, not the first.
aha, the shame that bestows upon me  :-[

In that case i read things wrongly. thanks for the correction rvk.

If things like pos are not allowed (e.g. are considered a loop) then afaik only recursion can solve this challenge (but that on itself could perhaps also be considered a loop).

sam707

  • Guest
Re: Help me make a program please..
« Reply #20 on: July 19, 2017, 11:11:29 pm »
badluck SeekCharacter also is a loop  :D :D :D

a recursive onstack one poor guy  im not born from last rain LOL
« Last Edit: July 19, 2017, 11:13:32 pm by sam707 »

sam707

  • Guest
Re: Help me make a program please..
« Reply #21 on: July 19, 2017, 11:14:39 pm »
i wont eat any of my shoe on a sad onstack "makeup" of loop hehehehehe

sam707

  • Guest
Re: Help me make a program please..
« Reply #22 on: July 19, 2017, 11:19:11 pm »
i'm on a Stmartass mode awaiting other attempts!
Just sayin'
I worked on this logical 'basics' (LOL) problems from 1987 to 1996

so I'm pretty sure to never eat a shoe
 :o :P :D
« Last Edit: July 19, 2017, 11:32:21 pm by sam707 »

sam707

  • Guest
Re: Help me make a program please..
« Reply #23 on: July 19, 2017, 11:29:58 pm »
furthermore, as I'm a bad guy  :P

even if the compiler had tricks to make it seems ONE loop, I would show you the assember result and PROOF there is PLENTY of loops,

because I also programmed such logics in C and asm  (z80, i286) :P

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Help me make a program please..
« Reply #24 on: July 19, 2017, 11:33:39 pm »
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
 :o :P :D
Yeah, and if somebody does do it, you'll say the function Length() are loops too  >:D
(Who is the smartass now)

Quote
... 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.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #25 on: July 20, 2017, 12:13:47 am »
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
 :o :P :D
Yeah, and if somebody does do it, you'll say the function Length() are loops too  >:D
(Who is the smartass now)

Quote
... 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 ?

Code: Pascal  [Select][+][-]
  1. program challenge_take2;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. // Challenge: remove all characters that have dups from a string using a single loop
  6.  
  7. function removeDups(Source: shortstring): string;
  8. Type
  9.   TStatus = (stCount, stCopy, stDone);
  10. var
  11.   Counts       : array[#0..#255] of integer;
  12.   CurrentChar  : char;
  13.   Source_index : integer = 1;
  14.   Finished     : boolean = false;
  15.   Status       : TStatus;
  16.   Len          : Byte absolute source[0];
  17. begin
  18.   // yes, another loop. Blame the compiler as i'm too lazy
  19.   for CurrentChar := Low(Counts) to High(Counts) do
  20.     Counts[CurrentChar] := 0;
  21.  
  22.   Result := '';
  23.  
  24.   Status := stCount;
  25.  
  26.   Source_index := 1;
  27.  
  28.   while not finished do
  29.   begin
  30.     //    WriteLn('Status = ', ord(Status), 'len = ', len, ' source_index = ', source_index);
  31.     if (Status = stCount) then
  32.     begin
  33.       if (Source_index <= len) then
  34.       begin
  35.         inc(Counts[Source[Source_index]], 1);
  36.         inc(Source_index);
  37.       end
  38.       else
  39.       begin
  40.         Source_index := 1;
  41.         Status := stCopy;
  42.       end;
  43.     end
  44.     else
  45.     if (Status = stCopy) then
  46.     begin
  47.       if (Source_index <= len) then
  48.       begin
  49.         if Counts[Source[Source_index]] = 1
  50.         then Result := Result + Source[source_index];
  51.         inc(Source_index);
  52.       end
  53.       else Status := stDone;
  54.     end
  55.     else
  56.     if (Status = stDone) then
  57.     begin
  58.       Finished := true;
  59.     end
  60.   end;
  61. end;
  62.  
  63. begin
  64.   WriteLn(RemoveDups('hello my friend hello something'));
  65.   WriteLn(RemoveDups('aahelloaa'));
  66. end.
  67.  

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  :P

edit: pasted wrong version, corrected.
« Last Edit: July 20, 2017, 12:35:06 am by molly »

Josh

  • Hero Member
  • *****
  • Posts: 1270
Re: Help me make a program please..
« Reply #26 on: July 20, 2017, 12:27:19 am »
Does this count a single loop
Code: Pascal  [Select][+][-]
  1. function Dups(s:shortstring):String;
  2. var i,k:Integer;
  3.   ch:char;
  4.   nw:string;
  5.   le:Byte absolute s[0];
  6. begin
  7.   result:='';
  8.   i:=1;
  9.   k:=1;
  10.   nw:=s[1];
  11.   repeat
  12.     if i=k then inc(k)  // do not check current char with itself
  13.     else
  14.     begin
  15.       ch:=s[i];
  16.       if ch=s[k] then nw:='';
  17.       inc(k);
  18.     end;
  19.     if k>=le then
  20.     begin
  21.       inc(i);
  22.       result:=result+nw;
  23.       nw:=s[i];
  24.       k:=1;
  25.     end;
  26.   until i>le;
  27. end;        
  28.  

Thanks to Molly No Length either :)
« Last Edit: July 20, 2017, 12:45:35 am by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #27 on: July 20, 2017, 12:37:42 am »
Nice one josh  :)

i was about to call on the result initialization, as i got some weird output from your code  :-*

According to user sam, length is not allowed because internally it uses a loop to count the characters. Use a shortstring and String[0] (as i did) instead  :-X

sam707

  • Guest
Re: Help me make a program please..
« Reply #28 on: July 20, 2017, 12:45:05 am »
you all get now that when a little small ridiculous problem raise, your human brain think it is EASY because it can look global and local at the same time, BUT in computers world there's NOWAY to do this!

So im gona keep my shoes on, for some decades  :D :D :D
enven some centuries  :D :D :D

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Help me make a program please..
« Reply #29 on: July 20, 2017, 12:59:34 am »
you all get now that when a little small ridiculous problem raise, your human brain think it is EASY because it can look global and local at the same time, BUT in computers world there's NOWAY to do this!
Like molly showed, you could create one "while" loop in which you would reset the index multiple times to 1 when needed. In that case you would really have one loop. I'm tempted to do it in assembler so you wouldn't complain about compiler-loops etc... but my guess is that you then start about loops in the cpu itself. So I'm not going to bother.

Hey... even when it's possible... "the earth is spinning around its axes... yeah, we've got a loop there so I'm not going to eat my shoe".
« Last Edit: July 20, 2017, 01:01:26 am by rvk »

 

TinyPortal © 2005-2018