Skip to content

Commit f9e8468

Browse files
committed
DSA
0 parents  commit f9e8468

30 files changed

+958
-0
lines changed

Class_fraction.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from Fraction import Fraction,gcd
2+
#fra.show()
3+
f1 = Fraction(3,2)
4+
f2 = Fraction(5,2)
5+
print("%s + %s = "%(f1,f2)),
6+
print(f1+f2)
7+
print(f1-f2)
8+
print(f1*f2)
9+
print(f1/f2)
10+
print(f1==f2) #checks only for shallow equality i.e.
11+
#whether both var reference to same object
12+
f3 = f1 #assigning reference of f1 to f3, i.e. both points to same object
13+
print(f1==f3) #hence shallow equality is true
14+
print(f1==f2)
15+
16+
print(f1<=f2)
17+
print(f1>=f2)
18+
19+
print f1.get_num()
20+
print f1.get_den()
21+
22+

Deque.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Deque:
2+
def __init__(self):
3+
self.items = []
4+
5+
def is_empty(self):
6+
return self.items == []
7+
8+
def add_front(self, item):
9+
self.items.append(item)
10+
11+
def add_rear(self, item):
12+
self.items.insert(0, item)
13+
14+
def remove_front(self):
15+
return self.items.pop()
16+
17+
def remove_rear(self):
18+
return self.items.pop(0)
19+
20+
def size(self):
21+
return len(self.items)
22+
23+
def __str__(self):
24+
return (str(self.items))

Deque.pyc

1.61 KB
Binary file not shown.

Deque1.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from Deque import Deque
2+
3+
dq = Deque()
4+
5+
print(dq.is_empty())
6+
print(dq.add_rear(4))
7+
print(dq.add_rear('dog'))
8+
print(dq.add_front('cat'))
9+
print(dq.add_front(True))
10+
print(dq.size())
11+
print(dq.is_empty())
12+
print(dq.add_rear(8.4))
13+
print(dq.remove_rear())
14+
print(dq.remove_front())

Deque_palindrome.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from Deque import Deque
2+
3+
def pal_checker(test_string):
4+
tst_str = Deque()
5+
6+
for ch in test_string:
7+
tst_str.add_front(ch)
8+
9+
pal_flag = True
10+
11+
while tst_str.size() > 1 and pal_flag:
12+
first_c = tst_str.remove_front()
13+
last_c = tst_str.remove_rear()
14+
15+
if first_c != last_c:
16+
pal_flag = False
17+
18+
return pal_flag
19+
20+
print(pal_checker('qasdfghfdsa'))
21+
print(pal_checker('asdfggfdsa'))
22+
print(pal_checker('asdfgfdsa'))
23+
print(pal_checker('1234321'))

Fraction.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class Fraction:
2+
"""docstring fos Fraction"""
3+
def __init__(self, up,down):
4+
x = gcd(up,down)
5+
self.num = up/x
6+
self.den = down/x
7+
8+
def show(self):
9+
#print('%d/%d'%(self.num,self.den))
10+
print self.num,"/",self.den
11+
12+
def __str__(self):
13+
return str(self.num)+"/"+str(self.den)
14+
15+
def __add__(self,other):
16+
sum_num = self.num*other.den + self.den*other.num
17+
sum_den = self.den*other.den
18+
temp = gcd(sum_num,sum_den)
19+
return str(sum_num/temp)+'/'+str(sum_den/temp)
20+
21+
def __sub__(self,other):
22+
sum_num = self.num*other.den - self.den*other.num
23+
sum_den = self.den*other.den
24+
temp = gcd(sum_num,sum_den)
25+
return str(sum_num/temp)+'/'+str(sum_den/temp)
26+
27+
def __mul__(self,other):
28+
sum_num = self.num*other.num
29+
sum_den = self.den*other.den
30+
temp = gcd(sum_num,sum_den)
31+
return str(sum_num/temp)+'/'+str(sum_den/temp)
32+
33+
def __div__(self,other):
34+
35+
sum_num = self.num*other.den
36+
sum_den = self.den*other.num
37+
temp = gcd(sum_num,sum_den)
38+
return str(sum_num/temp)+'/'+str(sum_den/temp)
39+
40+
def __eq__(self,other):
41+
#to check deep equality I have overriden the __eq__ fn so that
42+
#instead of checking reference it compares values and
43+
#returns True if both matches
44+
first_num = self.num*other.den
45+
second_num = self.den*other.num
46+
47+
return first_num == second_num
48+
49+
def __lt__(self,other):
50+
51+
first_num = self.num*other.den
52+
second_num = self.den*other.num
53+
54+
return first_num < second_num
55+
56+
def __le__(self,other):
57+
58+
first_num = self.num*other.den
59+
second_num = self.den*other.num
60+
61+
return first_num <= second_num
62+
63+
def __gt__(self,other):
64+
65+
first_num = self.num*other.den
66+
second_num = self.den*other.num
67+
68+
return first_num > second_num
69+
70+
def __ge__(self,other):
71+
72+
first_num = self.num*other.den
73+
second_num = self.den*other.num
74+
75+
return first_num >= second_num
76+
77+
def __ne__(self,other):
78+
79+
first_num = self.num*other.den
80+
second_num = self.den*other.num
81+
82+
return first_num != second_num
83+
84+
85+
def get_num(self):
86+
return(self.num)
87+
88+
def get_den(self):
89+
return(self.den)
90+
91+
def gcd(m,n):
92+
while (m % n != 0):
93+
temp = m % n
94+
m = n
95+
n = temp
96+
return n
97+

Fraction.pyc

3.79 KB
Binary file not shown.

ListSearch.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
numbers = [2,3,49999,5,6,6,7,223,45,75,8,5,97,3200,5222,37,8,23,3,5,66,4,2,4,6,74,846,85,6,56,3,2,43,83,58,368,57,386,4,3,7,64]
2+
3+
def functionQuadSearch(numbers):
4+
greater = 0;
5+
6+
for i in numbers:
7+
temp = i
8+
for j in numbers:
9+
10+
if j>i:
11+
if j>greater:
12+
greater = j
13+
14+
return greater
15+
16+
def functionLinearSearch(numbers):
17+
greater = 0
18+
for i in numbers:
19+
if i < greater:
20+
greater = i
21+
return greater
22+
23+
24+
print(functionQuadSearch(numbers))
25+
print(functionLinearSearch(numbers))

List_1.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
class Node:
2+
def __init__(self,init_data):
3+
self.data = init_data
4+
self.next = None
5+
6+
def get_data(self):
7+
return self.data
8+
9+
def get_next(self):
10+
return self.next
11+
12+
def set_data(self, new_data):
13+
self.data = new_data
14+
15+
def set_next(self, new_next):
16+
self.next = new_next
17+
18+
class UnorderedList:
19+
def __init__(self):
20+
self.head = None
21+
22+
def is_empty(self):
23+
return self.head == None
24+
25+
def add(self, item):
26+
temp = Node(item)
27+
temp.next = self.head
28+
self.head = temp
29+
30+
def size(self):
31+
count = 0
32+
current = self.head
33+
while current != None:
34+
count += 1
35+
current = current.get_next()
36+
37+
return count
38+
39+
def search(self, item):
40+
loc = None
41+
count = 0
42+
found = False
43+
current = self.head
44+
45+
while current != None and not found:
46+
if item == current.get_data():
47+
found = True
48+
loc = count
49+
current = current.get_next()
50+
count += 1
51+
52+
if found:
53+
return str("item found at position %r"%(loc))
54+
else:
55+
return "item not found"
56+
57+
def remove(self, item):
58+
prev = None
59+
found = False
60+
current = self.head
61+
62+
while current != None and not found:
63+
if item == current.get_data():
64+
found = True
65+
else:
66+
prev = current
67+
current = current.get_next()
68+
69+
if prev == None:
70+
self.head = current.get_next()
71+
else:
72+
prev.set_next(current.get_next())
73+
74+
def append(self, item):
75+
prev = None
76+
found = False
77+
current = self.head
78+
79+
while current != None and not found:
80+
81+
prev = current
82+
current = current.get_next()
83+
84+
temp = Node(item)
85+
prev.set_next(temp)
86+
87+
88+
list1 = UnorderedList()
89+
90+
print list1.is_empty()
91+
list1.add(31)
92+
list1.add(77)
93+
list1.add(17)
94+
print list1.size()
95+
list1.add(93)
96+
list1.add(54)
97+
print list1.size()
98+
print list1.remove(31)
99+
print list1.size()
100+
print list1.remove(54)
101+
print list1.size()
102+
list1.append(73)
103+
print list1.size()
104+
list1.append(799)
105+
print list1.size()

Queue.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Queue:
2+
def __init__(self):
3+
self.items = []
4+
5+
def is_empty(self):
6+
return self.items == []
7+
8+
def enqueue(self, item):
9+
self.items.insert(0, item)
10+
11+
def dequeue(self):
12+
return self.items.pop()
13+
14+
def size(self):
15+
return len(self.items)

0 commit comments

Comments
 (0)