Friday, July 23, 2021

Converting to C++ and refactoring #24 from Eugene Djobs Stream as of 12/25/2020

Writing C++ procedure below we get a final answer as C++ string , then via sequence of operators which convert string to  the pointer (say c) to "const char" and finally return required value via pointer to PyObject provided  by PyUnicode_FromString(c) to Python Runtime module.

    std::string answer = maxChar + std::to_string(maxFreq);
    const char *ars = answer.c_str();
    PyObject *result = PyUnicode_FromString(ars);
    return result;

Original task sounds like 

The text file contains only capital letters of the Latin alphabet (ABC ... Z). Identify the most common character in the file immediately after the letter X. In the answer, write down this symbol first, and then immediately (no separator) how many times it occurs after the letter X.

Python Code for this task 

with open('24-B251220.txt') as f:

  lines = f.readlines()

  for line in lines:

     letters = 'QWERTYUIOPASDFGHJKLZXCVBNM'

     count = [0]*26

     maxChar, maxFreq = ' ', 0

     for i in range(26):

        for j in range(1, len(line)):

           if line[j-1] == 'X' and line[j] == letters[i]:

              count[i] += 1

     for i in range(26):

       if  maxFreq < count[i]:

        maxFreq = count[i]

        maxChar = letters[i]

print(maxChar,maxFreq)

where 24-B251220.txt is just a row of length 1000000 bytes

***********************************************
If we would loop 50 times 24-B251220.txt >> 24-C251220.txt then we could wait for a while Python module above to exit
***********************************************
Create following test.cpp file for Python to C++ extension :

#include <iostream>
#include <fstream>
#include <string>
#include <cstddef>
#include <Python.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wreturn-type"

extern "C"{}
namespace {

static PyObject* XFrecuencyLetter(PyObject* self, PyObject* args)
{
   char *filename = NULL;
   int idx = 0;

   if(!PyArg_ParseTuple(args, "is", &idx, &filename)) {
        return NULL;
    }

   std::string letters = "QWERTYUIOPASDFGHJKLZXCVBNM";
   char alpha[27] =  "QWERTYUIOPASDFGHJKLZXCVBNM";
   int count [26] = {};
   int maxFreq = 0;
   char maxChar;
   std::fstream newfile;
   newfile.open(filename,std::ios::in);

   if (newfile.is_open()){
   std::string tp;
    while (getline(newfile, tp))
     {
      for(int i=1; i < 26;i++)
      {
       for(int j=1; j <= tp.size(); j++)
        {
          if ( tp.at(j-1) == alpha[idx]  && tp.at(j) == letters.at(i))
              count[i] += 1;
        }
      }
      for(int i=1; i < 26;i++)
      {
        if (maxFreq < count[i])
         {
          maxFreq = count[i];
          maxChar = letters[i];
         }
      }
     }
    std::string answer = maxChar + std::to_string(maxFreq);
    const char *ars = answer.c_str();
    PyObject *result = PyUnicode_FromString(ars);
    newfile.close();
    return result;
   }
};

static PyMethodDef myMethods[] = {
    { "XFrecuencyLetter",XFrecuencyLetter,METH_VARARGS, "Prints frequency letter" },
    { NULL, NULL, 0, NULL }
};

static struct PyModuleDef myModule = {
    PyModuleDef_HEAD_INIT,
    "myModule",
    "Test Module",
    -1,
    myMethods
};

PyMODINIT_FUNC PyInit_myModule(void)
{
    return PyModule_Create(&myModule);
};
} //namespace


Now create MyAlpha.py as follows to be able analyzed the question for any letter different from 'X'

(.env) [boris@fedora33server DJS24251220]$ cat MyAlpha.py
import myModule
Z = input("Input Letter you are concerned about : ")
alpha = ['Q','W','E','R','T','Y',',U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M']
index = alpha.index(Z)
file = input("Input filename : " )
print(myModule.XFrecuencyLetter(index,file))

******************************************
Build and install required shared library
******************************************
(.env) [boris@fedora33server DJS24251220]$ python setup.py install
running install
running build
running build_ext
building 'myModule' extension
creating build
creating build/temp.linux-x86_64-3.9
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/DJS24251220/.env/include -I/usr/include/python3.9 -c test.cpp -o build/temp.linux-x86_64-3.9/test.o
creating build/lib.linux-x86_64-3.9
g++ -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g build/temp.linux-x86_64-3.9/test.o -L/usr/lib64 -o build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so
running install_lib
copying build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so -> /home/boris/DJS24251220/.env/lib64/python3.9/site-packages
running install_egg_info
Removing /home/boris/DJS24251220/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info
Writing /home/boris/DJS24251220/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info

(.env) [boris@fedora33server DJS24251220]$ ll *.txt
-rw-rw-r--. 1 boris boris  1000000 Jul 21 21:33 24-B251220.txt
-rw-rw-r--. 1 boris boris 50000000 Jul 21 23:09 24-C251220.txt

Now test C++ perfomance via running

(.env) [boris@fedora33server DJS24251220]$ python MyAlpha.py
Input Letter you concerned about : X
Input filename : 24-B251220.txt
U1618

(.env) [boris@fedora33server DJS24251220]$ python MyAlpha.py
Input Letter you concerned about : X
Input filename : 24-C251220.txt
U80900

Then runtime for "C" file is around 1-2 sec.










































































Sunday, July 18, 2021

UPDATE as of 18/07/201 for C++ versus Python solving one problem from the field of number theory

 Consider following code implementing the same business logic as procVector.cpp

(.env) [boris@fedora33server NEWFORWARD]$ cat procVector2.cpp

#include <iostream>

#include <vector>

#include <cstddef>

#include <cmath>

#pragma GCC diagnostic ignored "-Wsign-compare"

namespace abc {

std::vector<std::vector<int> >  resultList(int m, int n)

{

   std::vector<std::vector<int> > vec;

   std::vector<int> v1;

   int j,cCount,cntDiv;


    for (int i = m; i <= n  ; i++) {

       cCount = 0 ;

       cntDiv = 0 ; 

       j = 2 ;

      while (cCount <= 1 && j <= int(sqrt(i)))

      {

          if (i%j == 0)

          {

           cntDiv = j;

           cCount +=1;

          }

          j += 1;

      }           

      if (cCount == 1 && cntDiv != i/cntDiv) 

      {        

         v1.push_back(cntDiv);

         v1.push_back(i/cntDiv);

         vec.push_back(v1);

         while (!v1.empty())

         {

             v1.pop_back();

         }

       }

     } 

    return vec;

};

} //namespace

Now update setup.py as follows and reinstall

from distutils.core import setup, Extension

import sysconfig

language = 'c++'

std = 'c++20'

default_compile_args = sysconfig.get_config_var('CFLAGS').split()

extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]

setup(name = 'myModule', version = '1.0',  \

   ext_modules = [Extension('myModule', ['mainVector.cpp','procVector2.cpp'])])

Now update MyProg.py

(.env) [boris@fedora33server NEWFORWARD]$ cat MyProg.py

import myModule

# a = int(input("Input start   number : "))

# b = int(input("Input finish  number : "))

# a = 174457

# b = 174505

a = 1744457

b = 3044505

myModule.resList(a,b)

lines = []

with open('./outvector.dat') as f:

    lines = f.readlines()

Run with new version 

for line in lines:

    print(line)

Now run with new version of procVector.cpp

(.env) [boris@fedora33server NEWFORWARD]$ cat ./speed.sh
date
python MyProg.py
date
(.env) [boris@fedora33server NEWFORWARD]$ ./speed.sh | tee log

(.env) [boris@fedora33server NEWFORWARD]$ head -5 log

Sun Jul 18 06:49:48 PM MSK 2021
643 2713 

2 872231 

(.env) [boris@fedora33server NEWFORWARD]$ tail -5 log
3 1014833 

199 15299 

Sun Jul 18 06:49:52 PM MSK 2021


Friday, July 16, 2021

C++ versus Python solving one interesting problem from the field of number theory

UPDATE as of 18/07/201 for C++ versus Python solving one problem from the field of number theory 

The version of C++ code here differs from the first one been posted at Lxer.com . Vector "v1" been pushed back by 2D vector "vec" is supposed to be re-declared before every next push  back by "vec" versus option of been cleaned up in previous version of compilation. I just believe that attempt to follow previous schema doesn't work properly as it did before due to pushing back the same variable "j" by vector "v1" in the loop preparing "v1" to be pushed by "vec".  

