Advertisement
FroztGal

string_task_mail

Apr 29th, 2021
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. // ------------------------------------------supconcat.c
  2.  
  3. #include <Python.h>
  4.  
  5. static PyObject* supconcat_sconcat(PyObject* self, PyObject* args){
  6.  
  7.   PyObject * numArr;
  8.   char * res_str;
  9.  
  10.   if (! PyArg_ParseTuple( args, "sO", &res_str, &numArr))
  11.     return NULL;
  12.  
  13.   long long ans_length = 0;
  14.  
  15.   for (int i = 0; i < PyList_Size(numArr); i++) {
  16.         PyObject* tmp_elem = PyList_GetItem(numArr, i);
  17.         long numCount = PyLong_AsLong(tmp_elem);
  18.         ans_length += numCount;
  19.   }
  20.  
  21.   long long k = 0;
  22.  
  23.   for (int i = 0; i < PyList_Size(numArr); i++) {
  24.         PyObject* tmp_elem = PyList_GetItem(numArr, i);
  25.         long numCount = PyLong_AsLong(tmp_elem);
  26.  
  27.         for (long j = 0; j < numCount; j++) {
  28.              res_str[k] = i + '0';
  29.              k++;
  30.         }
  31.   }
  32.  
  33.  
  34.   return Py_BuildValue("i", ans_length);
  35. }
  36.  
  37. static char supconcat_sconcat_docs[] =
  38.     "sconcat( ): do ...\n";
  39.  
  40. static PyMethodDef supconcat_funcs[] = {
  41.     {"sconcat", (PyCFunction)supconcat_sconcat, METH_VARARGS, supconcat_sconcat_docs},
  42.     {NULL, NULL, 0, NULL}
  43. };
  44.  
  45. static struct PyModuleDef supconcat =
  46. {
  47.     PyModuleDef_HEAD_INIT,
  48.     "supconcat", /* name of module */
  49.     "usage: supconcat.sconcat()\n", /* module documentation, may be NULL */
  50.     -1,   /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
  51.     supconcat_funcs
  52. };
  53.  
  54. PyMODINIT_FUNC PyInit_supconcat(void)
  55. {
  56.     return PyModule_Create(&supconcat);
  57. }
  58.  
  59. // ------------------------------------------setup.py
  60.  
  61. from distutils.core import setup, Extension
  62. setup(name='supconcat', version='1.0', ext_modules=[Extension('supconcat', ['code\\supconcat.c'])])
  63.  
  64.  
  65. // python setup.py install - ввести в терминале, что бы потом импортировать
  66.  
  67. // ------------------------------------------file_with_str.py
  68.  
  69. import supconcat
  70. // testStr - длинная строка
  71. // numArr - массив с содержащий число вхождений каждой цифры в строку
  72. // Меняется сама строка testStr, и возвращается количество символов в ней
  73. c = supconcat.sconcat(testStr, numArr)
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement