pointers

How to avoid "pointing to same object" while creating nested dictionary using zip and map?

How to avoid "pointing to same object" while creating nested dictionary using zip and map? Question: I would like to avoid pointing to the same object in the below code. Please look at output and desired output stated below. Code: import random prototype = [{‘uniqueDict’: list()}] def mapme(somekey): global aa aa = [random.randint(0, 5), random.randint(6, …

Total answers: 1

Python: Need help solving this problem using two pointers method

Python: Need help solving this problem using two pointers method Question: I’m currently practicing coding problems and found this problem called "Successful Pairs of Spells and Potions", the problem describtion is as follows; You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of …

Total answers: 1

Allocation of Memory with python to pass to dll

Allocation of Memory with python to pass to dll Question: I got a dll which expects a memory pointer to a C Type Byte Array. The dll will read and modify the Array and will also put some extra data at the end of the array. How do I allocate 1MB memory as C Type …

Total answers: 2

How can i cast a double pointer ctype to numpy array?

How can i cast a double pointer ctype to numpy array? Question: Hey I got this code which i’m using to cast numpy 2d array to ctype double pointer. import ctypes as ct import numpy as np arr = np.empty([500, 500], dtype=np.uint8) UI8Ptr = ct.POINTER(ct.c_uint8) UI8PtrPtr = ct.POINTER(UI8Ptr) ct_arr = np.ctypeslib.as_ctypes(arr) UI8PtrArr = UI8Ptr * …

Total answers: 1

When does a pointer to a linked list change the actual list?

When does a pointer to a linked list change the actual list? Question: I have a singly-linked-list, L, and create a pointer to this list P. It seems like sometimes modifying P changes the actual list, while other times modifying P does nothing to the actual list L and only changes what P is pointing …

Total answers: 3

How to pass a 2d array from Python to C?

How to pass a 2d array from Python to C? Question: I’m trying to pass a 2d array from Python to C, using ctypes. The array dtype is uint16. I wrote a simple code just to understand how it works: C: #include <stdint.h> __declspec(dllexport) uint16_t Test(uint16_t **arr) { return (arr[5][5]); } Python: import numpy as …

Total answers: 1

Explanation about dummy nodes and pointers in linked lists

Explanation about dummy nodes and pointers in linked lists Question: I have the following class for a list node: def __init__(self, x): self.val = x self.next = None If I initialize the lists l and r as follows: l = ListNode(1) l.next = ListNode(4) l.next.next = ListNode(5) r = ListNode(1) r.next = ListNode(3) r.next.next = …

Total answers: 1

How to make a list of functions in python?

How to make a list of functions in python? Question: I tried to achieve something like this list1 = [lambda x: x+0, lambda x: x+1] by doing this list2 = [lambda x: x+n for n in range(2)] But it doesn’t work! (Why not?) How do I create a list of functions programmatically? Asked By: Ziofil …

Total answers: 1

Storing unsafe C derivative of temporary Python reference error in Cython

Storing unsafe C derivative of temporary Python reference error in Cython Question: I was writing code to store a (potentially) very large integer value into an array of chars referenced by a pointer. My code looks like this: cdef class Variable: cdef unsigned int Length cdef char * Array def __cinit__(self, var, length): self.Length = …

Total answers: 2

Don't understand Python's csv.reader object

Don't understand Python's csv.reader object Question: I’ve come across a behavior in python’s built-in csv module that I’ve never noticed before. Typically, when I read in a csv, it’s following the doc’s pretty much verbatim, using ‘with’ to open the file then looping over the reader object with a ‘for’ loop. However, I recently tried …

Total answers: 3