Skip to content

Commit ef1baca

Browse files
committed
repo setup, pushed 8 files
1 parent e807cc4 commit ef1baca

8 files changed

+342
-0
lines changed

BinarySearchTree.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Binary Search Tree in Python
2+
3+
class Node:
4+
def __init__(self, val):
5+
self.value = val
6+
self.leftChild = None
7+
self.rightChild = None
8+
9+
def insert(self, data):
10+
if self.value == data:
11+
return False
12+
13+
elif self.value > data:
14+
if self.leftChild:
15+
return self.leftChild.insert(data)
16+
else:
17+
self.leftChild = Node(data)
18+
return True
19+
20+
else:
21+
if self.rightChild:
22+
return self.rightChild.insert(data)
23+
else:
24+
self.rightChild = Node(data)
25+
return True
26+
27+
def find(self, data):
28+
if(self.value == data):
29+
return True
30+
elif self.value > data:
31+
if self.leftChild:
32+
return self.leftChild.find(data)
33+
else:
34+
return False
35+
else:
36+
if self.rightChild:
37+
return self.rightChild.find(data)
38+
else:
39+
return False
40+
41+
def preorder(self):
42+
if self:
43+
print (str(self.value))
44+
if self.leftChild:
45+
self.leftChild.preorder()
46+
if self.rightChild:
47+
self.rightChild.preorder()
48+
49+
def postorder(self):
50+
if self:
51+
if self.leftChild:
52+
self.leftChild.postorder()
53+
if self.rightChild:
54+
self.rightChild.postorder()
55+
print (str(self.value))
56+
57+
def inorder(self):
58+
if self:
59+
if self.leftChild:
60+
self.leftChild.inorder()
61+
print (str(self.value))
62+
if self.rightChild:
63+
self.rightChild.inorder()
64+
65+
class Tree:
66+
def __init__(self):
67+
self.root = None
68+
69+
def insert(self, data):
70+
if self.root:
71+
return self.root.insert(data)
72+
else:
73+
self.root = Node(data)
74+
return True
75+
76+
def find(self, data):
77+
if self.root:
78+
return self.root.find(data)
79+
else:
80+
return False
81+
82+
def preorder(self):
83+
print("PreOrder")
84+
self.root.preorder()
85+
86+
def postorder(self):
87+
print("PostOrder")
88+
self.root.postorder()
89+
90+
def inorder(self):
91+
print("InOrder")
92+
self.root.inorder()
93+
94+
bst = Tree()
95+
print(bst.insert(10))
96+
bst.insert(5)
97+
bst.insert(15)
98+
bst.insert(11)
99+
bst.preorder()
100+
bst.postorder()
101+
bst.inorder()
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+

BinaryToDecimal.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Python: Binary to Decimal Conversion
2+
# binToDec and decToBin functions are rendered obsolete by the universal convert function
3+
4+
def binToDec(binNum):
5+
decNum = 0
6+
power = 0
7+
while binNum > 0:
8+
decNum += 2 ** power * (binNum % 10)
9+
binNum //= 10
10+
power += 1
11+
return decNum
12+
13+
def decToBin(decNum):
14+
binNum = 0
15+
power = 0
16+
while decNum > 0:
17+
binNum += 10 ** power * (decNum % 2)
18+
decNum //= 2
19+
power += 1
20+
return binNum
21+
22+
def convert(fromNum, fromBase, toBase):
23+
toNum = 0
24+
power = 0
25+
while fromNum > 0:
26+
toNum += fromBase ** power * (fromNum % toBase)
27+
fromNum //= toBase
28+
power += 1
29+
return toNum
30+
31+
# print (str(binToDec(101011)))
32+
# print (str(decToBin(128)))
33+
print (str(convert(127, 10, 8))) # converts 127 in base 10 to base 8
34+
print (str(convert(101001, 2, 2)))

HexToDec.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Python Hexadecimal to Decimal Conversion
2+
3+
def __getDecDigit(digit):
4+
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
5+
'A', 'B', 'C', 'D', 'E', 'F']
6+
for x in range(len(digits)):
7+
if digit == digits[x]:
8+
return x
9+
10+
def hexToDec(hexNum):
11+
decNum = 0
12+
power = 0
13+
for digit in range(len(hexNum), 0, -1):
14+
decNum = decNum + 16 ** power * __getDecDigit(hexNum[digit-1])
15+
power += 1
16+
print(str(decNum))
17+
18+
hexToDec("A5")

LinkedLists.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Node(object):
2+
3+
def __init__ (self, d, n = None):
4+
self.data = d
5+
self.next_node = n
6+
7+
def get_next (self):
8+
return self.next_node
9+
10+
def set_next (self, n):
11+
self.next_node = n
12+
13+
def get_data (self):
14+
return self.data
15+
16+
def set_data (self, d):
17+
self.data = d
18+
19+
20+
class LinkedList (object):
21+
22+
def __init__(self, r = None):
23+
self.root = r
24+
self.size = 0
25+
26+
def get_size (self):
27+
return self.size
28+
29+
def add (self, d):
30+
new_node = Node (d, self.root)
31+
self.root = new_node
32+
self.size += 1
33+
34+
def remove (self, d):
35+
this_node = self.root
36+
prev_node = None
37+
38+
while this_node:
39+
if this_node.get_data() == d:
40+
if prev_node:
41+
prev_node.set_next(this_node.get_next())
42+
else:
43+
self.root = this_node
44+
self.size -= 1
45+
return True # data removed
46+
else:
47+
prev_node = this_node
48+
this_node = this_node.get_next()
49+
return False # data not found
50+
51+
def find (self, d):
52+
this_node = self.root
53+
while this_node:
54+
if this_node.get_data() == d:
55+
return d
56+
else:
57+
this_node = this_node.get_next()
58+
return None
59+
60+
myList = LinkedList()
61+
myList.add(5)
62+
myList.add(8)
63+
myList.add(12)
64+
myList.remove(8)
65+
print(myList.remove(12))
66+
print(myList.find(5))

MaxHeap.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Python MaxHeap
2+
# public functions: push, peek, pop
3+
# private functions: __swap, __floatUp, __bubbleDown
4+
5+
class MaxHeap:
6+
def __init__(self, items=[]):
7+
super().__init__()
8+
self.heap = [0]
9+
for i in items:
10+
self.heap.append(i)
11+
self.__floatUp(len(self.heap) - 1)
12+
13+
def push(self, data):
14+
self.heap.append(data)
15+
self.__floatUp(len(self.heap) - 1)
16+
17+
def peek(self):
18+
if self.heap[1]:
19+
return self.heap[1]
20+
else:
21+
return False
22+
23+
def pop(self):
24+
if len(self.heap) > 2:
25+
self.__swap(1, len(self.heap) - 1)
26+
max = self.heap.pop()
27+
self.__bubbleDown(1)
28+
elif len(self.heap) == 2:
29+
max = self.heap.pop()
30+
else:
31+
max = False
32+
return max
33+
34+
def __swap(self, i, j):
35+
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
36+
37+
def __floatUp(self, index):
38+
parent = index//2
39+
if index <= 1:
40+
return
41+
elif self.heap[index] > self.heap[parent]:
42+
self.__swap(index, parent)
43+
self.__floatUp(parent)
44+
45+
def __bubbleDown(self, index):
46+
left = index * 2
47+
right = index * 2 + 1
48+
largest = index
49+
if len(self.heap) > left and self.heap[largest] < self.heap[left]:
50+
largest = left
51+
if len(self.heap) > right and self.heap[largest] < self.heap[right]:
52+
largest = right
53+
if largest != index:
54+
self.__swap(index, largest)
55+
self.__bubbleDown(largest)
56+
57+
m = MaxHeap([95, 3, 21])
58+
m.push(10)
59+
print(str(m.heap[0:len(m.heap)]))
60+
print(str(m.pop()))

Primes.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# prime number calculator
2+
3+
max = int(input("Find primes up to what number? : "))
4+
primeList = []
5+
6+
for x in range(2, max + 1):
7+
isPrime = True
8+
index = 0
9+
root = int(x ** 0.5) + 1
10+
11+
while index < len(primeList) and primeList[index] <= root:
12+
if x % primeList[index] == 0:
13+
isPrime = False
14+
break
15+
index += 1
16+
17+
if isPrime:
18+
primeList.append(x)
19+
20+
print(primeList)
21+
File renamed without changes.

TempConversion.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Temperature Conversion Program
2+
3+
def menu():
4+
print("\n1. Celsius to Fahrenheit")
5+
print("2. Fahrenheit to Celsius")
6+
print("3. Exit")
7+
choice = int(input("Enter a choice: "))
8+
return choice
9+
10+
def toCelsius(f):
11+
return int((f - 32) / 1.8)
12+
13+
def toFahrenheit(c):
14+
return int(c * 1.8 + 32)
15+
16+
def main():
17+
choice = menu()
18+
while choice != 3:
19+
if choice == 1:
20+
c = eval(input("Enter degrees Celsius: "))
21+
print(str(c) + "C = " + str(toFahrenheit(c)) + "F")
22+
elif choice == 2:
23+
f = eval(input("Enter degrees Fahrenheit: "))
24+
print(str(f) + "F = " + str(toCelsius(f)) + "C")
25+
else:
26+
print("Invalid choice.")
27+
choice = menu()
28+
29+
main()

0 commit comments

Comments
 (0)