Recent

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

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Help me make a program please..
« Reply #45 on: July 20, 2017, 06:20:43 am »
This is my way of doing this, to the OP not for the competition.

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. function remove_char(c: char; s: string): string;
  4. var
  5.   i: integer;
  6. begin
  7.   result := '';
  8.   for i := 1 to Length(s) do
  9.     if (s[i] <> c) then
  10.       result := result + s[i];
  11. end;
  12.  
  13. function has_duplicate(index: integer; s: string): boolean;
  14. var
  15.   i: integer;
  16. begin
  17.   result := false;
  18.   for i := 1 to Length(s) do
  19.     if (i <> index) and (s[i] = s[index]) then
  20.     begin
  21.       result := true;
  22.       exit;
  23.     end;
  24. end;
  25.  
  26. function remove_duplicates(s: string): string;
  27. var
  28.   i: integer;
  29. begin
  30.   i := 1;
  31.   while(i <= Length(s)) do
  32.   begin
  33.     if (has_duplicate(i, s)) then
  34.     begin
  35.       s := remove_char(s[i],s);
  36.       if (i = 1) then
  37.         Dec(i);
  38.     end;
  39.     Inc(i);
  40.   end;
  41.   result := s;
  42. end;
  43.  
  44. begin
  45.   writeln(remove_duplicates('abcba'));
  46.   writeln(remove_duplicates('hello my friend hello something'));
  47.   readln();
  48. end.

My code was (hopefully not anymore) faulty, thanks to molly for his test case 'hello my friend hello something'.

BTW this tool is useful to write weird sentences, :D
« Last Edit: July 20, 2017, 06:23:50 am by lainz »

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Help me make a program please..
« Reply #46 on: July 20, 2017, 06:45:46 am »
but k is multiple loops in i primary loop!
So you find assigning k a new value inside a repeat loop, a loop?
So even k:=1 is a loop now?
Real strange defenition of loop.

Like I said, in that case you'll grab on cpu-cycles as a loop too.
The code from josh just has a repeat/until, where inside it, values are assigned. The fact that sometimes the value jumps back or forward isn' a classical pascal loop. I guess we can all stop trying as you'll always find something else.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Help me make a program please..
« Reply #47 on: July 20, 2017, 06:54:22 am »
but k is multiple loops in i primary loop!
So you find assigning k a new value inside a repeat loop, a loop?
So even k:=1 is a loop now?
Real strange defenition of loop.

Like I said, in that case you'll grab on cpu-cycles as a loop too.
The code from josh just has a repeat/until, where inside it, values are assigned. The fact that sometimes the value jumps back or forward isn' a classical pascal loop. I guess we can all stop trying as you'll always find something else.

of course you are right.

and maybe he thinks 'a loop' as a linear 0 to n, but that's not possible.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Help me make a program please..
« Reply #48 on: July 20, 2017, 09:00:30 am »
"Pos" is a loop too.

Copy() probably also (at least when you look at the assembler output).
And I guess writeln() also is, maybe not the asm outut of fpc, but it will call a system library of the underlying OS that definitely will use some sort of loop.

The fact that you didnt write it in your own code, but used a loop implemented in a library does not matter.

I tend to disagree.
Where does that argument stop, at the assembler level?

Pos() is in the system library, the implementation of this should not matter.

Anyway: Pos() can be replaced by a function that does not use a loop (if we define a loop to be a Pascal construct that uses either "for", "while" or "repeat").

The code was merely a proof of concept.

Bart
« Last Edit: July 20, 2017, 09:10:19 am by Bart »

Josh

  • Hero Member
  • *****
  • Posts: 1270
Re: Help me make a program please..
« Reply #49 on: July 20, 2017, 09:27:55 am »
@Molly
Maybe adding a quick if then at the beginning to alleviate in the beginning.

Also removed if s='' and replaced with if le<=1 in case there is a loop in the string comparison routine.
Code: Pascal  [Select][+][-]
  1.   if le<=1 then
  2.   begin
  3.     result:=s;
  4.     exit;
  5.   end;
  6.  result:='';
  7. .....
  8.  
  9.  
« Last Edit: July 20, 2017, 09:43:31 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 #50 on: July 20, 2017, 10:24:09 am »
@Molly
Maybe adding a quick if then at the beginning to alleviate in the beginning.
Yes, that is indeed a possible solution (quickest in execution time).

I used:
Code: Pascal  [Select][+][-]
  1. begin
  2.   if le <= 1 then exit(s);
  3.   result:='';
  4.   ...
  5. end;
  6.  

Quote
Also removed if s='' and replaced with if le<=1 in case there is a loop in the string comparison routine.
Also my thought  :)

In case you wish to stay true to your original/initial design, you could also remove the above length check and opt for:
Code: Pascal  [Select][+][-]
  1.       ...
  2.       if nw<>'' then
  3.       begin
  4.         inc(sz);
  5.         ans[sz]:=nw[1]; // instead of: ans[sz]:=ch;
  6.       end;
  7.       ...
  8.  

At least, that is my understanding *morning-yawn*

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #51 on: July 20, 2017, 10:34:53 am »
My code was (hopefully not anymore) faulty, thanks to molly for his test case 'hello my friend hello something'.
Thank for the compliment, although you discovered your own bug and fixed it  :)

Thank you for your contribution (even though it was not meant for a competitive comparison).

In case you're interested, a small test-suite:
Code: Pascal  [Select][+][-]
  1. program test_dupes;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. function RemoveDups_lazy(S: String): String;
  9. var
  10.   Ch: Char;
  11. begin
  12.   for Ch in S do if (S.CountChar(Ch) > 1) then S := S.Replace(Ch, '', [rfReplaceAll]);
  13.   Result := S;
  14. end;
  15.  
  16.  
  17. function removeDups_lainz(s: string): string;
  18.   function has_duplicate(index: integer; s: string): boolean;
  19.   var
  20.     i: integer;
  21.   begin
  22.     result := false;
  23.     for i := 1 to Length(s) do
  24.       if (i <> index) and (s[i] = s[index]) then
  25.       begin
  26.         result := true;
  27.         exit;
  28.       end;
  29.   end;
  30.   function remove_char(c: char; s: string): string;
  31.   var
  32.     i: integer;
  33.   begin
  34.     result := '';
  35.     for i := 1 to Length(s) do
  36.       if (s[i] <> c) then
  37.         result := result + s[i];
  38.   end;
  39. var
  40.   i: integer;
  41. begin
  42.   i := 1;
  43.   while(i <= Length(s)) do
  44.   begin
  45.     if (has_duplicate(i, s)) then
  46.     begin
  47.       s := remove_char(s[i],s);
  48.       if (i = 1) then
  49.         Dec(i);
  50.     end;
  51.     Inc(i);
  52.   end;
  53.   result := s;
  54. end;
  55.  
  56. var
  57.   TestStrings : array[0..9] of string =
  58.   (
  59.     '',
  60.     'q',
  61.     'bb',
  62.     'asda',
  63.     'aa-hello-aa',
  64.     'XabababcbcbcYbcbdbdbdbdbababaZ',
  65.     'hello my friend hello something',
  66.     'the quick brown fox jumps over the lazy dog',
  67.     'sator arepo tenet opera rotas',
  68.     'party boobytrap'
  69.   );
  70.   S: String;
  71. begin
  72.   for S in TestStrings do
  73.   begin
  74.     WriteLn;
  75.     WriteLn( 'Results for "', S, '"');
  76.     WriteLn( 'lai: ', '"', RemoveDups_lainz(S), '"' );
  77.     WriteLn( 'laz: ', '"', RemoveDups_lazy (S), '"' );
  78.   end;
  79. end.
  80.  

My output for posted contributions so far reads:
Code: [Select]
Results for ""
bar: ""
rvk: ""
jos: ""
lai: ""
mol: ""
laz: ""

Results for "q"
bar: "q"
rvk: "q"
jos: "q"
lai: "q"
mol: "q"
laz: "q"

Results for "bb"
bar: ""
rvk: ""
jos: ""
lai: ""
mol: ""
laz: ""

Results for "asda"
bar: "sd"
rvk: "sd"
jos: "sd"
lai: "sd"
mol: "sd"
laz: "sd"

Results for "aa-hello-aa"
bar: "heo"
rvk: "heo"
jos: "heo"
lai: "heo"
mol: "heo"
laz: "heo"

Results for "XabababcbcbcYbcbdbdbdbdbababaZ"
bar: "XYZ"
rvk: "XYZ"
jos: "XYZ"
lai: "XYZ"
mol: "XYZ"
laz: "XYZ"

Results for "hello my friend hello something"
bar: "yfrdstg"
rvk: "yfrdstg"
jos: "yfrdstg"
lai: "yfrdstg"
mol: "yfrdstg"
laz: "yfrdstg"

Results for "the quick brown fox jumps over the lazy dog"
bar: "qickbwnfxjmpsvlazydg"
rvk: "qickbwnfxjmpsvlazydg"
jos: "qickbwnfxjmpsvlazydg"
lai: "qickbwnfxjmpsvlazydg"
mol: "qickbwnfxjmpsvlazydg"
laz: "qickbwnfxjmpsvlazydg"

Results for "sator arepo tenet opera rotas"
bar: "n"
rvk: "n"
jos: "n"
lai: "n"
mol: "n"
laz: "n"

Results for "party boobytrap"
bar: " "
rvk: " "
jos: " "
lai: " "
mol: " "
laz: " "

I've shortened the test-suite a little, as it was out of scope otherwise. Timing could easily be added, or a iteration counter to show how effective some routines are.

