Skip to content

Commit 43a6f50

Browse files
authored
Reorganizing files only
1 parent 031df3f commit 43a6f50

File tree

8 files changed

+661
-0
lines changed

8 files changed

+661
-0
lines changed

Numpy/Numpy commands.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
https://docs.scipy.org/doc/numpy-1.12.0/reference/
2+
3+
Numpy
4+
install Numpy using pip
5+
import numpy as np
6+
ndarray - an N-dimensional array, which describes a collection of “items” of the same type
7+
array(list) # constructor
8+
asarray(a[, dtype, order]) # Convert the input to an array
9+
Constants:
10+
ndarray.shape tuple of array dimensions
11+
ndarray.size number of elements in array
12+
ndarray.itemsize size of one element
13+
ndarray.dtype data type of elements
14+
ndarray.flat 1D iterator over elements of array
15+
Common Functions
16+
np.tolist()
17+
np.reshape(a, (3,2))
18+
np.swapaxes(axis1, axis2)
19+
np.copy()
20+
arange()
21+
Statistics Functions:
22+
np.sum(a, axis)
23+
np.prod
24+
np.min
25+
np.max
26+
np.mean
27+
np.std standard deviation
28+
np.var
29+
np.sort(axis)
30+
Other Functions:
31+
String operations
32+
logical operations - AND, OR, XOR, NOT, >, <, =, ...
33+
trig functions
34+
complex numbers (real + imaginary)
35+
polynomials
36+
2D matrix operations
37+
Fourier transforms
38+
====================================================================================
39+
import numpy as np
40+
x = [0,1,2,3,4,5]
41+
a = np.array(x)
42+
43+
index: a[2]
44+
45+
slice: a[start:stop:step]
46+
a[1:4:2]
47+
a[3:]
48+
a[:3]
49+
a.shape
50+
a.size
51+
a.itemsize
52+
a.dtype
53+
54+
b = np.array([[1,2,3], [4,5,6]])
55+
b.swapaxes(0,1)
56+
57+
a = np.arange(0,6)
58+
a = np.arange(0,6).reshape(2,3)
59+
60+

Numpy/Numpy.pptx

463 KB
Binary file not shown.

Numpy/data.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
a, b, c, d, e, f, g, h, i, j
2+
9, 3, 8, 7, 6, 1, 0, 4, 2, 5
3+
1, 7, 4, 9, 2, 6, 8, 3, 5, 0
4+
4, 8, 3, 9, 5, 7, 2, 6, 0, 1
5+
1, 7, 4, 2, 5, 9, 6, 8, 0, 3
6+
0, 7, 5, 2, 8, 6, 3, 4, 1, 9
7+
5, 9, 1, 4, 7, 0, 3, 6, 8, 2

Pandas/Pandas.pptx

440 KB
Binary file not shown.

Trees/2-3_tree.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# 2-3 Tree
2+
# balanced tree data structure with up to 2 data items per node
3+
4+
class Node:
5+
def __init__(self, data, par = None):
6+
#print ("Node __init__: " + str(data))
7+
self.data = list([data])
8+
self.parent = par
9+
self.child = list()
10+
11+
def __str__(self):
12+
if self.parent:
13+
return str(self.parent.data) + ' : ' + str(self.data)
14+
return 'Root : ' + str(self.data)
15+
16+
def __lt__(self, node):
17+
return self.data[0] < node.data[0]
18+
19+
def _isLeaf(self):
20+
return len(self.child) == 0
21+
22+
# merge new_node sub-tree into self node
23+
def _add(self, new_node):
24+
# print ("Node _add: " + str(new_node.data) + ' to ' + str(self.data))
25+
for child in new_node.child:
26+
child.parent = self
27+
self.data.extend(new_node.data)
28+
self.data.sort()
29+
self.child.extend(new_node.child)
30+
if len(self.child) > 1:
31+
self.child.sort()
32+
if len(self.data) > 2:
33+
self._split()
34+
35+
# find correct node to insert new node into tree
36+
def _insert(self, new_node):
37+
# print ('Node _insert: ' + str(new_node.data) + ' into ' + str(self.data))
38+
39+
# leaf node - add data to leaf and rebalance tree
40+
if self._isLeaf():
41+
self._add(new_node)
42+
43+
# not leaf - find correct child to descend, and do recursive insert
44+
elif new_node.data[0] > self.data[-1]:
45+
self.child[-1]._insert(new_node)
46+
else:
47+
for i in range(0, len(self.data)):
48+
if new_node.data[0] < self.data[i]:
49+
self.child[i]._insert(new_node)
50+
break
51+
52+
# 3 items in node, split into new sub-tree and add to parent
53+
def _split(self):
54+
# print("Node _split: " + str(self.data))
55+
left_child = Node(self.data[0], self)
56+
right_child = Node(self.data[2], self)
57+
if self.child:
58+
self.child[0].parent = left_child
59+
self.child[1].parent = left_child
60+
self.child[2].parent = right_child
61+
self.child[3].parent = right_child
62+
left_child.child = [self.child[0], self.child[1]]
63+
right_child.child = [self.child[2], self.child[3]]
64+
65+
self.child = [left_child]
66+
self.child.append(right_child)
67+
self.data = [self.data[1]]
68+
69+
# now have new sub-tree, self. need to add self to its parent node
70+
if self.parent:
71+
if self in self.parent.child:
72+
self.parent.child.remove(self)
73+
self.parent._add(self)
74+
else:
75+
left_child.parent = self
76+
right_child.parent = self
77+
78+
# find an item in the tree; return item, or False if not found
79+
def _find(self, item):
80+
# print ("Find " + str(item))
81+
if item in self.data:
82+
return item
83+
elif self._isLeaf():
84+
return False
85+
elif item > self.data[-1]:
86+
return self.child[-1]._find(item)
87+
else:
88+
for i in range(len(self.data)):
89+
if item < self.data[i]:
90+
return self.child[i]._find(item)
91+
92+
def _remove(self, item):
93+
pass
94+
95+
# print preorder traversal
96+
def _preorder(self):
97+
print (self)
98+
for child in self.child:
99+
child._preorder()
100+
101+
class Tree:
102+
def __init__(self):
103+
print("Tree __init__")
104+
self.root = None
105+
106+
def insert(self, item):
107+
print("Tree insert: " + str(item))
108+
if self.root is None:
109+
self.root = Node(item)
110+
else:
111+
self.root._insert(Node(item))
112+
while self.root.parent:
113+
self.root = self.root.parent
114+
return True
115+
116+
def find(self, item):
117+
return self.root._find(item)
118+
119+
def remove(self, item):
120+
self.root.remove(item)
121+
122+
def printTop2Tiers(self):
123+
print ('----Top 2 Tiers----')
124+
print (str(self.root.data))
125+
for child in self.root.child:
126+
print (str(child.data), end=' ')
127+
print(' ')
128+
129+
def preorder(self):
130+
print ('----Preorder----')
131+
self.root._preorder()
132+
133+
tree = Tree()
134+
135+
lst = [13, 7, 24, 15, 4, 29, 20, 16, 19, 1, 5, 22, 17]
136+
for item in lst:
137+
tree.insert(item)
138+
tree.printTop2Tiers()
139+
140+
# for i in range (25):
141+
# tree.insert(i)
142+
# tree.printTop2Tiers()
143+
# tree.preorder()
144+
# print (tree.find(16))

Trees/BST_Height_Size.pptx

206 KB
Binary file not shown.

0 commit comments

Comments
 (0)