Original problem itself

Write a program that searches among the integers that belong to the numerical segment [174457;174505], numbers that have exactly two different natural divisors, not counting the unit and the number itself. For each of the found number, write these two divisors into the table on the screen with a new lines in ascending order of the product of these two divisors. Divisors in the table row must also follow in ascending order.

We intend to assembly  Python External C++ procedure using 2D vector to solve the the task mentioned  above for segment [174457;184505] without significant performance penalty, which would affect pure Python module runtime


(.env) [boris@fedora33server NEWFORWARD]$ cat procVector.h

#pragma once

#include <iostream>

#include <vector>

#include <cstddef>

namespace abc {

std::vector<std::vector<int> >  resultList(int m, int n);

}

(.env) [boris@fedora33server NEWFORWARD]$ cat procVector.cpp

#include <iostream>

#include <vector>

#include <cstddef>

#include <cmath>

#pragma GCC diagnostic ignored "-Wsign-compare"

namespace abc {

std::vector<std::vector<int> >  resultList(int m, int n)

{

   std::vector<std::vector<int> > vec;

   int j,cntDiv;

    for (int i = m; i <= n  ; i++) {

        std::vector<int> v1;    

        cntDiv = 0;

      for (j = 2; j < i; j++)

      {

          if (i%j == 0)

          {

              cntDiv +=1;

              v1.push_back(j);

          }

      }           

      if (cntDiv == 2)

      {        

         vec.push_back(v1);

      }

     } 

 return vec;

};

} //namespace















(.env) [boris@fedora33server NEWFORWARD]$ cat mainVector.cpp

#include <Python.h>

#include <iostream>

#include <vector>

#include <cstddef>

#include <fstream>

#include <cstdlib>

#include "procVector.h"

#pragma GCC diagnostic ignored "-Wsign-compare"

using std::endl;

using std::ofstream;

#pragma GCC diagnostic ignored "-Wsign-compare"

extern "C"{}

namespace {

static PyObject *resList(PyObject* self, PyObject* args)

{

    ofstream outdata;

    int m1,n1;

    if(!PyArg_ParseTuple(args,"ii",&m1,&n1))

        return NULL;

    outdata.open("./outvector.dat"); 

    std::vector<std::vector<int> > rez = abc::resultList(m1,n1);

    // Writing to file content of the 2D vector

    for (int i = 0; i < rez.size(); i++) {

       for (int j = 0; j < rez[i].size(); j++)

           outdata << rez[i][j] << " ";

       outdata << endl;

  }

    return Py_None ;

};

// Our Module's Function Definition structure

// We require this `NULL` to signal the end of our method

// definition

static PyMethodDef myMethodsTable[] = {

    { "resList", resList, METH_VARARGS, "Tracking factorial" },

    { NULL, NULL, 0, NULL }

};

// Our Module Definition struct

static struct PyModuleDef myModule = {

    PyModuleDef_HEAD_INIT,

    "myModule",

    "Test Module",

    -1,

    myMethodsTable

};

// Initializes our module using our above struct

PyMODINIT_FUNC PyInit_myModule(void)

{

    return PyModule_Create(&myModule);

};

} //namespace


(.env) [boris@fedora33server NEWFORWARD]$ cat setup.py

from distutils.core import setup, Extension

import sysconfig

language = 'c++'

std = 'c++20'

default_compile_args = sysconfig.get_config_var('CFLAGS').split()

extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]

setup(name = 'myModule', version = '1.0',  \

   ext_modules = [Extension('myModule', ['mainVector.cpp','procVector.cpp'])])

*******************************************************

Now build and install Shared Library containing required C++

procedure

*******************************************************

$ python setup.py install

(.env) [boris@fedora33server build]$ ls -CRl

.:

total 0

drwxrwxr-x. 2 boris boris 53 Jul 15 16:46 lib.linux-x86_64-3.9

drwxrwxr-x. 2 boris boris 46 Jul 15 16:46 temp.linux-x86_64-3.9

./lib.linux-x86_64-3.9:

total 224

-rwxrwxr-x. 1 boris boris 225856 Jul 15 16:46 myModule.cpython-39-x86_64-linux-gnu.so

./temp.linux-x86_64-3.9:

total 408

-rw-rw-r--. 1 boris boris 221464 Jul 15 16:46 mainVector.o

-rw-rw-r--. 1 boris boris 189056 Jul 15 16:46 procVector.o


(.env) [boris@fedora33server NEWFORWARD]$ cat MyProg.py

import myModule

# a = int(input("Input start   number : "))

# b = int(input("Input finish  number : "))

# Initial segment [174457;174505] intentionally increased 

# to make Python performance hardly acceptable

a = 174457

b = 184505

myModule.resList(a,b)

lines = []

with open('./outvector.dat') as f:

    lines = f.readlines()

for line in lines:

    print(line)

(.env) [boris@fedora33server NEWFORWARD]$ cat speed.sh

date

python MyProg.py

date

(.env) [boris@fedora33server NEWFORWARD]$ ./speed.sh

Thu Jul 15 04:46:20 PM MSK 2021

3 58153 

7 24923 

59 2957 

13 13421 

149 1171 

5 34897 

211 827 

. . . . . 

3 61487 

2 92233 

29 6361 

2 92237 

3 61493 

2 92243 

5 36899 

7 26357 

17 10853 

2 92251 

5 36901 

Thu Jul 15 04:46:24 PM MSK 2021



Thursday, July 15, 2021

C++ versus Python solving Problem #25 of Demo USE in Informatics 2021

The version of C++ code here differs from the first one been posted at Lxer.com . Vector "v1" been pushed back by 2D vector "vec" is supposed to be re-declared before every next push  back by "vec" versus option of been cleaned up in previous version of compilation. I just believe that attempt to follow previous schema doesn't work properly as it did before due to pushing back the same variable "j" by vector "v1" in the loop preparing "v1" to be pushed back by "vec".  

Original problem itself

Write a program that searches among the integers that belong to the numerical segment [174457; 174505], numbers that have exactly two different natural divisors, not counting the unit and the number itself. For each of the found number, write these two divisors into the table on the screen with a new lines in ascending order of the product of these two divisors. Divisors in the table row must also follow in ascending order.

We intend to assembly  Python External C++ procedure using 2D vector to solve the the task mentioned above for segment [174457; 184505]  without significant performance penalty, which would affect pure Python module runtime


(.env) [boris@fedora33server NEWFORWARD]$ cat procVector.h

#pragma once

#include <iostream>

#include <vector>

#include <cstddef>

namespace abc {

std::vector<std::vector<int> >  resultList(int m, int n);

}

(.env) [boris@fedora33server NEWFORWARD]$ cat procVector.cpp

#include <iostream>

#include <vector>

#include <cstddef>

#include <cmath>

#pragma GCC diagnostic ignored "-Wsign-compare"

namespace abc {

std::vector<std::vector<int> >  resultList(int m, int n)

{

   std::vector<std::vector<int> > vec;

   int j,cntDiv;

    for (int i = m; i <= n  ; i++) {

        std::vector<int> v1;    

        cntDiv = 0;

      for (j = 2; j < i; j++)

      {

          if (i%j == 0)

          {

              cntDiv +=1;

              v1.push_back(j);

          }

      }           

      if (cntDiv == 2)

      {        

         vec.push_back(v1);

      }

     } 

 return vec;

};

} //namespace















(.env) [boris@fedora33server NEWFORWARD]$ cat mainVector.cpp

#include <Python.h>

#include <iostream>

#include <vector>

#include <cstddef>

#include <fstream>

#include <cstdlib>

#include "procVector.h"

#pragma GCC diagnostic ignored "-Wsign-compare"

using std::endl;

using std::ofstream;

#pragma GCC diagnostic ignored "-Wsign-compare"

extern "C"{}

