├── LAB 5 ├── Task 1 d.py ├── Task 1 a.py ├── Task 2 a.py ├── Task 1 b.py ├── Task 1 c.py ├── Task 3.py ├── Task 4 a.py ├── Task 4 b.py ├── Task 5.py ├── Task 2 b.py └── Task 2 c.py ├── LAB 2 ├── task 1.py ├── Task 2.py └── task 3.py ├── LAB 6 ├── Task 7.py ├── Task 2.py ├── Task 1.py ├── Task 6.py ├── Task 5.py ├── Task 3.py └── Task 4.py ├── LAB 1 ├── task 2.py ├── task 6.py ├── task 1.py ├── task 7.py ├── task 3.py ├── task 8.py ├── task 4.py ├── task 9.py ├── task 5.py ├── task 11.py └── task 10.py ├── LAB 8 ├── Task 4.py ├── Task 5.py ├── Task 3.py ├── task 7.py ├── Task 2.py ├── Task 1.py └── Task 6.py ├── LAB 7 ├── Task 1.py └── Task 2.py ├── LAB 4 ├── Task 1.py └── Task 2.py ├── LAB 3 └── task1+2.py └── README.md /LAB 5/Task 1 d.py: -------------------------------------------------------------------------------- 1 | # d 2 | def powerN(base, n): 3 | if n == 0: 4 | return 1 5 | else: 6 | return base*powerN(base, n-1) 7 | 8 | 9 | power = powerN(3, 3) 10 | print(power) 11 | -------------------------------------------------------------------------------- /LAB 5/Task 1 a.py: -------------------------------------------------------------------------------- 1 | # Task 1 [Very Easy] 2 | # a 3 | 4 | def factorial(n): 5 | if n == 0 | n == 1: 6 | return 1 7 | else: 8 | return n * factorial(n-1) 9 | 10 | 11 | fact = factorial(6) 12 | print(fact) 13 | -------------------------------------------------------------------------------- /LAB 5/Task 2 a.py: -------------------------------------------------------------------------------- 1 | # task 2 [Easy] 2 | # a 3 | def deci_to_binary(n): 4 | if n == 0: 5 | return 0 6 | else: 7 | return deci_to_binary(n//2)*10 + (n % 2) 8 | 9 | 10 | decimal = deci_to_binary(11) 11 | print(decimal) 12 | -------------------------------------------------------------------------------- /LAB 5/Task 1 b.py: -------------------------------------------------------------------------------- 1 | # b 2 | 3 | def fibonacci(n): 4 | if n == 0: 5 | return 0 6 | elif n == 1: 7 | return 1 8 | else: 9 | return fibonacci(n-1) + fibonacci(n-2) 10 | 11 | 12 | fib = fibonacci(4) 13 | print(fib) 14 | -------------------------------------------------------------------------------- /LAB 5/Task 1 c.py: -------------------------------------------------------------------------------- 1 | # c 2 | def print_array(list, index): 3 | if index == len(list): 4 | return 5 | else: 6 | print(list[index]) 7 | print_array(list, index+1) 8 | 9 | 10 | list = [1, 2, 3, 4, 5, 7] 11 | print_array(list, 0) 12 | -------------------------------------------------------------------------------- /LAB 2/task 1.py: -------------------------------------------------------------------------------- 1 | # Task 1: 2 | 3 | class Node: 4 | def __init__(self, integer, next): 5 | self.integer = integer 6 | self.next = next 7 | 8 | 9 | class MyList: 10 | def __init__(self): 11 | self.head = None 12 | 13 | 14 | mylinkedlist = MyList() 15 | -------------------------------------------------------------------------------- /LAB 6/Task 7.py: -------------------------------------------------------------------------------- 1 | # task 7 2 | arr = [-1]*99 3 | 4 | 5 | def fibonacci(n): 6 | if n == 0 or n == 1: 7 | return n 8 | else: 9 | if arr[n] == -1: 10 | arr[n] = fibonacci(n-1) + fibonacci(n-2) 11 | return arr[n] 12 | 13 | 14 | fi = fibonacci(12) 15 | print(fi) 16 | -------------------------------------------------------------------------------- /LAB 5/Task 3.py: -------------------------------------------------------------------------------- 1 | # task 3 [Medium] 2 | def hocBuilder(height): 3 | if height == 0: 4 | return str(0) + "\nnot build a house at all." 5 | elif height == 1: 6 | return 8 7 | else: 8 | return 5+hocBuilder(height-1) 9 | 10 | 11 | height = int(input("Enter height: ")) 12 | print(hocBuilder(height)) 13 | -------------------------------------------------------------------------------- /LAB 1/task 2.py: -------------------------------------------------------------------------------- 1 | def rotateLeft(source, k): 2 | list_lenght = len(source) 3 | for values in range(k): 4 | new_value = source[0] 5 | for value in range(1, list_lenght): 6 | source[value - 1] = source[value] 7 | source[list_lenght - 1] = new_value 8 | 9 | 10 | source = [10, 20, 30, 40, 50, 60] 11 | rotateLeft(source, 3) 12 | print(source) 13 | -------------------------------------------------------------------------------- /LAB 1/task 6.py: -------------------------------------------------------------------------------- 1 | def array_series(n): 2 | n_squared = n*n 3 | new_array = [0]*n_squared 4 | 5 | x = 1 6 | while x <= n: 7 | y = 1 8 | while y <= x: 9 | new_array[(x*n)-y] = y 10 | 11 | y += 1 12 | x += 1 13 | return new_array 14 | 15 | 16 | n = int(input("Please input a number: n= ")) 17 | print(array_series(n)) 18 | -------------------------------------------------------------------------------- /LAB 5/Task 4 a.py: -------------------------------------------------------------------------------- 1 | # task 4 [Hard] 2 | # a 3 | 4 | def pattern(n): 5 | if n == 0: 6 | return 0 7 | else: 8 | pattern(n-1) 9 | print(n, end="") 10 | 11 | 12 | def print_pattern(n): 13 | if n == 0: 14 | return 0 15 | else: 16 | print_pattern(n-1) 17 | pattern(n) 18 | print() 19 | 20 | 21 | n = int(input("Enter n: ")) 22 | 23 | print_pattern(n) 24 | -------------------------------------------------------------------------------- /LAB 1/task 1.py: -------------------------------------------------------------------------------- 1 | def shiftLeft(source, k): 2 | index = len(source) # total index of array 3 | for v in range(index): 4 | x = v - k 5 | if x >= 0: 6 | source[x] = source[v] 7 | 8 | remain_index = len(source) - k 9 | for v in range(remain_index, index): 10 | source[v] = 0 11 | 12 | print(source) 13 | 14 | 15 | source = [10, 20, 30, 40, 50, 60] 16 | 17 | shiftLeft(source, 3) 18 | -------------------------------------------------------------------------------- /LAB 1/task 7.py: -------------------------------------------------------------------------------- 1 | # 7 Max Bunch Count 2 | 3 | def Max_Bunch_Count(new_array): 4 | sum_list = [] 5 | first = new_array[0] 6 | c = 0 7 | for values in new_array: 8 | if values == first: 9 | c += 1 10 | 11 | else: 12 | first = values 13 | c = 1 14 | sum_list.append(c) 15 | print(max(sum_list)) 16 | 17 | 18 | new_array = [1, 2, 2, 3, 4, 4, 4] 19 | Max_Bunch_Count(new_array) 20 | -------------------------------------------------------------------------------- /LAB 1/task 3.py: -------------------------------------------------------------------------------- 1 | def remove(source, size, idx): 2 | source_index = len(source) 3 | new_list = source_index * [0] 4 | x = 0 5 | for values in range(size): 6 | if values == idx: 7 | continue 8 | else: 9 | new_list[x] = source[values] 10 | x += 1 11 | 12 | source = new_list 13 | 14 | print(source) 15 | 16 | 17 | source = [10, 20, 30, 40, 50, 0, 0] 18 | 19 | remove(source, 5, 2) 20 | -------------------------------------------------------------------------------- /LAB 6/Task 2.py: -------------------------------------------------------------------------------- 1 | # task 2 2 | 3 | def rec_insertion_sort(array, index): 4 | if index <= 1: 5 | return -1 6 | rec_insertion_sort(array, index-1) 7 | x = index - 2 8 | y = array[index - 1] 9 | while (array[x] > y and x >= 0): 10 | array[x+1] = array[x] 11 | x -= 1 12 | array[x+1] = y 13 | 14 | 15 | array = [6, 7, 8, 9, 4, 3, 2] 16 | index = len(array) 17 | rec_insertion_sort(array, index) 18 | print(array) 19 | -------------------------------------------------------------------------------- /LAB 1/task 8.py: -------------------------------------------------------------------------------- 1 | # 8 Repetition 2 | 3 | def repetition(new_array): 4 | new_list = [] 5 | dictionary = {x: new_array.count(x) for x in new_array} 6 | for keys, values in dictionary.items(): 7 | if values > 1: 8 | new_list.append(values) 9 | if len(new_list) == len(set(new_list)): 10 | print("False") 11 | else: 12 | print("True") 13 | 14 | 15 | new_array = [4, 5, 6, 6, 4, 3, 6, 4] 16 | repetition(new_array) 17 | -------------------------------------------------------------------------------- /LAB 1/task 4.py: -------------------------------------------------------------------------------- 1 | def removeAll(source, size, elements): 2 | source_index = len(source) 3 | new_list = [0] * source_index 4 | x = 0 5 | for values in source: 6 | if values == elements: 7 | continue 8 | else: 9 | new_list[x] = values 10 | x += 1 11 | if x >= size: 12 | break 13 | source = new_list 14 | print(source) 15 | 16 | 17 | source = [10, 2, 30, 2, 50, 2, 2, 0, 0] 18 | removeAll(source, 7, 2) 19 | -------------------------------------------------------------------------------- /LAB 6/Task 1.py: -------------------------------------------------------------------------------- 1 | # task 1 2 | 3 | def rec_selection_sort(array, x, y): 4 | index = len(array) 5 | if x < index-1: 6 | min_index = x 7 | rec_selection_sort(array, x + 1, x + 2) 8 | if y < index: 9 | if array[y] < array[min_index]: 10 | min_index = y 11 | rec_selection_sort(array, x, y+1) 12 | array[x], array[min_index] = array[min_index], array[x] 13 | 14 | 15 | array = [5, 7, 9, 8, 2, 1] 16 | rec_selection_sort(array, 0, 1) 17 | print(array) 18 | -------------------------------------------------------------------------------- /LAB 5/Task 4 b.py: -------------------------------------------------------------------------------- 1 | # b 2 | 3 | 4 | def pattern(n): 5 | if n == 0: 6 | return 0 7 | else: 8 | pattern(n-1) 9 | print(n, end="") 10 | 11 | 12 | def print_pattern(n, i): 13 | if n == 0: 14 | return 0 15 | else: 16 | space(n-1) 17 | pattern((i)-n+1) 18 | print() 19 | print_pattern(n-1, i) 20 | 21 | 22 | def space(n): 23 | if n == 0: 24 | return 0 25 | else: 26 | space(n - 1) 27 | print("", end=" ") 28 | 29 | 30 | print_pattern(6, 6) 31 | -------------------------------------------------------------------------------- /LAB 6/Task 6.py: -------------------------------------------------------------------------------- 1 | # task 6 2 | 3 | def rec_binary_search(array, left, right, value): 4 | if left > right: 5 | return -1 6 | else: 7 | mid_value = int((left+right) // 2) 8 | if array[mid_value] < value: 9 | return rec_binary_search(array, mid_value+1, right, value) 10 | elif array[mid_value] > value: 11 | return rec_binary_search(array, left, mid_value-1, value) 12 | else: 13 | return mid_value 14 | 15 | 16 | array = [1, 2, 3, 4, 5, 6, 7] 17 | left = 0 18 | right = len(array) 19 | value = 6 20 | print(rec_binary_search(array, left, right, value)) 21 | -------------------------------------------------------------------------------- /LAB 1/task 9.py: -------------------------------------------------------------------------------- 1 | # Circular Array 2 | # 1 Palindrome 3 | 4 | 5 | def palindrome(array, start, size): 6 | empty_list = [] 7 | start_num = start 8 | array_lenght = len(array) 9 | for values in range(size): 10 | if array[start_num] != 0: 11 | empty_list.append(array[start_num]) 12 | start_num += 1 13 | if start_num == array_lenght: 14 | start_num = 0 15 | logic = (len(empty_list)+1)//2 16 | if empty_list[0:logic] == empty_list[-1:-(logic+1):-1]: 17 | print("True") 18 | else: 19 | print("False") 20 | 21 | 22 | array = [20, 10, 0, 0, 0, 10, 20, 30] 23 | palindrome(array, 5, 5) 24 | -------------------------------------------------------------------------------- /LAB 5/Task 5.py: -------------------------------------------------------------------------------- 1 | class FinalQ: 2 | def print(self, array, idx): 3 | if(idx < len(array)): 4 | profit = self.calcProfit(array[idx]) 5 | print(f"{str(idx+1)}. Investment: {array[idx]}; Profit: {profit} ") 6 | self.print(array, idx+1) 7 | 8 | def calcProfit(self, investment): 9 | if investment <= 25000: 10 | return 0.0 11 | elif investment > 25000 and investment <= 100000: 12 | return 45 + self.calcProfit(investment-1000) 13 | elif investment > 100000: 14 | return 80 + self.calcProfit(investment-1000) 15 | else: 16 | return 0 17 | 18 | 19 | array = [25000, 100000, 250000, 350000] 20 | f = FinalQ() 21 | f.print(array, 0) 22 | -------------------------------------------------------------------------------- /LAB 1/task 5.py: -------------------------------------------------------------------------------- 1 | def Splitting_array(A): 2 | zero_list = [0] 3 | empty_list = [] 4 | A_lentgh = len(A) 5 | logic = None 6 | for i in range(A_lentgh): 7 | if i == 0: 8 | zero_list[i] = A[i] 9 | empty_list = A[i+1:(A_lentgh)] 10 | if sum(zero_list) == sum(empty_list): 11 | logic = True 12 | break 13 | else: 14 | zero_list = [] 15 | logic = False 16 | else: 17 | zero_list = A[0:i+1] 18 | empty_list = A[i + 1:(A_lentgh)] 19 | if sum(zero_list) == sum(empty_list): 20 | logic = True 21 | break 22 | else: 23 | logic = False 24 | print(logic) 25 | 26 | 27 | A = [10, 3, 1, 2, 10] 28 | Splitting_array(A) 29 | -------------------------------------------------------------------------------- /LAB 8/Task 4.py: -------------------------------------------------------------------------------- 1 | # task 4 2 | 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.parent = None 8 | self.left = None 9 | self.right = None 10 | 11 | 12 | def inorder(node): 13 | if node is None: 14 | return 15 | else: 16 | inorder(node.left) 17 | print(node.data, end=" ") 18 | inorder(node.right) 19 | 20 | 21 | root = Node(1) 22 | second_node = Node(2) 23 | thrd_node = Node(3) 24 | fourth_node = Node(4) 25 | fifth_node = Node(5) 26 | sixt_node = Node(6) 27 | seven_node = Node(7) 28 | eight_node = Node(8) 29 | root.left = second_node 30 | root.right = thrd_node 31 | thrd_node.left = fourth_node 32 | thrd_node.right = fifth_node 33 | fifth_node.left = sixt_node 34 | fifth_node.right = seven_node 35 | seven_node.left = eight_node 36 | 37 | inorder(root) 38 | -------------------------------------------------------------------------------- /LAB 8/Task 5.py: -------------------------------------------------------------------------------- 1 | # task 5 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.parent = None 7 | self.left = None 8 | self.right = None 9 | 10 | 11 | def postorder(node): 12 | if node is None: 13 | return 14 | else: 15 | postorder(node.left) 16 | postorder(node.right) 17 | print(node.data, end=" ") 18 | 19 | 20 | root = Node(1) 21 | second_node = Node(2) 22 | thrd_node = Node(3) 23 | fourth_node = Node(4) 24 | fifth_node = Node(5) 25 | sixt_node = Node(6) 26 | seven_node = Node(7) 27 | eight_node = Node(8) 28 | root.left = second_node 29 | root.right = thrd_node 30 | thrd_node.left = fourth_node 31 | thrd_node.right = fifth_node 32 | fifth_node.left = sixt_node 33 | fifth_node.right = seven_node 34 | seven_node.left = eight_node 35 | 36 | postorder(root) 37 | -------------------------------------------------------------------------------- /LAB 8/Task 3.py: -------------------------------------------------------------------------------- 1 | # task 3 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.parent = None 7 | self.left = None 8 | self.right = None 9 | 10 | 11 | def preorder(node): 12 | if node is None: 13 | return 14 | else: 15 | print(node.data, end=" ") 16 | preorder(node.left) 17 | preorder(node.right) 18 | 19 | 20 | root = Node(1) 21 | second_node = Node(2) 22 | thrd_node = Node(3) 23 | fourth_node = Node(4) 24 | fifth_node = Node(5) 25 | sixt_node = Node(6) 26 | seven_node = Node(7) 27 | eight_node = Node(8) 28 | root.left = second_node 29 | root.right = thrd_node 30 | thrd_node.left = fourth_node 31 | thrd_node.right = fifth_node 32 | fifth_node.left = sixt_node 33 | fifth_node.right = seven_node 34 | seven_node.left = eight_node 35 | 36 | 37 | preorder(root) 38 | -------------------------------------------------------------------------------- /LAB 5/Task 2 b.py: -------------------------------------------------------------------------------- 1 | # b 2 | class Node: 3 | def __init__(self, value): 4 | self.value = value 5 | self.next = None 6 | 7 | 8 | class linklist: 9 | def __init__(self, array): 10 | self.head = None 11 | tail = None 12 | for x in array: 13 | new_node = Node(x) 14 | if self.head == None: 15 | self.head = new_node 16 | tail = new_node 17 | else: 18 | tail.next = new_node 19 | tail = new_node 20 | 21 | def sum_add(self, head): 22 | if head is None: 23 | return 0 24 | else: 25 | return head.value + self.sum_add(head.next) 26 | 27 | def sum(self): 28 | return self.sum_add(self.head) 29 | 30 | 31 | array = [1, 2, 3, 4, 5, 6, 7] 32 | listlinked = linklist(array) 33 | print(listlinked.sum()) 34 | -------------------------------------------------------------------------------- /LAB 8/task 7.py: -------------------------------------------------------------------------------- 1 | # task 7 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.parent = None 7 | self.left = None 8 | self.right = None 9 | 10 | 11 | def copy_tree(node): 12 | if node == None: 13 | return 14 | else: 15 | print(node.data) 16 | copy_tree(node.left) 17 | copy_tree(node.right) 18 | node.left, node.right = node.right, node.left 19 | 20 | 21 | root = Node(1) 22 | second_node = Node(2) 23 | thrd_node = Node(3) 24 | fourth_node = Node(4) 25 | fifth_node = Node(5) 26 | sixt_node = Node(6) 27 | seven_node = Node(7) 28 | eight_node = Node(8) 29 | root.left = second_node 30 | root.right = thrd_node 31 | thrd_node.left = fourth_node 32 | thrd_node.right = fifth_node 33 | fifth_node.left = sixt_node 34 | fifth_node.right = seven_node 35 | seven_node.left = eight_node 36 | 37 | copy_tree(root) 38 | -------------------------------------------------------------------------------- /LAB 5/Task 2 c.py: -------------------------------------------------------------------------------- 1 | # c 2 | class Node: 3 | def __init__(self, value): 4 | self.value = value 5 | self.next = None 6 | 7 | 8 | class linklist: 9 | def __init__(self, array): 10 | self.head = None 11 | tail = None 12 | for x in array: 13 | new_node = Node(x) 14 | if self.head == None: 15 | self.head = new_node 16 | tail = new_node 17 | else: 18 | tail.next = new_node 19 | tail = new_node 20 | 21 | def reverse(self, head): 22 | if head.next is None: 23 | print(head.value) 24 | else: 25 | self.reverse(head.next) 26 | print(head.value) 27 | 28 | def reverse_print(self): 29 | return self.reverse(self.head) 30 | 31 | 32 | array = [1, 2, 3, 4, 5, 6, 7] 33 | listlinked = linklist(array) 34 | listlinked.reverse_print() 35 | -------------------------------------------------------------------------------- /LAB 8/Task 2.py: -------------------------------------------------------------------------------- 1 | # task 2 2 | 3 | class Node: 4 | 5 | def __init__(self, data): 6 | self.data = data 7 | self.parent = None 8 | self.left = None 9 | self.right = None 10 | 11 | 12 | def get_level(node, data, l): 13 | if node is None: 14 | return 0 15 | if data == node.data: 16 | return l 17 | 18 | leveld = get_level(node.left, data, l + 1) 19 | if leveld != 0: 20 | return leveld 21 | 22 | leveld = get_level(node.right, data, l + 1) 23 | return leveld 24 | 25 | 26 | def getlevel(node, data): 27 | return get_level(node, data, 1) 28 | 29 | 30 | root = Node(1) 31 | second_node = Node(2) 32 | thrd_node = Node(3) 33 | fourth_node = Node(4) 34 | fifth_node = Node(5) 35 | sixt_node = Node(6) 36 | seven_node = Node(7) 37 | eight_node = Node(8) 38 | root.left = second_node 39 | root.right = thrd_node 40 | thrd_node.left = fourth_node 41 | thrd_node.right = fifth_node 42 | fifth_node.left = sixt_node 43 | fifth_node.right = seven_node 44 | seven_node.left = eight_node 45 | 46 | print(f"LEVEL: {getlevel(root,8)}") 47 | -------------------------------------------------------------------------------- /LAB 7/Task 1.py: -------------------------------------------------------------------------------- 1 | # task 1 2 | 3 | def max_val(list): 4 | val = list[0] 5 | for i in list: 6 | if val < abs(i): 7 | val = abs(i) 8 | return val 9 | 10 | 11 | class keyIndex: 12 | def __init__(self, k): 13 | self.k = k 14 | 15 | def key_indx(self): 16 | global maxv 17 | maxv = max_val(self.k) 18 | self.temp = [0] * ((max_val(self.k)*2)+1) 19 | 20 | for i in self.k: 21 | if 0 == self.temp[maxv + i]: 22 | self.temp[maxv + i] = 1 23 | else: 24 | self.temp[maxv+i] = self.temp[maxv+i]+1 25 | 26 | def printlist(self): 27 | print(self.temp) 28 | 29 | def search(self, key): 30 | k1 = int(key) 31 | if 0 > k1+maxv or k1+maxv >= len(self.temp): 32 | print(False) 33 | else: 34 | print(True) 35 | 36 | def sorted(self): 37 | indx = 0 38 | for x in range(0, len(self.temp)): 39 | if len(self.k) <= indx: 40 | print(self.k) 41 | break 42 | if 1 <= self.temp[x]: 43 | for y in range(0, self.temp[x]): 44 | self.k[indx] = x - maxv 45 | indx = indx + 1 46 | 47 | 48 | list = [-4, 7, 3, 4, 2, -9, -7] 49 | key_indx = keyIndex(list) 50 | key_indx.key_indx() 51 | key_indx.printlist() 52 | key_indx.search(3) 53 | key_indx.sorted() 54 | -------------------------------------------------------------------------------- /LAB 8/Task 1.py: -------------------------------------------------------------------------------- 1 | # task 1 2 | 3 | class Node: 4 | def __init__(self, data, parent_node, left, right): 5 | self.data = data 6 | self.parent_node = parent_node 7 | self.left = left 8 | self.right = right 9 | 10 | 11 | class binarytree: 12 | def __init__(self): 13 | self.root = Node(1, None, None, None) 14 | self.second_node = Node(2, self.root, None, None) 15 | self.thrd_node = Node(3, self.root, None, None) 16 | self.fourth_node = Node(4, self.thrd_node, None, None) 17 | self.fifth_node = Node(5, self.thrd_node, None, None) 18 | self.sixt_node = Node(6, self.fifth_node, None, None) 19 | self.seven_node = Node(7, self.fifth_node, None, None) 20 | self.root.left = self.second_node 21 | self.root.right = self.thrd_node 22 | self.thrd_node.left = self.fourth_node 23 | self.thrd_node.right = self.fifth_node 24 | self.fifth_node.left = self.sixt_node 25 | self.fifth_node.right = self.seven_node 26 | 27 | def maximum(self, first, second): 28 | if second <= first: 29 | return first 30 | else: 31 | return second 32 | 33 | def height(self, root): 34 | 35 | if root is None: 36 | return 0 37 | 38 | return 1 + self.maximum(self.height(root.left), self.height(root.right)) 39 | 40 | 41 | tree = binarytree() 42 | 43 | print(" Height : ", tree.height(tree.root)) 44 | -------------------------------------------------------------------------------- /LAB 1/task 11.py: -------------------------------------------------------------------------------- 1 | # 3 2 | 3 | import random 4 | 5 | participant = [1, 2, 3, 4, 5, 6, 7] 6 | parti_lenght = len(participant) 7 | 8 | def musical_chair(list): 9 | while parti_lenght > 1: 10 | print("Player Order:", participant) 11 | randint = random.randint(0, 3) 12 | 13 | if randint == 1: 14 | if parti_lenght % 2 == 0: 15 | delete = int(parti_lenght/2)-1 16 | else: 17 | delete = int(parti_lenght/2) 18 | print(f'player{delete} is out') 19 | participant.pop(delete) 20 | participant.insert(0, participant[len(participant)-1]) 21 | participant.pop(len(participant)-1) 22 | print("Winner is", participant[0]) 23 | 24 | participant = [1, 2, 3, 4, 5, 6, 7] 25 | musical_chair(list) 26 | 27 | Player Order: [1, 2, 3, 4, 5, 6, 7] 28 | Player Order: [7, 1, 2, 3, 4, 5, 6] 29 | player3 is out 30 | Player Order: [6, 7, 1, 2, 4, 5] 31 | player3 is out 32 | Player Order: [5, 6, 7, 1, 4] 33 | Player Order: [4, 5, 6, 7, 1] 34 | Player Order: [1, 4, 5, 6, 7] 35 | Player Order: [7, 1, 4, 5, 6] 36 | Player Order: [6, 7, 1, 4, 5] 37 | player3 is out 38 | Player Order: [5, 6, 7, 1] 39 | Player Order: [1, 5, 6, 7] 40 | Player Order: [7, 1, 5, 6] 41 | Player Order: [6, 7, 1, 5] 42 | player3 is out 43 | Player Order: [1, 6, 7] 44 | Player Order: [7, 1, 6] 45 | player3 is out 46 | -------------------------------------------------------------------------------- /LAB 6/Task 5.py: -------------------------------------------------------------------------------- 1 | # task 5 2 | 3 | class Node: 4 | def __init__(self, value, next, prev): 5 | self.value = value 6 | self.next = next 7 | self.prev = prev 8 | 9 | 10 | class Doublylinkedlist: 11 | def __init__(self, array): 12 | self.head = Node(array[0], None, None) 13 | tail = self.head 14 | for x in range(1, len(array)): 15 | new_node = Node(array[x], None, tail) 16 | tail.next = new_node 17 | tail = new_node 18 | 19 | def showList(self): 20 | if self.head.next is None: 21 | print("Empty list") 22 | else: 23 | head = self.head 24 | while self.head is not None: 25 | if head.next is None: 26 | print(head.value) 27 | break 28 | else: 29 | print(head.value, end=" ") 30 | head = head.next 31 | 32 | def insertion_sort(self): 33 | head = self.head 34 | if head == None: 35 | return 36 | else: 37 | while head is not None: 38 | tail = head.next 39 | while tail and tail.prev != None and tail.value < tail.prev.value: 40 | tail.value, tail.prev.value = tail.prev.value, tail.value 41 | tail = tail.prev 42 | head = head.next 43 | 44 | 45 | array = [7, 6, 9, 3, 5, 99, 1] 46 | doublylinkedlist = Doublylinkedlist(array) 47 | doublylinkedlist.insertion_sort() 48 | doublylinkedlist.showList() 49 | -------------------------------------------------------------------------------- /LAB 6/Task 3.py: -------------------------------------------------------------------------------- 1 | # task 3 2 | 3 | class Node: 4 | def __init__(self, value): 5 | self.value = value 6 | self.next = None 7 | 8 | 9 | class MyList: 10 | def __init__(self, a): 11 | self.head = None 12 | tail = None 13 | 14 | for x in a: 15 | new_node = Node(x) 16 | if self.head == None: 17 | self.head = new_node 18 | tail = new_node 19 | 20 | else: 21 | tail.next = new_node 22 | tail = new_node 23 | 24 | def showList(self): 25 | temp = self.head 26 | if temp is None: 27 | print("Empty List") 28 | else: 29 | while temp is not None: 30 | if temp.next is None: 31 | print(temp.value) 32 | break 33 | else: 34 | print(temp.value, end=" ") 35 | temp = temp.next 36 | 37 | def bubblesort(self): 38 | head = self.head 39 | tail = None 40 | if self.head is None: 41 | return 42 | else: 43 | while head != None: 44 | tail = head.next 45 | while tail != None: 46 | if tail.value < head.value: 47 | head.value, tail.value = tail.value, head.value 48 | tail = tail.next 49 | head = head.next 50 | 51 | 52 | array = [4, 7, 45, 9, 33, 2, 8] 53 | linkedlist = MyList(array) 54 | linkedlist.bubblesort() 55 | linkedlist.showList() 56 | -------------------------------------------------------------------------------- /LAB 8/Task 6.py: -------------------------------------------------------------------------------- 1 | # task 6 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.parent = None 7 | self.left = None 8 | self.right = None 9 | 10 | 11 | def sameornot(tree_node1, tree_node2): 12 | 13 | if tree_node1 == None and tree_node2 == None: 14 | return True 15 | 16 | if tree_node1 and tree_node2 is not None: 17 | return tree_node1.data == tree_node2.data and sameornot(tree_node1.left, tree_node2.left) and sameornot(tree_node1.right, tree_node2.right) 18 | 19 | return False 20 | 21 | 22 | root = Node(1) 23 | second_node = Node(2) 24 | thrd_node = Node(3) 25 | fourth_node = Node(4) 26 | fifth_node = Node(5) 27 | root.left = second_node 28 | root.right = thrd_node 29 | thrd_node.left = fourth_node 30 | thrd_node.right = fifth_node 31 | sixt_node = Node(6) 32 | seven_node = Node(7) 33 | eight_node = Node(8) 34 | fifth_node.left = sixt_node 35 | fifth_node.right = seven_node 36 | seven_node.left = eight_node 37 | 38 | 39 | root2 = Node(1) 40 | second_node2 = Node(2) 41 | thrd_node2 = Node(3) 42 | fourth_node2 = Node(4) 43 | fifth_node2 = Node(5) 44 | root2.left = second_node2 45 | root2.right = thrd_node2 46 | thrd_node2.left = fourth_node2 47 | thrd_node2.right = fifth_node2 48 | sixt_node2 = Node(6) 49 | seven_node2 = Node(7) 50 | eight_node2 = Node(8) 51 | fifth_node2.left = sixt_node2 52 | fifth_node2.right = seven_node2 53 | seven_node2.left = eight_node2 54 | 55 | 56 | if sameornot(root, root2) is True: 57 | print("Both two trees are exactly same") 58 | else: 59 | print("two trees are not exactly same") 60 | -------------------------------------------------------------------------------- /LAB 7/Task 2.py: -------------------------------------------------------------------------------- 1 | # task 2 2 | 3 | 4 | vowels = ["A", "E", "I", "O", "U"] 5 | consonants = ["B", "C", "D", "F", "G", "H", "J", "K", "L", 6 | "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z"] 7 | 8 | 9 | class Hashstring: 10 | def __init__(self, str): 11 | self.str = str 12 | 13 | def string_get(self): 14 | return self.str 15 | 16 | def string_key(self): 17 | val = 0 18 | n = 0 19 | for x in self.str: 20 | if x in consonants: 21 | val = val + 1 22 | if x not in consonants and x not in vowels: 23 | n += int(x) 24 | s_key = ((val * 24) + n) 25 | return s_key 26 | 27 | 28 | class Hashtable: 29 | def __init__(self, size=9): 30 | self.size = size 31 | self.list = [0] * size 32 | 33 | def function(self, string_key): 34 | return string_key % self.size 35 | 36 | def add(self, s): 37 | val = s.string_key() 38 | position = self.function(val) 39 | for y in range(1, self.size): 40 | if self.list[position] == 0: 41 | self.list[position] = s.string_get() 42 | break 43 | else: 44 | position = (val+y) % self.size 45 | 46 | def print(self): 47 | for y in range(self.size): 48 | print(self.list[y]) 49 | 50 | 51 | hash_table = Hashtable() 52 | list = ["X9ZAR2JD6F7", "ZJO9V49NDOU", "G94F5SMC2DG", "KD9LNX46TT2", 53 | "WN2NY54F22D", "YVRFKVUOH6Y", "NJ89I7DN7V8", "5W8UWJSDCJH", "5YLHRBO79CT"] 54 | for x in list: 55 | v = Hashstring(x) 56 | hash_table.add(v) 57 | hash_table.print() 58 | -------------------------------------------------------------------------------- /LAB 6/Task 4.py: -------------------------------------------------------------------------------- 1 | # task 4 2 | 3 | class Node: 4 | def __init__(self, value): 5 | self.value = value 6 | self.next = None 7 | 8 | 9 | class MyList: 10 | def __init__(self, a): 11 | self.head = None 12 | tail = None 13 | 14 | for x in a: 15 | new_node = Node(x) 16 | if self.head == None: 17 | self.head = new_node 18 | tail = new_node 19 | 20 | else: 21 | tail.next = new_node 22 | tail = new_node 23 | 24 | def showList(self): 25 | temp = self.head 26 | if temp is None: 27 | print("Empty List") 28 | else: 29 | while temp is not None: 30 | if temp.next is None: 31 | print(temp.value) 32 | break 33 | else: 34 | print(temp.value, end=" ") 35 | temp = temp.next 36 | 37 | def selection_sort(self): 38 | head = self.head 39 | if self.head is None: 40 | return 41 | else: 42 | while head.next != None: 43 | tail = head 44 | val = head.value 45 | next_val = head.next 46 | while next_val != None: 47 | if val > next_val.value: 48 | val = next_val.value 49 | tail = next_val 50 | next_val = next_val.next 51 | head.value, tail.value = tail.value, head.value 52 | head = head.next 53 | 54 | 55 | array = [4, 7, 45, 9, 33, 2, 8] 56 | linkedlist = MyList(array) 57 | linkedlist.selection_sort() 58 | linkedlist.showList() 59 | -------------------------------------------------------------------------------- /LAB 1/task 10.py: -------------------------------------------------------------------------------- 1 | # 2 Intersection 2 | def intersection(array_1, start1, size1, array_2, start2, size2): 3 | new_list1 = [] 4 | new_list2 = [] 5 | new_list3 = [] 6 | new_list4 = [] 7 | new_list5 = [] 8 | array1_lenght = len(array_1) 9 | array2_lenght = len(array_2) 10 | for values in range(1, array1_lenght): 11 | if array_1[values] == 0 and array_1[values-1] != 0: 12 | for x in range(values): 13 | new_list1.append(array_1[x]) 14 | 15 | for value in range(1, array1_lenght): 16 | if array_1[value] != 0 and array_1[value - 1] == 0: 17 | for x in range(value, array1_lenght): 18 | new_list2.append(array_1[x]) 19 | 20 | for values in range(1, array2_lenght): 21 | if array_2[values] == 0 and array_2[values-1] != 0: 22 | for y in range(values): 23 | new_list3.append(array_2[y]) 24 | 25 | for value in range(1, array2_lenght): 26 | if array_2[value] != 0 and array_2[value - 1] == 0: 27 | for y in range(value, array2_lenght): 28 | new_list4.append(array_2[y]) 29 | 30 | combined_list1 = new_list2 + new_list1 31 | combined_list2 = new_list4 + new_list3 32 | 33 | for value in range(len(combined_list1)): 34 | new = combined_list1[value] 35 | for values in range(len(combined_list2)): 36 | if new == combined_list2[values]: 37 | if new not in new_list5: 38 | new_list5.append(new) 39 | else: 40 | continue 41 | else: 42 | continue 43 | print(new_list5) 44 | 45 | 46 | intersection([40, 50, 0, 0, 0, 10, 20, 30], 5, 6, [ 47 | 10, 20, 5, 0, 0, 0, 0, 0, 5, 40, 15, 25], 8, 7) 48 | -------------------------------------------------------------------------------- /LAB 4/Task 1.py: -------------------------------------------------------------------------------- 1 | # Task 1 2 | 3 | stack = [] 4 | index = [] 5 | openingbrackets = ["[",'{',"("] 6 | closingbrackets = ["]","}",")"] 7 | 8 | 9 | def paranthesis_balancing(string): 10 | t = 0 11 | expression = True 12 | for x in range(0,len(string)): 13 | if string[x] in openingbrackets or string[x] in closingbrackets: 14 | if string[x] in openingbrackets: 15 | stack.append(string[x]) 16 | index.append(x+1) 17 | else: 18 | if len(stack) == 0 and string[x] in closingbrackets: 19 | t = string[x] 20 | index.append(x+1) 21 | expression = False 22 | break 23 | t = stack.pop() 24 | expression = bracket_checking(t,string[x]) 25 | if expression == True: 26 | index.pop() 27 | 28 | 29 | if expression is True: 30 | print("This expression is correct.") 31 | 32 | 33 | else: 34 | if len(stack) != 0: 35 | print('This expression is NOT correct.') 36 | print("Error at character # {0}. '{1}'- not closed.".format(index[-1], t)) 37 | 38 | else: 39 | if expression is False: 40 | print('This expression is NOT correct.') 41 | print("Error at character # {0}. '{1}'- not opened.".format(index[0], t)) 42 | 43 | 44 | 45 | 46 | 47 | def bracket_checking(a,b): 48 | if a == "(" and b == ")": 49 | return True 50 | elif a == "{" and b == "}": 51 | return True 52 | elif a == "[" and b =="]": 53 | return True 54 | else: 55 | return False 56 | 57 | string = input() 58 | paranthesis_balancing(string) 59 | -------------------------------------------------------------------------------- /LAB 4/Task 2.py: -------------------------------------------------------------------------------- 1 | # Task 2 2 | 3 | 4 | class Node: 5 | def __init__(self,data): 6 | self.data = data 7 | self.next = None 8 | 9 | class linkedlist: 10 | def __init__(self): 11 | self.head = None 12 | def push(self,element): 13 | if self.head == None: 14 | self.head = Node(element) 15 | else: 16 | n = self.head 17 | while n.next is not None: 18 | n = n.next 19 | n.next = Node(element) 20 | 21 | def pop(self): 22 | global list_status 23 | global available 24 | if self.head == None: 25 | list_status = "Empty" 26 | available = None 27 | else: 28 | if self.head.next == None: 29 | available = self.head.data 30 | self.head = None 31 | else: 32 | n = self.head 33 | while n.next.next is not None: 34 | n = n.next 35 | available = n.next.data 36 | n.next = None 37 | 38 | 39 | 40 | 41 | openingrackets = ["[",'{',"("] 42 | closingrackets = ["]","}",")"] 43 | 44 | def bracket_checking(a,b): 45 | if a == "(" and b == ")": 46 | return True 47 | elif a == "{" and b == "}": 48 | return True 49 | elif a == "[" and b == "]": 50 | return True 51 | else: 52 | return False 53 | 54 | check = linkedlist() 55 | string = input() 56 | expression = False 57 | index = [] 58 | t = 1 59 | x = 0 60 | y = "" 61 | 62 | for v in string: 63 | if v in openingrackets: 64 | check.push(v) 65 | index.append(t) 66 | if v in closingrackets: 67 | check.pop() 68 | if available == None: 69 | expression = False 70 | x = t 71 | y = v 72 | break 73 | else: 74 | expression = bracket_checking(available,v) 75 | if expression == True: 76 | index.pop() 77 | else: 78 | x = index 79 | y = available 80 | t = t+1 81 | 82 | if expression is True: 83 | print('This expression is correct.') 84 | else: 85 | if isinstance(x,list): #The isinstance() function returns True if the specified object is of the specified type, otherwise False 86 | print('This expression is NOT correct.') 87 | print("Error at character # {0}. '{1}'- not closed.".format(x[-1], y)) 88 | else: 89 | print('This expression is NOT correct.') 90 | print("Error at character # {0}. '{1}'- not opened.".format(x, y)) 91 | -------------------------------------------------------------------------------- /LAB 3/task1+2.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data, next, prev): 3 | self.data = data 4 | self.next = next 5 | self.prev = prev 6 | 7 | 8 | class Doublylist: 9 | def __init__(self, a): 10 | self.head = Node(a[0], None, None) 11 | tail = self.head 12 | for x in range(1, len(a)): 13 | new_node = Node(a[x], None, tail) 14 | tail.next = new_node 15 | tail = new_node 16 | 17 | def showList(self): 18 | if self.head.next is None: 19 | print("Empty list") 20 | else: 21 | head = self.head 22 | while self.head is not None: 23 | if head.next is None: 24 | print(head.data) 25 | break 26 | else: 27 | print(head.data, "->", end=" ") 28 | head = head.next 29 | 30 | def insert(self, newElement): 31 | head = self.head 32 | new_node = Node(newElement, None, None) 33 | while new_node.next is None: 34 | if head.data == new_node.data: 35 | print("key already exists and does not insert the key") 36 | break 37 | if head.next is None: 38 | head.next = new_node 39 | new_node.prev = head 40 | break 41 | head = head.next 42 | 43 | def insert_index(self, newElement, index): 44 | condition = False 45 | if index == 0: 46 | new_node = Node(newElement, self.head, None) 47 | self.head.prev = new_node 48 | self.head = new_node 49 | else: 50 | n = self.head 51 | counter = 0 52 | while n is not None: 53 | n = n.next 54 | if condition is False: 55 | n = self.head 56 | while n is not None: 57 | counter = counter + 1 58 | if n.data == newElement: 59 | condition = True 60 | print("key already exists and does not insert the key.") 61 | if counter == index: 62 | new_node = Node(newElement, n.next, n.prev) 63 | n.next.prev = new_node 64 | n.prev.next = new_node 65 | n = n.next 66 | 67 | def remove(self, index): 68 | if self.head == None: 69 | print("list is empty") 70 | else: 71 | 72 | counter = 0 73 | head = self.head 74 | while head is not None: 75 | 76 | if index == counter: 77 | head.next.prev = head.prev 78 | head.prev.next = head.next 79 | counter = counter+1 80 | head = head.next 81 | if index > counter: 82 | print("index out of range") 83 | 84 | def removeKey(self, key): 85 | head = self.head 86 | while head is not None: 87 | if head.data == key: 88 | head.next.prev = head.prev 89 | head.prev.next = head.next 90 | head = head.next 91 | 92 | return key 93 | 94 | 95 | a = [1, 2, 3, 4, 5] 96 | 97 | mylist = Doublylist(a) 98 | mylist.insert(6) 99 | mylist.insert_index(8, 4) 100 | # mylist.remove(2) 101 | # mylist.removeKey(3) 102 | mylist.showList() 103 | -------------------------------------------------------------------------------- /LAB 2/Task 2.py: -------------------------------------------------------------------------------- 1 | # Task 2 2 | 3 | 4 | class Node: 5 | def __init__(self, value): 6 | self.value = value 7 | self.next = None 8 | 9 | 10 | class MyList: 11 | def __init__(self, a): 12 | self.head = None 13 | tail = None 14 | 15 | for x in a: 16 | new_node = Node(x) 17 | if (self.head == None): 18 | self.head = new_node 19 | tail = new_node 20 | 21 | else: 22 | tail.next = new_node 23 | tail = new_node 24 | 25 | def printList(self): 26 | temp = self.head 27 | while temp is not None: 28 | print(temp.value) 29 | temp = temp.next 30 | 31 | def showList(self): 32 | temp = self.head 33 | if temp is None: 34 | print("Empty List") 35 | else: 36 | while temp is not None: 37 | if temp.next is None: 38 | print(temp.value) 39 | break 40 | else: 41 | print(temp.value, end=" -> ") 42 | temp = temp.next 43 | 44 | def isEmpty(self): 45 | temp = self.head 46 | statement = None 47 | if temp is None: 48 | statement = True 49 | else: 50 | statement = False 51 | print(statement) 52 | 53 | def clear(self): 54 | temp = self.head 55 | while temp is not None: 56 | temp.value = None 57 | temp = temp.next 58 | self.head = None 59 | 60 | def insert(self, newElement): 61 | new_node = Node(newElement) 62 | counter = None 63 | if self.head == None: 64 | self.head = new_node 65 | tail = self.head 66 | while True: 67 | if tail.next is None: 68 | break 69 | if tail.value == newElement: 70 | counter = True 71 | tail = tail.next 72 | if counter is True: 73 | print("Key already exists") 74 | else: 75 | tail.next = new_node 76 | 77 | def insert_index(self, newElement, index): 78 | temp = self.head 79 | counter = 1 80 | while counter < index-1 and temp is not None: 81 | temp = temp.next 82 | counter = counter + 1 83 | if temp is None: 84 | print("key already exists and does not insert the key") 85 | else: 86 | new_node = Node(newElement) 87 | new_node.next = temp.next 88 | temp.next = new_node 89 | 90 | def remove(self, values): 91 | temp = None 92 | if self.head == values: 93 | self.head = self.head.next 94 | temp = str(self.head) 95 | str_value = temp+"was removed from the linked list" 96 | return str_value 97 | else: 98 | temp1 = self.head 99 | while temp1.next is not None: 100 | if temp1.next.value == values: 101 | temp = temp1.next.value 102 | break 103 | temp1 = temp1.next 104 | if temp1.next is None: 105 | print("Element doesn't exist") 106 | else: 107 | temp1.next = temp1.next.next 108 | print(temp, "was removed from the linked list") 109 | 110 | 111 | a = [10, 20, 30, 40] 112 | a2 = [] 113 | list1 = MyList(a) 114 | list2 = MyList(a2) 115 | list1.showList() 116 | list2.showList() 117 | list1.isEmpty() 118 | list2.isEmpty() 119 | list1.insert(50) 120 | list1.showList() 121 | list1.insert(10) 122 | list1.insert_index(60, 5) 123 | list1.showList() 124 | list1.remove(20) 125 | list1.showList() 126 | list1.clear() 127 | list1.showList() 128 | -------------------------------------------------------------------------------- /LAB 2/task 3.py: -------------------------------------------------------------------------------- 1 | # task 3 2 | 3 | 4 | class Node: 5 | def __init__(self, value): 6 | self.value = value 7 | self.next = None 8 | 9 | 10 | class MyList: 11 | def __init__(self): 12 | self.head = None 13 | self.tail = None 14 | 15 | def createlinkedlist(self, num_of_nodes): 16 | node_inputs = int(input("Enter node values: ")) 17 | x = 0 18 | tail = None 19 | self.lenght = 0 20 | while x < num_of_nodes: 21 | if x == 0: 22 | self.head = Node(node_inputs) 23 | tail = self.head 24 | else: 25 | new_node = Node(int(input("Enter node values: "))) 26 | tail.next = new_node 27 | tail = new_node 28 | x = x + 1 29 | self.lenght += 1 30 | 31 | def showlist(self): 32 | temp = self.head 33 | if temp is None: 34 | print("Empty list") 35 | else: 36 | while temp is not None: 37 | if temp.next is None: 38 | print(temp.value) 39 | break 40 | else: 41 | print(temp.value, end=" -> ") 42 | temp = temp.next 43 | 44 | def even_number(self): 45 | temp = self.head 46 | new_head = None 47 | tail = None 48 | while temp is not None: 49 | if temp.value % 2 == 0: 50 | new_node = Node(temp.value) 51 | if new_head == None: 52 | new_head = new_node 53 | tail = new_node 54 | else: 55 | tail.next = new_node 56 | tail = tail.next 57 | temp = temp.next 58 | tail = new_head 59 | while True: 60 | if tail.next is None: 61 | print(tail.value) 62 | break 63 | else: 64 | print(tail.value, end=" -> ") 65 | tail = tail.next 66 | 67 | def find_out(self, value): 68 | temp = self.head 69 | statement = None 70 | while temp.next is None: 71 | if temp.value == value: 72 | statement = True 73 | 74 | if statement == True: 75 | print("True") 76 | else: 77 | print("False") 78 | 79 | def reverse(self): 80 | temp = self.head 81 | before = None 82 | while temp != None: 83 | tail = temp.next 84 | temp.next = before 85 | before = temp 86 | temp = tail 87 | self.head = before 88 | 89 | def sort(self): 90 | self.temp = self.head 91 | self.value = None 92 | 93 | if (self.head == None): 94 | print("empty List") 95 | else: 96 | for val in range(self.lenght): 97 | if self.temp is not None: 98 | self.value = self.temp.next 99 | 100 | for val in range(self.lenght - 1): 101 | if self.value is not None: 102 | if (self.temp.value > self.value.value): 103 | cur = self.temp.value 104 | self.temp.value = self.value.value 105 | self.value.value = cur 106 | self.value = self.value.next 107 | self.temp = self.temp.next 108 | 109 | def sum(self): 110 | new = self.head 111 | total_sum = 0 112 | while new is not None: 113 | total_sum += new.value 114 | new = new.next 115 | print(total_sum) 116 | 117 | def rotate(self, rotate_position, k): 118 | if rotate_position == "left": 119 | for x in range(k): 120 | temp = self.head 121 | self.head = self.head.next 122 | temp.next = None 123 | position = self.head 124 | while position.next != None: 125 | position = position.next 126 | position.next = temp 127 | 128 | elif rotate_position == "right": 129 | for x in range(k): 130 | temp = None 131 | position = self.head 132 | while position.next.next != None: 133 | position = position.next 134 | temp = position.next 135 | position.next = None 136 | temp.next = self.head 137 | self.head = temp 138 | else: 139 | print("Invalid") 140 | 141 | 142 | list1 = MyList() 143 | list2 = list1 144 | list1.createlinkedlist(int(input("Please Enter the Number of Nods: "))) 145 | list1.showlist() 146 | list1.even_number() 147 | list1.find_out(7) 148 | list1.reverse() 149 | list1.showlist() 150 | list1.sort() 151 | list1.showlist() 152 | list1.sum() 153 | list2.rotate("left", 2) 154 | list2.showlist() 155 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DATA-STRUCTURES-CSE-220-BRACU 2 | 3 | # LAB ASSIGNMENTS 4 | 5 |