Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f77bda8

Browse files
Add files via upload
1 parent 2f6cec3 commit f77bda8

File tree

7 files changed

+325
-0
lines changed

7 files changed

+325
-0
lines changed

‎LinkedList.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Element:
2+
def __init__(self, elem):
3+
self.element = elem
4+
# self.current = None
5+
self.next = None
6+
print(self.element, self.next)
7+
8+
class LinkedList:
9+
def __init__(self, head=None):
10+
self.head = head
11+
# print(self.head)
12+
13+
def append_linked(self, value):
14+
current = self.head
15+
if self.head:
16+
while current.next:
17+
current = current.next
18+
# print(current)
19+
current.next = value
20+
# print(current.next)
21+
else:
22+
self.head = value
23+
def linked_list_view(self):
24+
current = self.head
25+
count = 0
26+
if self.head:
27+
print("if:{}".format(current.element))
28+
while current:
29+
print("while:{}".format(current.element))
30+
current = current.next
31+
32+
33+
def delete_first(self):
34+
if self.head:
35+
self.head = self.head.next
36+
else:
37+
self.head = None
38+
39+
def insert_first(self, new_element):
40+
if self.head:
41+
self.head, new_element.next = new_element, self.head
42+
else:
43+
self.head = new_element
44+
45+
46+
47+

‎LinkedList1.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Element:
2+
"""Individual Linked Lists
3+
4+
declares an element with value and address to next linked list element.
5+
Here, next will be none"""
6+
7+
def __init__(self, value):
8+
self.value = value
9+
self.next = None
10+
11+
class LinkedList:
12+
"""Implementation
13+
14+
Implementing linked list"""
15+
def __init__(self, head = None):
16+
self.head = head
17+
self.linked_list = {}
18+
19+
def append(self, new_element):
20+
current = self.head
21+
if self.head:
22+
print(current.value)
23+
while current.next:
24+
# print(current.value)
25+
current = current.next
26+
print(current.value)
27+
current.next = new_element
28+
else:
29+
self.head = new_element
30+
31+
def view(self):
32+
current = self.head
33+
i = 1
34+
if self.head:
35+
self.linked_list[i] = current.value
36+
print(current.value)
37+
while current.next:
38+
current = current.next
39+
i += 1
40+
self.linked_list[i] = current.value
41+
print(self.linked_list)
42+
43+
else:
44+
print("No head, empty linked list")
45+
46+
def get_position(self, position):
47+
self.view()
48+
linked_list_keys = self.linked_list.keys()
49+
print(linked_list_keys)
50+
if position in linked_list_keys:
51+
print(self.linked_list[position])
52+
else:
53+
print("None")
54+
55+
56+
57+
58+
59+

‎binary_search.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
def binary_search(arr, start, end, num):
2+
if start > end:
3+
return -1
4+
elif start <= end:
5+
mid = (start+end)//2
6+
if num == arr[mid]:
7+
return (num, mid)
8+
elif num < arr[mid]:
9+
end = mid - 1
10+
elif num > arr[mid]:
11+
start = mid + 1
12+
return binary_search(arr, start, end, num)
13+
14+
15+
def binary_search_loop(arr, n, x):
16+
start, end = 0, len(arr)-1
17+
while (start <= end):
18+
mid = (start+end)//2
19+
if x == arr[mid]:
20+
return ((mid, x))
21+
elif x < arr[mid]:
22+
end = mid - 1
23+
elif x > arr[mid]:
24+
start = mid + 1
25+
return -1
26+
27+
result = -1
28+
29+
def first_occur_binary(arr, start, end, num):
30+
"""if start > end:
31+
return "start>end: {} {}".format(-1, result)"""
32+
global result
33+
if start <= end:
34+
mid = (start+end)//2
35+
if num == arr[mid]:
36+
result = mid
37+
end = mid-1
38+
elif num < arr[mid]:
39+
end = mid-1
40+
elif num > arr[mid]:
41+
start = mid+1
42+
print("start:{}, end:{}, mid:{}, arr[mid]:{}, result:{}".format(start, end, mid, arr[mid], result))
43+
first_occur_binary(arr, start, end, num)
44+
return result
45+
result_last = -1
46+
def last_occur_binary(arr, start, end, num):
47+
"""if start > end:
48+
return "start>end: {} {}".format(-1, result)"""
49+
global result_last
50+
if start <= end:
51+
mid = (start+end)//2
52+
if num == arr[mid]:
53+
result_last = mid
54+
start = mid+1
55+
elif num < arr[mid]:
56+
end = mid-1
57+
elif num > arr[mid]:
58+
start = mid+1
59+
print("start:{}, end:{}, mid:{}, arr[mid]:{}, result:{}".format(start, end, mid, arr[mid], result))
60+
last_occur_binary(arr, start, end, num)
61+
return result_last

‎countingSort.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def counting_sort(ar):
2+
min_arr = min(ar)
3+
max_arr = max(ar)
4+
list_, counting, count = [], [], []
5+
for i in range(min_arr, max_arr+1):
6+
list_.append(i)
7+
counting.append(ar.count(i))
8+
for j in range(len(list_)):
9+
if counting[j] != 0:
10+
count.append(list_[j])
11+
print(count)
12+
for k in range(len(ar)):
13+
ar[k] = count[k]

‎mergeSort.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from math import ceil,floor as mukund
2+
def merge(left_array, right_array, a):
3+
len_left_array, len_right_array, len_a = len(left_array), len(right_array), len(a)
4+
i, j, k = 0, 0, 0
5+
while(i<len_left_array and j<len_right_array):
6+
if (left_array[i] <= right_array[j]):
7+
a[k] = left_array[i]
8+
i += 1
9+
else:
10+
a[k] = right_array[j]
11+
j += 1
12+
k += 1
13+
while(i<len_left_array):
14+
a[k] = left_array[i]
15+
i += 1
16+
k += 1
17+
while(j<len_right_array):
18+
a[k] = right_array[j]
19+
j += 1
20+
k += 1
21+
22+
23+
def merge_sort(a):
24+
len_a = len(a)
25+
if len_a < 2:
26+
return a
27+
else:
28+
"""if len_a%2==0:
29+
mid_array = len_a//2
30+
else:
31+
mid_array = len_a//2 + 1"""
32+
mid_array = math.ceil(len_a/2)
33+
left_array = a[:mid_array]
34+
right_array = a[mid_array:]
35+
merge_sort(left_array)
36+
merge_sort(right_array)
37+
merge(left_array, right_array, a)
38+
# print("left_array:{}, right_array:{}".format(left_array, right_array))

‎quick_sort.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def quick_sort(arr, start, end):
2+
if start <= end:
3+
pIndex = partition(arr, start, end)
4+
quick_sort(arr, start, pIndex-1)
5+
quick_sort(arr, pIndex+1, end)
6+
7+
8+
9+
def partition(arr, start, end):
10+
pivot = arr[end]
11+
pIndex = start
12+
for i in range(start, end):
13+
if arr[i] <= pivot:
14+
arr[i], arr[pIndex] = arr[pIndex], arr[i]
15+
pIndex += 1
16+
17+
arr[pIndex], arr[end] = arr[end], arr[pIndex]
18+
return pIndex

‎stackList.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import numbers
2+
class Stack:
3+
"""Stack-Data Structure
4+
5+
Last-In-First-Out Abstract Data Structure, built on the basis of different
6+
data structures as List, Arrays, LinkedList etc.
7+
Four Operations:
8+
1. Push() - adds/appends an element to the stack.
9+
2. Pop() - removes and displays the recently added element from the stack.
10+
3. Peek()/Top() - returns the top element of the stack.
11+
4. isEmpty() - checks if the stack is empty. """
12+
13+
def __init__(self):
14+
self.stack = []
15+
16+
def isEmpty(self):
17+
if len(self.stack) == 0:
18+
return True
19+
else:
20+
return False
21+
22+
23+
def push(self, element):
24+
self.stack.append(element)
25+
print(self.stack)
26+
27+
def pop(self):
28+
if self.isEmpty():
29+
return "Stack is empty"
30+
else:
31+
print(self.stack[len(self.stack)-1:])
32+
del self.stack[len(self.stack)-1:]
33+
34+
def peek(self):
35+
if self.isEmpty():
36+
return "Stack is empty"
37+
else:
38+
print(self.stack[len(self.stack)-1:])
39+
40+
def balanced_parentheses(self, parentheses):
41+
self.parentheses = parentheses
42+
count = 0
43+
for i in parentheses:
44+
if i == '(' or i == '{' or i == '[':
45+
self.stack.append(i)
46+
elif i == ')':
47+
if self.stack[len(self.stack)-1] == '(':
48+
self.pop()
49+
count += 1
50+
elif i == '}':
51+
if self.stack[len(self.stack)-1] == '{':
52+
self.pop()
53+
count += 1
54+
elif i == ']':
55+
if self.stack[len(self.stack)-1] == '[':
56+
self.pop()
57+
count += 1
58+
else:
59+
continue
60+
#return "Only Parentheses not found, Invalid Input"
61+
print("Count of parentheses: {}".format(count))
62+
63+
def postfix_evaluation(self, exp):
64+
self.exp = exp.split(' ')
65+
for i in self.exp:
66+
try:
67+
if isinstance(int(i), numbers.Number):
68+
self.stack.append(int(i))
69+
print("self.stack: {}".format(self.stack))
70+
except ValueError:
71+
if i == '+':
72+
self.stack[len(self.stack)-2] = self.stack[len(self.stack)-2] + self.stack[len(self.stack)-1]
73+
self.pop()
74+
elif i == '-':
75+
self.stack[len(self.stack)-2] = self.stack[len(self.stack)-2] - self.stack[len(self.stack)-1]
76+
self.pop()
77+
elif i == '*':
78+
self.stack[len(self.stack)-2] = self.stack[len(self.stack)-2] * self.stack[len(self.stack)-1]
79+
self.pop()
80+
elif i == "/":
81+
self.stack[len(self.stack)-2] = self.stack[len(self.stack)-2] / self.stack[len(self.stack)-1]
82+
self.pop()
83+
print(self.stack)
84+
85+
"""def infix_to_postfix(self, exp):
86+
self.exp = exp.split(' ')"""
87+
88+
89+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /