-
Notifications
You must be signed in to change notification settings - Fork 892
/
problem_006.py
55 lines (44 loc) · 1.15 KB
/
problem_006.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Node:
def __init__(self, data):
self.data = data
self.both = id(data)
def __repr__(self):
return str(self.data)
a = Node("a")
b = Node("b")
c = Node("c")
d = Node("d")
e = Node("e")
# id_map simulates object pointer values
id_map = dict()
id_map[id("a")] = a
id_map[id("b")] = b
id_map[id("c")] = c
id_map[id("d")] = d
id_map[id("e")] = e
class LinkedList:
def __init__(self, node):
self.head = node
self.tail = node
self.head.both = 0
self.tail.both = 0
def add(self, element):
self.tail.both ^= id(element.data)
element.both = id(self.tail.data)
self.tail = element
def get(self, index):
prev_node_address = 0
result_node = self.head
for i in range(index):
next_node_address = prev_node_address ^ result_node.both
prev_node_address = id(result_node.data)
result_node = id_map[next_node_address]
return result_node.data
llist = LinkedList(c)
llist.add(d)
llist.add(e)
llist.add(a)
assert llist.get(0) == "c"
assert llist.get(1) == "d"
assert llist.get(2) == "e"
assert llist.get(3) == "a"