Quote
BTW this tool is useful to write weird sentences, :D
Some produced strings makes heads nor tails though  :D

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Help me make a program please..
« Reply #52 on: July 20, 2017, 10:38:23 am »
TEASER!!!
Code: Pascal  [Select][+][-]
  1. var
  2.   a:set of char;
  3.   s:string = 'babcba';
  4.   t:char;
  5. begin
  6.   for t in s do
  7.   begin
  8.     if not (t in a) then write(t);  // print in-order
  9.     include(a,t); // include will ignore duplicates
  10.   end;         
  11. end.

Now you can solve the original question in the same manner, but that is homework.... for all of you...Except Bart: he spotted it is just one loop... KUDOS Bart!

The correct answer is a slight (very slight) modification of the above code....
« Last Edit: July 20, 2017, 10:55:18 am by Thaddy »
Specialize a type, not a var.

Josh

  • Hero Member
  • *****
  • Posts: 1270
Re: Help me make a program please..
« Reply #53 on: July 20, 2017, 10:43:50 am »
@Molly
Good points, also forgot that assigning a string could also possibly cause a loop

Modified the code
Code: Pascal  [Select][+][-]
  1. function Dups(s:shortstring):String;
  2. var i,k:Integer;
  3.   ch:char;
  4.   nw:shortstring;
  5.   le:Byte absolute s[0];
  6.   ans:shortstring;
  7.   sz:Byte absolute ans[0];
  8.  
  9. begin
  10.   if le <= 1 then exit(s);
  11.   result:='';
  12.   i:=1;
  13.   k:=1;
  14.   nw[0]:=chr(1);
  15.   nw[1]:=s[1];
  16.   sz:=0;
  17.   repeat
  18.     if i=k then inc(k)  // do not check current char with itself
  19.     else
  20.     begin
  21.       if s[i]=s[k] then
  22.       begin
  23.         nw[0]:=chr(0);
  24.       end;
  25.       inc(k);
  26.     end;
  27.     if k>le then
  28.     begin
  29.       if nw[0]<>chr(0) then
  30.       begin
  31.         inc(sz);
  32.         ans[sz]:=s[i];
  33.       end;
  34.       nw[0]:=chr(1);
  35.       inc(i);
  36.       nw:=s[i];
  37.       k:=1;
  38.     end;
  39.   until i>le;
  40.   result:=ans;
  41. end;
  42.  
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Help me make a program please..
« Reply #54 on: July 20, 2017, 10:47:36 am »
@Josh... you do need to iterate over a sequential series, but there is technically no loop required: a loop means you revert back to old positions in a series.
And the problem takes a linear iteration over the original string. (Oh well, call it a loop, Bart did... >:D 8-) O:-))

Why do people forget to write simple code? <very grumpy  >:D >:D >:D>  And your code just complicates matters. Pascal is a way better language than condoning such code, I dismiss it. Now look at my example... Do I have to give away the answer? It is still homework. Apparently for you and Molly too.... O:-) Use the language!
« Last Edit: July 20, 2017, 12:16:23 pm by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Help me make a program please..
« Reply #55 on: July 20, 2017, 06:26:46 pm »
@Molly
@josh
Two persons here that should warrant replacing teaser by taser?
Specialize a type, not a var.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #56 on: July 20, 2017, 06:33:51 pm »
@Thaddy:

Showing the same code as i did in this post, and me not persuing that approach you can safely assume that i did not 'spotted it' (otherwise i would have seen it back then, and i would not have started another approach).

So, unless there is some recursion involved, then i still don't get it.

I have my flaws (actually, i have many)  :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Help me make a program please..
« Reply #57 on: July 20, 2017, 07:30:45 pm »
@Molly:
don't feel offended. In Pascal everything is a one liner..... :) :D ;D I missed your post. :'( :-* :-X :-[ :-[ :-[ :-[
Anyway. Loads of pages of code, can be solved and *must* be solved much easier...
(you KNOW the solution, the rest does not know: hint for those: keep state in two places. Can be even more elegant but that keeps things understandable)

It is not to make a point. It is about the elegance of the language. Set operators are masks...Not... loops.... O:-)

[edit] And no: the code is very similar but not the same after review: I maintain order of occurrence as well. But I doubt that makes any difference... (old hands...)
« Last Edit: July 20, 2017, 07:52:57 pm by Thaddy »
Specialize a type, not a var.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #58 on: July 20, 2017, 07:55:18 pm »
@Molly:
don't feel offended. In Pascal everything is a one liner..... :) :D ;D I missed your post. :'( :-* :-X :-[ :-[ :-[ :-[
No problem. i am not offended at all.

It just annoys me to hell that i'm overlooking something obvious, so close in front of me that i can't seem to spot it. So, instead i get frustrated over my own stupidity :)

fwiw: i have tried using intersections, but got stuck. Seems that i have to try again ;D (Although not today as i'm steamed by this absurd humidity atm)

 

TinyPortal © 2005-2018