У меня есть вопрос и ответ на собеседование.Пожалуйста, помогите ..
Я не понимаю, зачем ей нужна структура c-data.
Я не понимаю, зачем нужна сборка мусора и зачем она нужна.
IНе понимаю, зачем нужен def _get_obj (id)?
import ctypes
# This is hacky. It's a data structure for C, not python.
class Node(object):
def __init__(self, val):
self.val = val
self.both = 0
class XorLinkedList(object):
def __init__(self):
self.head = self.tail = None
self.__nodes = [] # This is to prevent garbage collection
def add(self, node):
if self.head is None:
self.head = self.tail = node
else:
self.tail.both = id(node) ^ self.tail.both
node.both = id(self.tail)
self.tail = node
# Without this line, Python thinks there is no way to reach nodes between
# head and tail.
self.__nodes.append(node)
def get(self, index):
prev_id = 0
node = self.head
for i in range(index):
next_id = prev_id ^ node.both
if next_id:
prev_id = id(node)
node = _get_obj(next_id)
else:
raise IndexError('Linked list index out of range')
return node
def _get_obj(id):
return ctypes.cast(id, ctypes.py_object).value