namespace {

static PyObject *resList(PyObject* self, PyObject* args)

{

    ofstream outdata;

    int m1,n1;

    if(!PyArg_ParseTuple(args,"ii",&m1,&n1))

        return NULL;

    outdata.open("./outvector.dat"); 

    std::vector<std::vector<int> > rez = abc::resultList(m1,n1);

    // Writing to file content of the 2D vector

    for (int i = 0; i < rez.size(); i++) {

       for (int j = 0; j < rez[i].size(); j++)

           outdata << rez[i][j] << " ";

       outdata << endl;

  }

    return Py_None ;

};

// Our Module's Function Definition structure

// We require this `NULL` to signal the end of our method

// definition

static PyMethodDef myMethodsTable[] = {

    { "resList", resList, METH_VARARGS, "Tracking factorial" },

    { NULL, NULL, 0, NULL }

};

// Our Module Definition struct

static struct PyModuleDef myModule = {

    PyModuleDef_HEAD_INIT,

    "myModule",

    "Test Module",

    -1,

    myMethodsTable

};

// Initializes our module using our above struct

PyMODINIT_FUNC PyInit_myModule(void)

{

    return PyModule_Create(&myModule);

};

} //namespace


(.env) [boris@fedora33server NEWFORWARD]$ cat setup.py

from distutils.core import setup, Extension

import sysconfig

language = 'c++'

std = 'c++20'

default_compile_args = sysconfig.get_config_var('CFLAGS').split()

extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]

setup(name = 'myModule', version = '1.0',  \

   ext_modules = [Extension('myModule', ['mainVector.cpp','procVector.cpp'])])

*******************************************************

Now build and install Shared Library containing required C++

procedure

*******************************************************

$ python setup.py install

(.env) [boris@fedora33server build]$ ls -CRl

.:

total 0

drwxrwxr-x. 2 boris boris 53 Jul 15 16:46 lib.linux-x86_64-3.9

drwxrwxr-x. 2 boris boris 46 Jul 15 16:46 temp.linux-x86_64-3.9

./lib.linux-x86_64-3.9:

total 224

-rwxrwxr-x. 1 boris boris 225856 Jul 15 16:46 myModule.cpython-39-x86_64-linux-gnu.so

./temp.linux-x86_64-3.9:

total 408

-rw-rw-r--. 1 boris boris 221464 Jul 15 16:46 mainVector.o

-rw-rw-r--. 1 boris boris 189056 Jul 15 16:46 procVector.o


(.env) [boris@fedora33server NEWFORWARD]$ cat MyProg.py

import myModule

# a = int(input("Input start   number : "))

# b = int(input("Input finish  number : "))

# Initial segment [174457;174505] intentionally increased 

# to make Python performance hardly acceptable

a = 174457

b = 184505

myModule.resList(a,b)

lines = []

with open('./outvector.dat') as f:

    lines = f.readlines()

count = 0

for line in lines:

    count += 1

    # print(f'{count}: {line}')

    print(line)

(.env) [boris@fedora33server NEWFORWARD]$ cat speed.sh

date

python MyProg.py

date

(.env) [boris@fedora33server NEWFORWARD]$ ./speed.sh

Thu Jul 15 04:46:20 PM MSK 2021

3 58153 

7 24923 

59 2957 

13 13421 

149 1171 

5 34897 

211 827 

. . . . . 

3 61487 

2 92233 

29 6361 

2 92237 

3 61493 

2 92243 

5 36899 

7 26357 

17 10853 

2 92251 

5 36901 

Thu Jul 15 04:46:24 PM MSK 2021

Sunday, July 11, 2021

Build Catalan Sequence via Python Extension to C++

Notice that Python module 

(.env) [boris@fedora33server CATALAN]$ cat mainCatalan.py

def Catl(n):

    if n == 0: return 1

    s = 0

    for i in range(n):

       s += Catl(i)*Catl(n-1-i)

    return s

def main():

    a = 0

    sequence = []

    a = int(input("Input desired number of Catalan sequence : "))

    for i in range(a):

          sequence.append(Catl(i))

    print(sequence)

if __name__ == "__main__":

    main()

 Actually hangs  ( run for ever)  when value of "a" becomes equal 20. Oops. It exits in 4 min 30 seconds 

***************************************************************

Thus as next exercise test Python Extension to C++ building Catalan list in a second up to value 35 of Catalan Sequence number.

****************************************************************

(.env) [boris@fedora33server CATALAN]$ cat catalan.h

#pragma once

#include <vector>

#include <cstddef>

namespace abc

{

std::vector<long> catalanList(int n) ;

}

