Recent

Author Topic: [Solved] A Tip for beginners  (Read 3931 times)

jamie

  • Hero Member
  • *****
  • Posts: 6801
Re: A Tip for beginners
« Reply #15 on: January 18, 2025, 04:58:33 pm »
I think it's faster to simply create an empty string with length set to the current length and then copy char to this string that is valid. At the end reset the length.
The only true wisdom is knowing you know nothing

tetrastes

  • Hero Member
  • *****
  • Posts: 632
Re: A Tip for beginners
« Reply #16 on: January 18, 2025, 05:04:36 pm »
Even simpler, without set/reset length:
Code: Pascal  [Select][+][-]
  1.   S := '412345446444474';
  2.   S1 := '';
  3.   for x := 1 to length(S) do if s[x] <> '4' then S1 := S1 + S[x];

But the aim of OP was not to find the best algorithm...

TRon

  • Hero Member
  • *****
  • Posts: 3946
Re: A Tip for beginners
« Reply #17 on: January 18, 2025, 05:05:52 pm »
@jamie:
While that is true (phun intended) I believe the idea from TS was for the beginner reading it to figure out.

We are all able to come up with better/other approaches as indicated by TS but it is besides the point. Using a loop this way is a classic beginner error and when not aware might throw that beginner off-guard. It seem to happen on a regular basis when someone post a similar question about deleting items from a list.
I do not have to remember anything anymore thanks to total-recall.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8204
Re: A Tip for beginners
« Reply #18 on: January 18, 2025, 05:09:54 pm »
I think it's faster to simply create an empty string with length set to the current length and then copy char to this string that is valid. At the end reset the length.

No doubt. However OP was trying to contribute a "guide for the perplexed" for the beginner who found himself afflicted by unexpected behaviour: not (I emphasise) a bug in the language definition or implementation, but usually resulting in a bug in the program being written.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

tetrastes

  • Hero Member
  • *****
  • Posts: 632
Re: A Tip for beginners
« Reply #19 on: January 18, 2025, 05:22:43 pm »
It's strange to me that someone understand this phrase verbatim. I think its meaning was clear in the context of discussion.
Of course I don't think that "to is buggy" by itself. And that "downto is better" always and everywhere.  :D

In that case choose your words better: "to is /inappropriate/ in this case" is the correct way of putting it, particularly in something which is labelled as being for beginners.

MarkMLl

I will improve, my general, sir!  O:-)  :D

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: A Tip for beginners
« Reply #20 on: January 18, 2025, 05:37:20 pm »
@tetrastes

You are a nitwit, a great pretender,
Interpreting smilies is an art. (not)
If you want to be right, then please be right. I think I was not offensive at all. And I was right and still am. That is a fact, not opinion. All you get is - later - noise and that noise does not deviate from the fact you have no clue.
The answer is simply about efficient memory handling. Nothing more. Whatever the string indexiing method: we have zero based strings too, btw.
Can the cause of the noise please shut up? - that is not you -

The correct answer is as I have given.

The noise can delve into {$ZEROBASEDSTRINGS ON}

 :P

Now show me wtf is a better answer than mine: you guessed, you can't. >:D >:D >:( >:( :o
« Last Edit: January 18, 2025, 05:50:46 pm by Thaddy »
But I am sure they don't want the Trumps back...

tetrastes

  • Hero Member
  • *****
  • Posts: 632
Re: A Tip for beginners
« Reply #21 on: January 18, 2025, 05:48:12 pm »
The answer is simply about efficient memory handling. Nothing more.

I showed you example of very inefficient, but properly working code, did I?

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: A Tip for beginners
« Reply #22 on: January 18, 2025, 05:52:12 pm »
Code works, but is slow for the reasons I explained: excessive memory copying and (re-de-) allocation. That is the beginner stuff.

It is not about if code works, it is about why the code is extremely inefficient.
The question is really about optimization and efficient algorithms. 8)
« Last Edit: January 18, 2025, 05:57:08 pm by Thaddy »
But I am sure they don't want the Trumps back...

440bx

  • Hero Member
  • *****
  • Posts: 4998
Re: A Tip for beginners
« Reply #23 on: January 18, 2025, 05:57:41 pm »
@tetrastes

You are a nitwit, a great pretender,
Look who's talking... LOL


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

tetrastes

  • Hero Member
  • *****
  • Posts: 632
Re: A Tip for beginners
« Reply #24 on: January 18, 2025, 06:11:05 pm »
It is not about if code works, it is about why the code is extremely inefficient.
The question is really about optimization and efficient algorithms. 8)

Clean your glasses and reread the first post.  8-)

dbannon

  • Hero Member
  • *****
  • Posts: 3215
    • tomboy-ng, a rewrite of the classic Tomboy
Re: A Tip for beginners
« Reply #25 on: January 18, 2025, 11:30:59 pm »
Seriously folks, I am getting quite worried about the trend here. If someone posts bad code, get stuck into it. If someone says something silly, call it out.

But there is absolutely no reason to be firing off personal insults, especially unsupportable ones. Apart from causing offense to people who are, basically like minded, it does leave questions about your own technical knowledge, if you cannot explain the technical problem, is it easier to attack the person ?

Please, a bit of respect for your fellow forum users please !

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

silvercoder70

  • Full Member
  • ***
  • Posts: 138
    • Tim Coates
Re: A Tip for beginners
« Reply #26 on: January 19, 2025, 01:15:01 pm »
It can work moving forwards ... :)

Code: Pascal  [Select][+][-]
  1. procedure RemoveCharInPlace(var str: string; charToRemove: Char);
  2. var
  3.   i, j: Integer;
  4. begin
  5.   j := 1;
  6.   for i := 1 to Length(str) do
  7.   begin
  8.     if str[i] <> charToRemove then
  9.     begin
  10.       if i <> j then // Only assign if i and j are different
  11.         str[j] := str[i];
  12.       Inc(j);
  13.     end;
  14.   end;
  15.   SetLength(str, j - 1);
  16. end;
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

dseligo

  • Hero Member
  • *****
  • Posts: 1460
Re: A Tip for beginners
« Reply #27 on: January 19, 2025, 03:05:08 pm »
It can work moving forwards ... :)

Nice. Although, this one would be hard to adapt to work with UTF.

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: A Tip for beginners
« Reply #28 on: January 19, 2025, 03:07:02 pm »
It is plain wrong, utf8 or not. the setlength causes a relocation.
But I am sure they don't want the Trumps back...

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1317
Re: A Tip for beginners
« Reply #29 on: January 20, 2025, 01:20:31 pm »
I’m surprised to see people arguing over something so basic.
Downto , {descending index} is the correct way to approach anything that might be reduced in length. Only a genuine newbie would not know this.

It has nothing to do with memory allocation , it has to do with the items referenced by indices changing when an item is deleted. In fact if anything is removed your program will probably crash when you reach the last index and try to reference something that is no longer there.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

 

TinyPortal © 2005-2018