Recent

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

sam707

  • Guest
Re: Help me make a program please..
« Reply #30 on: July 20, 2017, 01:05:13 am »
video wanted?? Ok i am definitively MEAN

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12397
  • Debugger - SynEdit - and more
    • wiki
Re: Help me make a program please..
« Reply #31 on: July 20, 2017, 01:22:40 am »
in pseudo code

counting the recursion as one "loop", as it iterates every char once.

Code: Pascal  [Select][+][-]
  1. cnt: array [char] of integer;
  2.  
  3. function filter(idx, ): string;
  4. begin
  5.    result := '';
  6.    if idx > length(source) then exit; // fpc stores length, so no loop
  7.  
  8.    chr= source[idx]
  9.    inc(cnt[chr]);
  10.    result = filter(idx+1);
  11.    if cnt[chr] = 1 then
  12.       result = chr + result;  // ok, the mem move could be counted as a loop
  13. end
  14.  
  15. res := filter(1)
  16.  
  17.  

to avoid the mem move when joining strings (chr + result), you can do it backward.
Start with
  res := filter(length(source))
Allocate enough mem for the result, then you can write the chr to the string starting from pos 1, into the already allocated memory. A final setlength would shorten the string, but not need to move it. So that would be one loop only.

If you must need eat something, then google "edible shoes". ;)
« Last Edit: July 20, 2017, 01:31:37 am by Martin_fr »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #32 on: July 20, 2017, 01:26:41 am »
@josh:
I think i found a small issue inside your code.

I got a wrong result with testing TS example "asda", which returned "asd"

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  // Should this not read: if k> le ?
  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.  

Line that afaik causes the issue, highlighted in above code.

PS: in case that was on purpose e.g. to not make it too easy for TS, then please let me know and i'll remove.
« Last Edit: July 20, 2017, 02:06:36 am by molly »

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Help me make a program please..
« Reply #33 on: July 20, 2017, 01:44:01 am »
Code: Pascal  [Select][+][-]
  1. const
  2.   S = 'the quick brown fox jumped over the lazy dog';
  3.  
jumps  :D
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #34 on: July 20, 2017, 02:15:37 am »
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".
There will always be sore losers. No offense intended to sam707.

Especially playing the asm card is just plain vile. As if i am responsible for whatever the compiler produces. And in case RTL does an API call and that code contains a loop then... i had the impression we were talking about Pascal loops here.

sam707

  • Guest
Re: Help me make a program please..
« Reply #35 on: July 20, 2017, 03:20:47 am »
dang  :o

@Martin_fr is about to make me eat a shoe

yup .. I was asking for an "inplace" solution (because by past we spared mem a lot)

but if you have a secondary buffer it is possible to do it, in one loop, still with the help  of the  'Set' checker

OMG my stomach HURTS hahahaha

for n = 1 to len
 if char[n] not in set then
 add to buffer and add to set
   otherwise do not add anywhere and
 continue loop in any case
...
 check if 1st char in set, then do final remove (delete(buffer,1,1) in case of
 return buffer

ima old looser gona eat a shoe all by myself hahahaha
I just killed meh myself and I
« Last Edit: July 20, 2017, 03:29:14 am by sam707 »

Josh

  • Hero Member
  • *****
  • Posts: 1455
Re: Help me make a program please..
« Reply #36 on: July 20, 2017, 03:22:52 am »
Hi Molly,

Well spotted, was an error, should have tested a bit more.

just in case addition of strings causes a loop, I have modified the routine below.
As I have not used shortstring and this 0 byte index for size of string before; is it safe to create and add to a shortstring this way. ie does a shortstring assign a 256 byte chunk of memory when it is declared?

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

sam707

  • Guest
Re: Help me make a program please..
« Reply #37 on: July 20, 2017, 03:24:03 am »
@molly noone is going to offend an old gamer, you are welcome as we are kidding

sam707

  • Guest
Re: Help me make a program please..
« Reply #38 on: July 20, 2017, 03:32:02 am »
 :D :D :D

I'm thinking... hmmm

now the HAPPY one should be @irppaann with his newbie school assignment he's gonna write 3 books

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #39 on: July 20, 2017, 03:35:11 am »
Hi Molly,

Well spotted, was an error, should have tested a bit more.
No problem.

Another one for you to wrap it up: pass an empty string and see what happens  :)

Quote
As I have not used shortstring and this 0 byte index for size of string before; is it safe to create and add to a shortstring this way. ie does a shortstring assign a 256 byte chunk of memory when it is declared?
According to documentation:
Quote
If the size of the string is not specified, 255 is taken as a default. The actual length of the string can be obtained with the Length standard runtime routine. For example in
and
Quote
For short strings, the length is stored in the character at index 0. Old Turbo Pascal code relies on this, and it is implemented similarly in Free Pascal.

Despite this, to write portable code, it is best to set the length of a shortstring with the SetLength call, and to retrieve it with the Length call. These functions will always work, whatever the internal representation of the shortstrings or other strings in use: this allows easy switching between the various string types.

So, to answer your question: yes: your code will do just fine. It is an array of 255 characters (while rest of code depends on the length byte to obtain real number of characters from it).

You can even use a absolute byte variable to you ans shortstring variable and change that iin case that is more convenient *wink*

PS: i was already impressed with your code using the length function. This shortstring stuff is from old TP days and in the ends is very limited (and might not even be future proof).
« Last Edit: July 20, 2017, 03:51:06 am by molly »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #40 on: July 20, 2017, 03:37:17 am »
@molly noone is going to offend an old gamer, you are welcome as we are kidding
No problem, you are correct there......  (but, it still hurts   >:D  :D  :-*)

...
 check if 1st char in set, then do final remove (delete(buffer,1,1) in case of
 return buffer
I was under the impression that delete() function also uses a loop to accomplish its task ?
« Last Edit: July 20, 2017, 04:07:14 am by molly »

Josh

  • Hero Member
  • *****
  • Posts: 1455
Re: Help me make a program please..
« Reply #41 on: July 20, 2017, 03:51:31 am »
So my final tweaked code below

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.   ans:shortstring;
  7.   sz:Byte absolute ans[0];
  8.  
  9. begin
  10.   if le<=1 then
  11.   begin
  12.     result:=s;
  13.     exit;
  14.   end;
  15.   result:='';
  16.   i:=1;
  17.   k:=1;
  18.   nw:=s[1];
  19.   sz:=0;
  20.   repeat
  21.     if i=k then inc(k)  // do not check current char with itself
  22.     else
  23.     begin
  24.       ch:=s[i];
  25.       if ch=s[k] then nw:='';
  26.       inc(k);
  27.     end;
  28.     if k>le then
  29.     begin
  30.       inc(i);
  31.       if nw<>'' then
  32.       begin
  33.         inc(sz);
  34.         ans[sz]:=ch;
  35.       end;
  36.       nw:=s[i];
  37.       k:=1;
  38.     end;
  39.   until i>le;
  40.   result:=ans;
  41. end;            
  42.  
« Last Edit: July 20, 2017, 09:42:35 am by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

sam707

  • Guest
Re: Help me make a program please..
« Reply #42 on: July 20, 2017, 04:11:51 am »
@josh nice impressive oldschool snippet

but k is multiple loops in i primary loop!

so far my prefered snippet still is the one from @Bart which use a Set

dont panic, it's impossible in one only loop because EACH chr needs to be checked againts all the others

i'll spare my money and not buy new shoes  :D
« Last Edit: July 20, 2017, 04:14:37 am by sam707 »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Help me make a program please..
« Reply #43 on: July 20, 2017, 04:13:42 am »
@josh:
neat  :)

Only goes wrong now for passing a single character for string. I'm currently too tired to see what goes wrong. I'll have a fresh look after some good nights rest  :)

sam707

  • Guest
Re: Help me make a program please..
« Reply #44 on: July 20, 2017, 04:17:57 am »
'in fine'
each not read chr of the array/string needs a loop against the others remaining forward, but... the author of the post didn't ask for 'one loop only', its a @Bart assertion ... so its all good
« Last Edit: July 20, 2017, 04:34:55 am by sam707 »

 

TinyPortal © 2005-2018