Recent

Author Topic: Contest: fastest IsAnagram function  (Read 9608 times)

BrunoK

  • Hero Member
  • *****
  • Posts: 623
  • Retired programmer
Re: Contest: fastest IsAnagram function
« Reply #180 on: November 02, 2024, 06:44:16 pm »
@Bart
I will fix it, of course, but since I've joined a bit late, can't I have a test bench source in some form. It is hard to orient in such a big topic... thanks!
My testbench, maybe it also works for you.

It's the set of tests I considered recently. The original program was proposed by ??? somewhere higher in the thread.

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: Contest: fastest IsAnagram function
« Reply #181 on: November 02, 2024, 06:50:51 pm »
I already showed relevant code that checks the validity of the algorithm's as well as the (valid and invalid) anagrams I throw at them.

Your code also accepts a #1 in the input, but it crashes if a #128 is in it:
Code: [Select]
Alpine: #0 ->TRUE (should be False)
Alpine: #1 ->TRUE (should be False)
Alpine: #128 An unhandled exception occurred at $0040527B:
EAccessViolation: Access violation
  $0040527B
  $00405F13

Bart

alpine

  • Hero Member
  • *****
  • Posts: 1302
Re: Contest: fastest IsAnagram function
« Reply #182 on: November 03, 2024, 01:27:31 am »
My testbench, maybe it also works for you.
Thanks again.

I already showed relevant code that checks the validity of the algorithm's as well as the (valid and invalid) anagrams I throw at them.
If you mean post #97 (as you pointed to Warfrey) it contains just a snippets, no e.g. ValidAnagrams, InValidAnagrams.
Anyway, I can see my check failing for #0..#31 when put at the same positions in S1,S2, that is what you're checking about

EDIT: I'm withdrawing. Have no chance against others well-crafted solutions such as AVK's. Not without considerable effort. Sorry for the noise.
« Last Edit: November 03, 2024, 09:19:51 am by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: Contest: fastest IsAnagram function
« Reply #183 on: November 03, 2024, 08:11:22 pm »
Anyway, I can see my check failing for #0..#31 when put at the same positions in S1,S2, that is what you're checking about

No, your code says that #1#0 is a valid anagram of #0#1, but the rusles say that the input may not contain anything lower than #32 (or higher than #127).

it contains just a snippets, no e.g. ValidAnagrams, InValidAnagrams.
I also posted the ValidAnagrams I'm currently tested (posted them more tham once) as well as the invalid ones.

Bart

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 87
Re: Contest: fastest IsAnagram function
« Reply #184 on: November 03, 2024, 09:21:23 pm »
Code: Text  [Select][+][-]
  1. My code below "DEFINITELY" tells the user if "in the programs opinion"
  2. one string is an anagram of another string.
  3. ****I will entertain no disputes on this******** :D :D :D
  4.  


Code: Pascal  [Select][+][-]
  1. Function Is_Anagram (S1,s2:String): Boolean;
  2. Begin
  3. Result:= Random(2)=0;
  4. end;
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. begin
  8. With Button1 Do if (Is_Anagram(edit1.caption,edit2.caption)) then caption:='Is Anagram' else Caption:='Is Not Anagram';
  9. end;
  10.  
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: Contest: fastest IsAnagram function
« Reply #185 on: November 03, 2024, 11:04:36 pm »
Code: Text  [Select][+][-]
  1. My code below "DEFINITELY" tells the user if "in the programs opinion"
  2. one string is an anagram of another string.
  3. ****I will entertain no disputes on this******** :D :D :D
  4.  

Still slower than my dummy code (you can see in my test results it doesn't pass the validity test :D ):
Code: Pascal  [Select][+][-]
  1. function IsAnagram_Dummy(const S1, S2: String; IgnoreSpaces: Boolean = True; ExceptionOnError: Boolean = False): Boolean;
  2. begin
  3.   Result := True;
  4. end;  

Bart

alpine

  • Hero Member
  • *****
  • Posts: 1302
Re: Contest: fastest IsAnagram function
« Reply #186 on: November 04, 2024, 05:04:19 am »
Anyway, I can see my check failing for #0..#31 when put at the same positions in S1,S2, that is what you're checking about

No, your code says that #1#0 is a valid anagram of #0#1, but the rusles say that the input may not contain anything lower than #32 (or higher than #127).
"No" to what? Because you're telling me the same thing that I did.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: Contest: fastest IsAnagram function
« Reply #187 on: November 04, 2024, 12:15:13 pm »
"No" to what? Because you're telling me the same thing that I did.

Sorry for the confusion.
Anyway, I can see my check failing for #0..#31 when put at the same positions in S1,S2, that is what you're checking about

I interpreted that sentence as in: my validity test only checks #0..#32 in the same position and only that is illegal.
The validity test in fact is implemented in such a way that both input strings are the same illegal character.
The reason for that is that I can check every individual illegal character this way.
If I would e.g. check all characters in one string, then I cannot be sure which illegal character it misses, or if the failure may  be in another part of the code.
E.g. if I check #1#2#2#4#5#6#7#8#9 as input for both string paramters and your code returns False (as it should), it may be that it returns False because it accepts all characters, but the counting has a bug.
For the same reason it makes no sense to check e.g. #0 against #1. The code should return False, but it may do so because #0 <> #1 and not because it detects an illegal character.

Bart



O

  • New Member
  • *
  • Posts: 40
  • Creator of Pack
    • Pack
Re: Contest: fastest IsAnagram function
« Reply #188 on: November 04, 2024, 05:34:37 pm »
Here is a quick draft based on the reference code.
Code: Pascal  [Select][+][-]
  1.   function IsAnagram(const AValue1, AValue2: String; AIgnoreSpaces, AExceptionOnError: Boolean): Boolean;
  2.   const
  3.     Primes: array[Char] of Integer = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4.       0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
  5.       73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
  6.       191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307,
  7.       311, 313, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
  8.       241, 251, 257, 263, 269, 271, 277, 317, 331, 337, 347, 349, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9.       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  10.       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  11.       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12.       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  13.  
  14.     procedure Error(const AName: String; APosition: Integer); overload;
  15.     begin
  16.       Raise ERangeError.CreateFmt('IsAnagram: illegal character in ' + AName + ' at position %d', [APosition]);
  17.     end;
  18.  
  19.     function Process(const AName, AValue: String): QWord; inline;
  20.     var
  21.       I: Integer;
  22.     begin
  23.       Result := 1;
  24.       for I := 1 to Length(AValue) do
  25.         Result *= Primes[AValue[I]];
  26.       if Result = 0 then
  27.         if AExceptionOnError then
  28.           for I := 1 to Length(AValue) do
  29.             if Primes[AValue[I]] = 0 then
  30.               Error(AName, I);
  31.     end;
  32.  
  33.   begin
  34.     if (not AIgnoreSpaces) and (not AExceptionOnError) and (Length(AValue1) <> Length(AValue2)) then
  35.       Exit(False);
  36.     Primes[' '] := specialize IfThen<Integer>(AIgnoreSpaces, 1, 2);
  37.     Result := Process('S1', AValue1) = Process('S2', AValue2);
  38.   end;
  39.                                                                                                          

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: Contest: fastest IsAnagram function
« Reply #189 on: November 04, 2024, 06:19:27 pm »
Here is a quick draft based on the reference code.

Alas,
Code: [Select]
TestValidity for O
FAIL to detect invalid character #0
Validitycheck FAIL for O

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: Contest: fastest IsAnagram function
« Reply #190 on: November 04, 2024, 06:25:40 pm »
I think we should finish this thread.

The winner is: AVK.

Thanks to all of you.

 

TinyPortal © 2005-2018