(.env) [boris@fedora33server CATALAN]$ cat catalanProc.cpp

#include <iostream>

#include "catalan.h" 

#pragma GCC diagnostic ignored "-Wsign-compare"

namespace abc {

long catan(int n)

{

    long catalan[n + 1];

    catalan[0] = catalan[1] = 1;

    for (int i = 2; i <= n; i++) {

        catalan[i] = 0;

        for (int j = 0; j < i; j++)

            catalan[i] += catalan[j] * catalan[i - j - 1];

    }

    return catalan[n];

};

std::vector<long>  catalanList(int m)

{    

       std::vector<long> result;

       int j;

       for(j=0; j <= m; j++)

       {

           result.push_back(catan(j));

       }

     return result;

};

}  // namespace abc

(.env) [boris@fedora33server CATALAN]$ cat catalanSigma.cpp

#include <Python.h>

#include "catalan.h"

#pragma GCC diagnostic ignored "-Wsign-compare"

extern "C"{}

namespace {

static PyObject *catanList(PyObject* self, PyObject* args)

{

    int m;

    if(!PyArg_ParseTuple(args,"i",&m))

        return NULL;

    std::vector<long> sequence  = abc::catalanList(m);

    PyObject* result = PyList_New(sequence.size());

    for(int i = 0; i < sequence.size(); i++) {

        PyList_SetItem(result, i, PyLong_FromLong(sequence[i]));

    }

    return result;

};

// Our Module's Function Definition structure

// We require this `NULL` to signal the end of our method

// definition

static PyMethodDef myMethodsTable[] = {

    { "catanList",catanList, METH_VARARGS, "Tracking CatanList" },

    { NULL, NULL, 0, NULL }

};

// Our Module Definition struct

static struct PyModuleDef myModule = {

    PyModuleDef_HEAD_INIT,

    "myModule",

    "Test Module",

    -1,

    myMethodsTable

};

// Initializes our module using our above struct

PyMODINIT_FUNC PyInit_myModule(void)

{

    return PyModule_Create(&myModule);

};

} //namespace

(.env) [boris@fedora33server CATALAN]$ cat setup.py

from distutils.core import setup, Extension

import sysconfig

language = 'c++'

std = 'c++20'

default_compile_args = sysconfig.get_config_var('CFLAGS').split()

extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]

setup(name = 'myModule', version = '1.0',  \

   ext_modules = [Extension('myModule', ['catalanSigma.cpp','catalanProc.cpp'])])

(.env) [boris@fedora33server CATALAN]$ python setup.py install

running install

running build

running build_ext

building 'myModule' extension

creating build

creating build/temp.linux-x86_64-3.9

gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/CATALAN/.env/include -I/usr/include/python3.9 -c catalanProc.cpp -o build/temp.linux-x86_64-3.9/catalanProc.o

gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/CATALAN/.env/include -I/usr/include/python3.9 -c catalanSigma.cpp -o build/temp.linux-x86_64-3.9/catalanSigma.o

creating build/lib.linux-x86_64-3.9

g++ -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g build/temp.linux-x86_64-3.9/catalanProc.o build/temp.linux-x86_64-3.9/catalanSigma.o -L/usr/lib64 -o build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so

running install_lib

copying build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so -> /home/boris/CATALAN/.env/lib64/python3.9/site-packages

running install_egg_info

Removing /home/boris/CATALAN/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info

Writing /home/boris/CATALAN/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info


(.env) [boris@fedora33server CATALAN]$ cat  MyCatan.py

import myModule

number = int(input("Inpute desired number in Catalan sequence : " ))

print(myModule.catanList(number))

*****************************************************

This particular case addresses exactly the Yandex Q Question

*****************************************************

(.env) [boris@fedora33server CATALAN]$ python MyCatan.py

Inpute desired number in Catalan sequence : 10

[1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796]

(.env) [boris@fedora33server CATALAN]$ python MyCatan.py

Inpute desired number in Catalan sequence : 20

[1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 

2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420]

(.env) [boris@fedora33server CATALAN]$ python MyCatan.py

Inpute desired number in Catalan sequence : 30

[1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 

2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 

24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452, 

18367353072152, 69533550916004, 263747951750360, 1002242216651368, 3814986502092304]