Home python Count method algorithm in Python 3

Count method algorithm in Python 3

Author

Date

Category

What algorithm used developers in the Count method for a string in Python 3? What is the complexity of this algorithm?


Answer 1, Authority 100%

For the standard implementation of the Python interpreter, i.e. CPYTHON, As far as I understand , the following :

To search for the number of patterns of pattern in the string used Algorithm Boire – Mura . Since COUNT Returns the amount of uncountable entries of the substring in the string, the complexity of the work of the boiler – Moore will be O (n + m), where n + m is the sum of the length of the string and pattern.


I do not know very well in the structure of the source code CPYthon, but it looks very similar to that the count method is really described in the count.h given @ Suvitruf . The function described in it contains a single call – call the FastSearch function, the description of which I found in the FastSearch.h , which contains the option of the boiler algorithm – Mura.


Sources:


Answer 2, Authority 33%

Well, the algorithm itself here :

# ifndef stringlib_fastsearch_h
#Error Must Include "StringLib / FastSearch.h" Before Including This Module
#Endif
PY_LOCAL_INLINE (PY_SIZE_T)
StringLib (Count) (Const StringLib_char * Str, Py_ssize_t Str_len,
        const StringLib_char * sub, py_ssize_t sub_len,
        PY_SSIZE_T MAXCOUNT)
{
  PY_SSIZE_T COUNT;
  if (STR_LEN & LT; 0)
    Return 0; / * start & gt; LEN (STR) * /
  if (sub_len == 0)
    Return (STR_LEN & LT; MaxCount)? STR_LEN + 1: MAXCOUNT;
  Count = FastSearch (STR, STR_LEN, SUB, SUB_LEN, MAXCOUNT, FAST_COUNT);
  If (Count & LT; 0)
    Return 0; / * no Match * /
  RETURN COUNT;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions