We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents cb713c1 + 5b7c47e commit 6c7d0b8Copy full SHA for 6c7d0b8
dfs_bfs.py
@@ -1,4 +1,4 @@
1
-# Given a graph, there are two methods to
+# Given a graph, there are two methods to
2
# perform traversal on it.
3
# 1. Depth First Search (DFS)
4
# 2. Breadth First Search (BFS)
@@ -17,9 +17,9 @@ def dfs_2(graph, start, visited=None):
17
visited = set()
18
visited.add(start)
19
for next in graph[start] - visited:
20
- dfs(graph, next, visited)
+ dfs_2(graph, next, visited)
21
return visited
22
-
+
23
def bfs(graph, start):
24
visited, queue = set(), [start]
25
while queue:
@@ -30,7 +30,7 @@ def bfs(graph, start):
30
31
32
# bfs(graph, 'A') # {'B', 'C', 'A', 'F', 'D', 'E'}
33
34
def dfs_paths(graph, start, goal):
35
stack = [(start, [start])]
36
while stack:
@@ -47,6 +47,6 @@ def dfs_paths(graph, start, goal):
47
'D': set(['B']),
48
'E': set(['B', 'F']),
49
'F': set(['C', 'E'])}
50
51
result = list(dfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]
52
print(result)
find_m_to_last_llist.py
@@ -3,21 +3,23 @@
# 2->3->4->8->5; m=2 will return 8
# since 8 is second to last
5
6
+from linked_list_data_structure import LinkedList
7
8
def findMToLast(l_list, m):
9
current = l_list.head
10
count = 0
11
12
while current is not None and count < m:
13
count+=1
14
current = current.getNextNode()
15
16
m_behind = l_list.head
while current.next_node is not None:
m_behind = m_behind.getNextNode()
return m_behind
linked_list = LinkedList()
m_to_last = 3
# Returns the third element from last
heap_structure.py
@@ -1,13 +1,13 @@
class Heap(object):
HEAP_SIZE = 10;
def __init__(self):
self.heap = [0]*Heap.HEAP_SIZE
self.currentPosition = -1
def insert(self, item):
# if heap is full , we print a notification
if self.isFull():
print("Heap is full")
@@ -16,7 +16,7 @@ def insert(self, item):
self.currentPosition+=1
self.heap[self.currentPosition] = item
self.fixUp(self.currentPosition)
def fixUp(self, index):
parentIndex = int((index-1)/2)
while parentIndex >=0 and self.heap[parentIndex] < self.heap[index]:
@@ -27,54 +27,54 @@ def fixUp(self, index):
27
# update the index and parentIndex
28
index = parentIndex
29
def fixDown(self, index, upto):
if upto < 0:
upto = self.currentPosition
while index<=upto:
leftChild = 2*index+1
37
rightChild = 2*index+2
38
39
if leftChild <= upto:
40
childToSwap = 0
41
else:
42
if self.heap[leftChild] < self.heap[rightChild]:
43
childToSwap = leftChild
44
45
childToSwap = rightChild
46
if self.heap[index] < self.heap[childToSwap]:
temp = self.heap[index]
self.heap[index] = self.heap[childToSwap]
self.heap[childToSwap] = temp
break
53
54
index = childToSwap
55
56
57
return
58
59
def heapSort(self):
60
for i in range(0, self.currentPosition+1):
61
temp = self.heap[0]
62
print("%d"%temp)
63
self.heap[0] = self.heap[self.currentPosition-i]
64
self.heap[self.currentPosition-i] = temp
65
self.fixDown(0, self.currentPosition-i-1)
66
67
- def getMax():
+ def getMax(self):
68
result = self.heap[0]
69
self.currentPosition-=1
70
self.heap[0] = self.heap[self.currentPosition]
71
del self.heap[self.currentPosition]
72
self.fixDown(0, -1)
73
return result
74
75
def isFull(self):
76
return self.currentPosition == Heap.HEAP_SIZE
77
78
some_heap=Heap()
79
some_heap.insert(12)
80
some_heap.insert(-3)
loop_in_linkedlist.py
@@ -1,3 +1,6 @@
+from find_m_to_last_llist import findMToLast
def hasLoop(l_list):
fast = l_list.head.next_node
slow = l_list.head
@@ -11,9 +14,9 @@ def hasLoop(l_list):
fast = fast.next_node
slow = slow.next_node
return hasLoop
print(findMToLast(linked_list))
preorder_iterative_bst.py
@@ -4,9 +4,11 @@ def preOrderTraversal(root):
while len(stack) > 0:
current = stack.pop()
print(current.value)
right = current.right
- stack.insert(0, right) if right is not None
+ if right is not None:
+ stack.insert(0, right)
left = current.left
- stack.insert(0, left) if left is not None
+ if left is not None:
+ stack.insert(0, left)
remove_duplicates_v2.py
@@ -4,8 +4,8 @@ def remove_duplicates_v2(arr):
for i in arr:
if i not in dedupe_arr:
dedupe_arr.append(i)
return dedupe_arr
-result = remove_duplicates([0,0,0,1,1,2,2,3,4,5])
+result = remove_duplicates_v2([0,0,0,1,1,2,2,3,4,5])
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments