Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------------------------------supconcat.c
- #include <Python.h>
- static PyObject* supconcat_sconcat(PyObject* self, PyObject* args){
- PyObject * numArr;
- char * res_str;
- if (! PyArg_ParseTuple( args, "sO", &res_str, &numArr))
- return NULL;
- long long ans_length = 0;
- for (int i = 0; i < PyList_Size(numArr); i++) {
- PyObject* tmp_elem = PyList_GetItem(numArr, i);
- long numCount = PyLong_AsLong(tmp_elem);
- ans_length += numCount;
- }
- long long k = 0;
- for (int i = 0; i < PyList_Size(numArr); i++) {
- PyObject* tmp_elem = PyList_GetItem(numArr, i);
- long numCount = PyLong_AsLong(tmp_elem);
- for (long j = 0; j < numCount; j++) {
- res_str[k] = i + '0';
- k++;
- }
- }
- return Py_BuildValue("i", ans_length);
- }
- static char supconcat_sconcat_docs[] =
- "sconcat( ): do ...\n";
- static PyMethodDef supconcat_funcs[] = {
- {"sconcat", (PyCFunction)supconcat_sconcat, METH_VARARGS, supconcat_sconcat_docs},
- {NULL, NULL, 0, NULL}
- };
- static struct PyModuleDef supconcat =
- {
- PyModuleDef_HEAD_INIT,
- "supconcat", /* name of module */
- "usage: supconcat.sconcat()\n", /* module documentation, may be NULL */
- -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
- supconcat_funcs
- };
- PyMODINIT_FUNC PyInit_supconcat(void)
- {
- return PyModule_Create(&supconcat);
- }
- // ------------------------------------------setup.py
- from distutils.core import setup, Extension
- setup(name='supconcat', version='1.0', ext_modules=[Extension('supconcat', ['code\\supconcat.c'])])
- // python setup.py install - ввести в терминале, что бы потом импортировать
- // ------------------------------------------file_with_str.py
- import supconcat
- // testStr - длинная строка
- // numArr - массив с содержащий число вхождений каждой цифры в строку
- // Меняется сама строка testStr, и возвращается количество символов в ней
- c = supconcat.sconcat(testStr, numArr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement