Skip to content

Commit 6956137

Browse files
Data structures examples in Python
1 parent d187a19 commit 6956137

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+4833
-0
lines changed

Algorithms/DFS.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def DFS(head):
2+
curr = head
3+
count = 0;
4+
while (curr != None & curr.visited == False):
5+
count += 1
6+
if (curr.lChild != None & curr.lChild.visited == False):
7+
curr= curr.lChild
8+
elif (curr.rChild != None & curr.rChild.visited == False):
9+
curr= curr.rChild;
10+
else:
11+
print (curr.value)
12+
curr.visited = 1;
13+
curr = head;
14+
15+
print("count is : ", count)

Algorithms/Fibo.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Prime(object):
2+
@classmethod
3+
def main(cls, args):
4+
print(cls.isPrime(50))
5+
print(cls.isPrime(47))
6+
7+
@classmethod
8+
def isPrime(self, n):
9+
if( n > 1):
10+
answer = True
11+
else:
12+
answer = False
13+
i = 2
14+
while(i*i <= n):
15+
if(n%i == 0):
16+
answer = False
17+
break
18+
i += 1
19+
return answer
20+
21+
if __name__ == '__main__':
22+
import sys
23+
Prime.main(sys.argv)

Algorithms/NQueen.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
class NQueen(object):
4+
@classmethod
5+
def printArr(cls, Q, n):
6+
i = 0
7+
while i < n:
8+
print(Q[i])
9+
i += 1
10+
print("")
11+
12+
@classmethod
13+
def Feasible(cls, Q, k):
14+
i = 0
15+
while i < k:
16+
if Q[k] == Q[i] or abs(Q[i] - Q[k]) == abs(i - k):
17+
return False
18+
i += 1
19+
return True
20+
21+
@classmethod
22+
def NQueens(cls, Q, k, n):
23+
if k == n:
24+
cls.printArr(Q, n)
25+
return
26+
i = 0
27+
while i < n:
28+
Q[k] = i
29+
if cls.Feasible(Q, k):
30+
cls.NQueens(Q, k + 1, n)
31+
i += 1
32+
33+
34+
@classmethod
35+
def main(cls, args):
36+
Q = [0] * 8
37+
cls.NQueens(Q, 0, 8)
38+
39+
if __name__ == '__main__':
40+
import sys
41+
NQueen.main(sys.argv)

Algorithms/Prime.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Fibo(object):
2+
3+
@classmethod
4+
def main(cls, args):
5+
print(cls.fibonacci(50))
6+
print(cls.fibonacci2(50))
7+
8+
@classmethod
9+
def fibonacci2(cls, n):
10+
if n <= 1:
11+
return n
12+
return cls.fibonacci(n - 1) + cls.fibonacci(n - 2)
13+
14+
@classmethod
15+
def fibonacci(cls, n):
16+
first = 0
17+
second = 1
18+
if (n == 0):
19+
return first;
20+
elif (n == 1):
21+
return second;
22+
i = 2
23+
while(i <= n):
24+
temp = first + second
25+
first = second
26+
second = temp
27+
i += 1
28+
return temp
29+
30+
def isPrime(self, n):
31+
if( n > 1):
32+
answer = True
33+
else:
34+
answer = False
35+
i = 2
36+
while(i*i <= n):
37+
if(n%i == 0):
38+
answer = True;
39+
break;
40+
i += 1
41+
return answer
42+
43+
if __name__ == '__main__':
44+
import sys
45+
Fibo.main(sys.argv)

Algorithms/TOH.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
""" generated source for module TOH """
3+
4+
class TOH(object):
5+
""" generated source for class TOH """
6+
7+
@classmethod
8+
def main(cls, args):
9+
""" generated source for method main """
10+
11+
cls.TowersOfHanoi(3)
12+
13+
@classmethod
14+
def TOHUtil(cls, num, source, dest, temp):
15+
""" generated source for method TOHUtil """
16+
if num < 1:
17+
return
18+
cls.TOHUtil(num - 1, source, temp, dest)
19+
print("Move disk" , num , "from peg" , source , "to peg" , dest)
20+
cls.TOHUtil(num - 1, temp, dest, source)
21+
22+
@classmethod
23+
def TowersOfHanoi(cls, num):
24+
""" generated source for method TowersOfHanoi """
25+
print("The sequence of moves involved in the Tower of Hanoi are :")
26+
cls.TOHUtil(num, 'A', 'C', 'B')
27+
28+
29+
if __name__ == '__main__':
30+
import sys
31+
TOH.main(sys.argv)

Collections/Counter.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections import Counter
2+
3+
class MyClass:
4+
5+
@classmethod
6+
def main(cls, args):
7+
""" generated source for method main """
8+
seq = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
9+
mycounter = Counter(seq)
10+
print mycounter
11+
print mycounter.get(3)
12+
print mycounter.has_key(3)
13+
mycounter.pop(4)
14+
print mycounter
15+
print 4 in mycounter
16+
mycounter[5] += 1
17+
mycounter[5] += 1
18+
print mycounter
19+
20+
21+
if __name__ == '__main__':
22+
import sys
23+
MyClass.main(sys.argv)

Collections/Dictionary.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from collections import OrderedDict
2+
3+
class MyClass:
4+
@classmethod
5+
def main(cls, args):
6+
""" generated source for method main """
7+
a = {}
8+
a["apple"] = 40
9+
a["banana"] = 10
10+
a["mango"] = 20
11+
print a
12+
print a["mango"]
13+
print a.get("mango")
14+
print "apple" in a
15+
16+
b = OrderedDict()
17+
b["apple"] = 40
18+
b["banana"] = 10
19+
b["mango"] = 20
20+
print b
21+
print b["mango"]
22+
print b.get("mango")
23+
print "banana" in b
24+
25+
26+
if __name__ == '__main__':
27+
import sys
28+
MyClass.main(sys.argv)

Collections/Heap.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import heapq
2+
3+
class MyClass:
4+
@classmethod
5+
def main(cls, args):
6+
""" generated source for method main """
7+
myheap = [19,1,18] # creates heap from list.
8+
heapq.heapify(myheap)
9+
print myheap
10+
heapq.heappush(myheap, 5) # pushes a new item on the heap
11+
heapq.heappush(myheap, 3)
12+
heapq.heappush(myheap, 8)
13+
heapq.heappush(myheap, 2)
14+
print myheap
15+
print heapq.heappop(myheap) # pops the smallest item from the heap
16+
print myheap
17+
print myheap[0] # peek the smallest element of the heap.
18+
print len(myheap)
19+
20+
if __name__ == '__main__':
21+
import sys
22+
MyClass.main(sys.argv)

Collections/List.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class MyClass:
2+
@classmethod
3+
def main(cls, args):
4+
""" generated source for method main """
5+
a = []
6+
a.append(5)
7+
a.append(4)
8+
a.append(2)
9+
a.append(40)
10+
print a
11+
print a + a
12+
print len(a)
13+
a.remove(4)
14+
print a
15+
print a.pop()
16+
a.sort()
17+
print a
18+
a.reverse()
19+
print a
20+
21+
22+
if __name__ == '__main__':
23+
import sys
24+
MyClass.main(sys.argv)

Collections/Queue.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import deque
2+
3+
class MyClass:
4+
@classmethod
5+
def main(cls, args):
6+
""" generated source for method main """
7+
a = deque([])
8+
a.append(5)
9+
a.append(4)
10+
a.append(3)
11+
a.append(2)
12+
a.append(1)
13+
a.append(0)
14+
print a
15+
print a.popleft()
16+
print a
17+
18+
if __name__ == '__main__':
19+
import sys
20+
MyClass.main(sys.argv)

Collections/Set.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class MyClass:
2+
@classmethod
3+
def main(cls, args):
4+
""" generated source for method main """
5+
a = set()
6+
a.add(5)
7+
a.add(4)
8+
a.add(2)
9+
a.add(1)
10+
a.add(5)
11+
print a
12+
print (4 in a)
13+
a.remove(4)
14+
print a
15+
16+
if __name__ == '__main__':
17+
import sys
18+
MyClass.main(sys.argv)

Collections/Stack.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from collections import Set
2+
3+
class MyClass:
4+
5+
@classmethod
6+
def main(cls, args):
7+
""" generated source for method main """
8+
a = set()
9+
a.add(5)
10+
a.add(5)
11+
a.add(4)
12+
a.add(2)
13+
a.add(5)
14+
print a
15+
print len(a)
16+
a.remove(4)
17+
print a
18+
a.add(8)
19+
print a.pop()
20+
print a
21+
print (5 in a)
22+
23+
if __name__ == '__main__':
24+
import sys
25+
MyClass.main(sys.argv)

0 commit comments

Comments
 (0)