Raised This Month: $32 Target: $400
 8% 

Solved Symbol is never used


Post New Thread Reply   
 
Thread Tools Display Modes
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-17-2019 , 15:45   Re: "Symbol is never used"
Reply With Quote #11

Quote:
Originally Posted by Natsheh View Post
You are wrong and this is the statistics


* Use read_flags (twice)
* Compare using == operator

Or you can just order them alphabetically and then use equal
read_flags is converting to bits, my bad.

Still, using my stock is way more efficient than both of these.
__________________
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-17-2019 , 16:04   Re: "Symbol is never used"
Reply With Quote #12

Your solution is O(n ^ 2) where n is the length of the strings(they can have different lengths, it will still be O(n^2)). It doesn't really matter it's a single stock, what the stock does is what matters.

If your strings will have the same length then a better way is to:
1.sort both strings -> this will be O(nlogn) with a good sorting algorithm. Probably the default sort in amxx is O(nlogn).
2.Use one for loop and check char at position i from str1 with char at position i from str2. If you find a difference, return false. This will be O(n). (or simply use equal)

The solution is O(nlogn + n) = O(nlogn) which is bettern than O(n^2).

Another solution is to use 2 arrays as tables and count how many times each character appears in both strings. Then check if counts for each char are the same.
This solution is going to be O(n), which is even better.

Finally, if they are flags as Veco said, just do read_flags(str1) == read_flags(str2). This solution is O(n) and the best solution(because read_flags is O(n), you do it twice which is O(2n) = O(n)).
Your stock is the worst solution because it has the worst complexity. It's not a big deal in this case, but I felt like clearing things up.
__________________

Last edited by HamletEagle; 07-17-2019 at 16:14.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-17-2019 , 17:52   Re: "Symbol is never used"
Reply With Quote #13

Quote:
Originally Posted by HamletEagle View Post
If your strings will have the same length then a better way is to:
1.sort both strings -> this will be O(nlogn) with a good sorting algorithm. Probably the default sort in amxx is O(nlogn).
2.Use one for loop and check char at position i from str1 with char at position i from str2. If you find a difference, return false. This will be O(n). (or simply use equal)
Well, they most likely won't have the same length.

Quote:
Originally Posted by HamletEagle View Post
Another solution is to use 2 arrays as tables and count how many times each character appears in both strings. Then check if counts for each char are the same.
This solution is going to be O(n), which is even better.
Unsure what you meant by "count how many times each character appears in both strings", there's only one "a" and one "b" in flags? Or am I missing something?

Quote:
Originally Posted by HamletEagle View Post
Your stock is the worst solution because it has the worst complexity.
I don't find it complex at all, I twist things a lot though. But it works, so doesn't really matter as long as there isn't a huge difference.
__________________

Last edited by edon1337; 07-17-2019 at 17:54.
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-18-2019 , 04:10   Re: Symbol is never used
Reply With Quote #14

I am not saying your code is conplex, I am saying it has a bad complexity. The complexity can refer to how much memory the code uses(spacial complexity) or how many steps it does(temporal complexity).
When we want to know how fast a code is we estimate the temporal complexity.

In your code you have two loops, one inside the other.
When the first loop executes one time, the inner loop will execute n times, where n is the length of the string.
Therefore, for each iteration of the outer loop we have n iterations of the inner loop. However, the outer loop will execute n times too, which means in total your code.does at most n*n steps to find the answer.

Using 2 read_flags calls and comparing:
read_flags will traverse the string and calculate the bitsum. To traverse a string of length n the for will execute n times, so n steps.
We need to do read_flags 2 times so we do 2*n steps.
Then we have to compare the results of the 2 read_flags which we can do in one step(one ==).

We obtain that this method does 2*n + 1 steps. However, 1 is much "smaller" than n so we can ignore it.
2*n has the same asymptotic growth as n so the final complexity is n.

Doing n steps to get an answer is better than n^2. That is, if you really wanted to use the most efficient solution( as you said previously).
Because n is small(small strings), the difference may not be noticed.

That's how you would usually estimate the speed of your code. In amxx we also have to account for the pawn c++ interop time so a call to read_flags may be more expensive than n steps.

Note: when dealing with complexity we usually measure the worst case complexity(meaning the code cant do more stepts than that no matter what).
You have a break in the loops which may reduce the number of stepts the algorithm makes, but this should reflect in the best case complexity, not worst case.
__________________
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-18-2019 , 09:01   Re: Symbol is never used
Reply With Quote #15

Quote:
Originally Posted by HamletEagle View Post
I am not saying your code is conplex, I am saying it has a bad complexity. The complexity can refer to how much memory the code uses(spacial complexity) or how many steps it does(temporal complexity).
When we want to know how fast a code is we estimate the temporal complexity.

In your code you have two loops, one inside the other.
When the first loop executes one time, the inner loop will execute n times, where n is the length of the string.
Therefore, for each iteration of the outer loop we have n iterations of the inner loop. However, the outer loop will execute n times too, which means in total your code.does at most n*n steps to find the answer.

Using 2 read_flags calls and comparing:
read_flags will traverse the string and calculate the bitsum. To traverse a string of length n the for will execute n times, so n steps.
We need to do read_flags 2 times so we do 2*n steps.
Then we have to compare the results of the 2 read_flags which we can do in one step(one ==).

We obtain that this method does 2*n + 1 steps. However, 1 is much "smaller" than n so we can ignore it.
2*n has the same asymptotic growth as n so the final complexity is n.

Doing n steps to get an answer is better than n^2. That is, if you really wanted to use the most efficient solution( as you said previously).
Because n is small(small strings), the difference may not be noticed.

That's how you would usually estimate the speed of your code. In amxx we also have to account for the pawn c++ interop time so a call to read_flags may be more expensive than n steps.

Note: when dealing with complexity we usually measure the worst case complexity(meaning the code cant do more stepts than that no matter what).
You have a break in the loops which may reduce the number of stepts the algorithm makes, but this should reflect in the best case complexity, not worst case.
I'm using a so-called nested loop, you probably know it's widely used in other programming languages, just to explain what the code does to someone who might be interested:

Let's say you have these flags "abcdefgh" and you need "abcdef" to earn a new rank.
PHP Code:
public containi_allszStringSrc[ ], iSize1szStringCompare[ ], iSize2 )
{
    new 
bool:bEqual;

    for( new 
iiSize1i++ )
    {
        for( new 
jiSize2j++ )
        {
            if( 
szStringSrc] == szStringCompare] )
            {
                
bEqual true;
                break;
            }
            
            else
            {
                
bEqual false;
            }
        }
        
        if( ! 
bEqual )
        break;
    }
    
    return 
bEqual;

Basically it will loop through all of the String1, thus being the flags you need, abcdef.

So it will go like, a, b, c, d, e, f.
Each time it goes through a character, let's say 'a', it will also check the entire String2 (your flags) to see if you have the flag, if it's true, it will return true and break the inner loop, then will continue to the second flag, being 'b'. And it will continue doing the same thing.

It will also work if you have flags 'abcdef' and you need 'badcef' which are basically the same flags with different order, normal contain/i won't.
__________________

Last edited by edon1337; 07-18-2019 at 09:03.
edon1337 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:49.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode