Skip to content

Commit

Permalink
Merge pull request matthewsamuel95#572 from shakib609/master
Browse files Browse the repository at this point in the history
Refactor all python files to comply with PEP-8 style guide
  • Loading branch information
matthewsamuel95 authored Oct 15, 2018
2 parents 7c56d2c + a0ea18b commit 0d79652
Show file tree
Hide file tree
Showing 38 changed files with 799 additions and 788 deletions.
4 changes: 2 additions & 2 deletions BFS/python/bfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def BFS(self, node):

while queue:
currentNode = queue.pop(0)
print currentNode
print(currentNode)

children = self.graph[currentNode]
for i in range(len(children)):
Expand All @@ -39,5 +39,5 @@ def BFS(self, node):
graph.addEdge(3, 2)
graph.addEdge(3, 4)

print "Breadth First Search starting from vertex 0:"
print("Breadth First Search starting from vertex 0:")
graph.BFS(0)
94 changes: 45 additions & 49 deletions BST/python/BinarySearchTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,147 +5,143 @@
@author: Daniel
"""

# Binary Search Tree for an array of a single type

# Binary Search Tree for an array of a single type
class BST:

def __init__(self, dataType):
self._root = None
self._dataType = dataType
self._size = 0

def loadArray(self, inputArray):
self.arr = inputArray

def getSize(self):
return self._size

def isEmpty(self):
return self._size == 0

def populateTree(self):
for i in range(len(self.arr)):
self.insert(self.arr[i])

def insert(self, value):
if type(value) == self._dataType:
itemNum = self._size
newNode = Node(value, type(value), itemNum, None, None, None)
if (self._root == None):
if (self._root is None):
self._root = newNode
self._size += 1
return

pointer = self._root
currentVal = pointer.getData()
while True:
if value < currentVal:
if pointer.getLeft() == None:
if pointer.getLeft() is None:
newNode.setParent(pointer)
pointer.setLeft(newNode)
self._size += 1
return
else:
pointer = pointer.getLeft()
currentVal = pointer.getData()

elif value > currentVal:
if pointer.getRight() == None:
if pointer.getRight() is None:
newNode.setParent(pointer)
pointer.setRight(newNode)
self._size += 1
return
else:
pointer = pointer.getRight()
currentVal = pointer.getData()

elif value == currentVal:
print "Value already exists in BST."
print("Value already exists in BST.")
return

def search(self, value):
if type(value) == self._dataType:
if (self._root == None):
if (self._root is None):
return False

pointer = self._root
currentVal = pointer.getData()
while True:
if value < currentVal:
if pointer.getLeft() == None:
if pointer.getLeft() is None:
return False

pointer = pointer.getLeft()
currentVal = pointer.getData()

elif value > currentVal:
if pointer.getRight() == None:
if pointer.getRight() is None:
return False

pointer = pointer.getRight()
currentVal = pointer.getData()

elif value == currentVal:
return pointer.getItemNum()

def inOrderTraversal(self, node):
if node != None:
if node is not None:
self.inOrderTraversal(node.getLeft())
self.sorted.append(node.getData())
self.inOrderTraversal(node.getRight())

def getSorted(self):
self.sorted = list()
self.inOrderTraversal(self._root)
return self.sorted


class Node:

def __init__(self, data, dataType, itemNum, parent, left, right):
self._data = data
self._dataType = dataType # specifies type which node holds
self._itemNum = itemNum # position in parent array
self._parent = parent # link to parent node
self._left = left # link to left child node
self._right = right # link to right child node


self._dataType = dataType # specifies type which node holds
self._itemNum = itemNum # position in parent array
self._parent = parent # link to parent node
self._left = left # link to left child node
self._right = right # link to right child node

def getData(self):
return self._data

def setData(self, newData):
if type(newData) == self._dataType:
self._data = newData
else:
print "Error, wrong data type."
print("Error, wrong data type.")

def getItemNum(self):
return self._itemNum

def getParent(self):
return self._parent

def setParent(self, newParent):
self._parent = newParent

def getLeft(self):
return self._left

def setLeft(self, newLeft):
self._left = newLeft

def getRight(self):
return self._right

def setRight(self, newRight):
self._right = newRight


if __name__ == "__main__":
bst = BST(int)
bst.loadArray((11,20,18,4,5,7,2,0))
bst.loadArray((11, 20, 18, 4, 5, 7, 2, 0))
bst.populateTree()
print bst.search(18)
print bst.getSorted()

print(bst.search(18))
print(bst.getSorted())
176 changes: 93 additions & 83 deletions BackTracking/Hamilton Path/hamilton.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,106 @@
class Graph():
def __init__(self, vertices):
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]
self.V = vertices

class Graph():
def __init__(self, vertices):
self.graph = [[0 for column in range(vertices)]\
for row in range(vertices)]
self.V = vertices

def isSafe(self, v, pos, path):
# Check if current vertex and last vertex
# in path are adjacent
if self.graph[ path[pos-1] ][v] == 0:
def isSafe(self, v, pos, path):
# Check if current vertex and last vertex
# in path are adjacent
if self.graph[path[pos - 1]][v] == 0:
return False
# Check if current vertex not already in path
for vertex in path:
if vertex == v:

# Check if current vertex not already in path
for vertex in path:
if vertex == v:
return False

return True
def hamCycleUtil(self, path, pos):
# base case: if all vertices are
# included in the path
if pos == self.V:
# Last vertex must be adjacent to the
# first vertex in path to make a cyle
if self.graph[ path[pos-1] ][ path[0] ] == 1:

def hamCycleUtil(self, path, pos):

# base case: if all vertices are
# included in the path
if pos == self.V:
# Last vertex must be adjacent to the
# first vertex in path to make a cyle
if self.graph[path[pos - 1]][path[0]] == 1:
return True
else:
else:
return False
for v in range(1,self.V):
if self.isSafe(v, pos, path) == True:
path[pos] = v
if self.hamCycleUtil(path, pos+1) == True:

for v in range(1, self.V):

if self.isSafe(v, pos, path) is True:

path[pos] = v

if self.hamCycleUtil(path, pos + 1) is True:
return True
# Remove current vertex if it doesn't
# lead to a solution

# Remove current vertex if it doesn't
# lead to a solution
path[pos] = -1

return False

def hamCycle(self):
path = [-1] * self.V

''' Let us put vertex 0 as the first vertex
in the path. If there is a Hamiltonian Cycle,
then the path can be started from any point

def hamCycle(self):
path = [-1] * self.V
''' Let us put vertex 0 as the first vertex
in the path. If there is a Hamiltonian Cycle,
then the path can be started from any point
of the cycle as the graph is undirected '''
path[0] = 0
if self.hamCycleUtil(path,1) == False:
print "Solution does not exist\n"

if self.hamCycleUtil(path, 1) is False:
print("Solution does not exist\n")
return False
self.printSolution(path)

self.printSolution(path)
return True

def printSolution(self, path):
print "Solution Exists: Following is one Hamiltonian Cycle"
for vertex in path:
print vertex,
print path[0], "\n"

''' Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3)-------(4) '''
g1 = Graph(5)
g1.graph = [ [0, 1, 0, 1, 0], [1, 0, 1, 1, 1],
[0, 1, 0, 0, 1,],[1, 1, 0, 0, 1],
[0, 1, 1, 1, 0], ]

# Print the solution
g1.hamCycle();

''' Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3) (4) '''
g2 = Graph(5)
g2.graph = [ [0, 1, 0, 1, 0], [1, 0, 1, 1, 1],
[0, 1, 0, 0, 1,], [1, 1, 0, 0, 0],
[0, 1, 1, 0, 0], ]

# Print the solution
g2.hamCycle();

def printSolution(self, path):
print("Solution Exists: Following is one Hamiltonian Cycle")
for vertex in path:
print(vertex)
print(path[0], "\n")


'''
Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3)-------(4)
'''
g1 = Graph(5)
g1.graph = [
[0, 1, 0, 1, 0],
[1, 0, 1, 1, 1],
[0, 1, 0, 0, 1],
[1, 1, 0, 0, 1],
[0, 1, 1, 1, 0],
]

# Print the solution
g1.hamCycle()
'''
Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3) (4)
'''
g2 = Graph(5)
g2.graph = [
[0, 1, 0, 1, 0],
[1, 0, 1, 1, 1],
[0, 1, 0, 0, 1],
[1, 1, 0, 0, 0],
[0, 1, 1, 0, 0],
]

# Print the solution
g2.hamCycle()
Loading

0 comments on commit 0d79652

Please sign in to comment.