├── 01. Basics ├── 01. Hello World.py ├── 02. If-Else.py ├── 03. Arithmetic Operators.py ├── 04. Division.py ├── 05. Loops.py ├── 06. Functions.py ├── 07. Print Function.py ├── 08. List comprehension.py ├── 09. Runner-Up Score!.py ├── 10. Nested Lists.py ├── 11. Finding the Percentage.py ├── 12. Lists.py ├── 13. Tuples.py ├── Classes.py └── README.md ├── 02. Array ├── 01. Array implementation.py ├── 02. Insert vs Append.py ├── 03. Delete.py ├── 04. Reverse.py └── 05. Rotation.py ├── 03. Linked List ├── 01. LL implementation.py ├── 02. Insertion.py ├── 03. Deletion.py └── 04. Complete LL.py ├── 04. Stack ├── 01. Stack Implementation.py └── 02. CollectionsDeque.py ├── 05. Queue └── 01. Queue Implementation.py ├── 06. Trees ├── 01. General tree.py ├── 02. Binary Tree imp.py ├── 03. Binary tree.py └── 04. Binary search tree.py ├── README.md ├── file.txt └── main.py /01. Basics/01. Hello World.py: -------------------------------------------------------------------------------- 1 | Here is a sample line of code that can be executed in Python: 2 | print("Hello, World!") 3 | 4 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 5 | 6 | CODE: 7 | if __name__ == '__main__': 8 | print("Hello, World!") 9 | 10 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 11 | -------------------------------------------------------------------------------- /01. Basics/02. If-Else.py: -------------------------------------------------------------------------------- 1 | Given an integer, , perform the following conditional actions: 2 | 3 | If n is odd, print Weird 4 | If n is even and in the inclusive range of 2 to 5, print Not Weird 5 | If n is even and in the inclusive range of 6 to 20, print Weird 6 | If n is even and greater than 20, print Not Weird 7 | 8 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 9 | 10 | CODE: 11 | n = int(input()) 12 | 13 | if n%2!=0: 14 | print('Weird') 15 | else: 16 | if n>=2 and n<=5: 17 | print('Not Weird') 18 | elif n>=6 and n<=20: 19 | print('Weird') 20 | else: 21 | print('Not Weird') 22 | 23 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 24 | -------------------------------------------------------------------------------- /01. Basics/03. Arithmetic Operators.py: -------------------------------------------------------------------------------- 1 | The provided code stub reads two integers from STDIN, and . Add code to print three lines where: 2 | 3 | 1. The first line contains the sum of the two numbers. 4 | 2. The second line contains the difference of the two numbers (first - second). 5 | 3. The third line contains the product of the two numbers. 6 | 7 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 8 | 9 | CODE: 10 | if __name__ == '__main__': 11 | a = int(input()) 12 | b = int(input()) 13 | 14 | print(a+b) 15 | print(a-b) 16 | print(a*b) 17 | 18 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 19 | -------------------------------------------------------------------------------- /01. Basics/04. Division.py: -------------------------------------------------------------------------------- 1 | The provided code stub reads two integers, a and b, from STDIN. 2 | 3 | Add logic to print two lines. The first line should contain the result of integer division, a // b. The second line should contain the result of float division, a / b. 4 | 5 | No rounding or formatting is necessary. 6 | 7 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 8 | 9 | CODE: 10 | if __name__ == '__main__': 11 | a = int(input()) 12 | b = int(input()) 13 | 14 | print(a//b) 15 | print(a/b) 16 | 17 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 18 | -------------------------------------------------------------------------------- /01. Basics/05. Loops.py: -------------------------------------------------------------------------------- 1 | The provided code stub reads and integer n, from STDIN. For all non-negative integers i < N, print Square of n. 2 | 3 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 4 | 5 | CODE: 6 | if __name__ == '__main__': 7 | n = int(input()) 8 | 9 | for i in range(n): 10 | print(i*i) 11 | 12 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 13 | -------------------------------------------------------------------------------- /01. Basics/06. Functions.py: -------------------------------------------------------------------------------- 1 | Given a year, determine whether it is a leap year. If it is a leap year, 2 | return the Boolean True, otherwise return False. 3 | Note that the code stub provided reads from STDIN and passes arguments 4 | to is_leap function. It is only necessary to complete the is_leap function. 5 | 6 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 7 | 8 | CODE: 9 | def is_leap(year): 10 | if year%4==0 and (year%100!=0 or year%400==0): 11 | return True 12 | else: 13 | return False 14 | 15 | year = int(input()) 16 | print(is_leap(year)) 17 | 18 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 19 | -------------------------------------------------------------------------------- /01. Basics/07. Print Function.py: -------------------------------------------------------------------------------- 1 | The included code stub will read an integer n, from STDIN. 2 | Without using any string methods, try to print the following: 3 | 123....n 4 | 5 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 6 | 7 | CODE: 8 | if __name__ == '__main__': 9 | n = int(input()) 10 | 11 | for i in range(1,n+1): 12 | print(i,end='') 13 | 14 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 15 | 16 | 17 | -------------------------------------------------------------------------------- /01. Basics/08. List comprehension.py: -------------------------------------------------------------------------------- 1 | Let's learn about list comprehensions! You are given three integers x, y and z 2 | representing the dimensions of a cuboid along with an integer n. 3 | 4 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 5 | 6 | CODE: 7 | if __name__ == '__main__': 8 | x = int(input()) 9 | y = int(input()) 10 | z = int(input()) 11 | n = int(input()) 12 | 13 | res = [] 14 | for i in range(x+1): 15 | for j in range(y+1): 16 | for k in range(z+1): 17 | if i+j+k != n: 18 | res.append([i,j,k]) 19 | 20 | print(res) 21 | 22 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 23 | -------------------------------------------------------------------------------- /01. Basics/09. Runner-Up Score!.py: -------------------------------------------------------------------------------- 1 | Given the participants score sheet for your University Sports Day, 2 | you are required to find the runner-up score. You are given n scores. 3 | Store them in a list and find the score of the runner-up. 4 | 5 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 6 | 7 | CODE: 8 | if __name__ == '__main__': 9 | n = int(input()) 10 | arr = list(map(int, input().split())) 11 | arr.sort() 12 | k = max(arr) 13 | l = arr.index(k) 14 | print(arr[l-1]) 15 | 16 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 17 | -------------------------------------------------------------------------------- /01. Basics/10. Nested Lists.py: -------------------------------------------------------------------------------- 1 | Given the names and grades for each student in a class of N students, 2 | store them in a nested list and print the name(s) of any student(s) having the second lowest grade. 3 | 4 | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 5 | 6 | CODE: 7 | if __name__ == '__main__': 8 | students = [] 9 | for _ in range(int(input())): 10 | name = input() 11 | score = float(input()) 12 | students.append([name, score]) 13 | 14 | students = sorted(students, key = lambda x: x[1]) 15 | #print(students) 16 | #second_lowest_score = students[1][1] 17 | second_lowest_score = sorted(list(set([x[1] for x in students])))[1] 18 | desired_students = [] 19 | for stu in students: 20 | if stu[1] == second_lowest_score: 21 | desired_students.append(stu[0]) 22 | print("\n".join(sorted(desired_students))) 23 | 24 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 25 | -------------------------------------------------------------------------------- /01. Basics/11. Finding the Percentage.py: -------------------------------------------------------------------------------- 1 | The provided code stub will read in a dictionary containing key/value pairs of name:[marks] for a list of students. 2 | Print the average of the marks array for the student name provided, showing 2 places after the decimal. 3 | 4 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 5 | 6 | CODE: 7 | if __name__ == '__main__': 8 | n = int(input()) 9 | student_marks = {} 10 | for _ in range(n): 11 | name, *line = input().split() 12 | scores = list(map(float, line)) 13 | student_marks[name] = scores 14 | query_name = input() 15 | marks = student_marks[query_name] 16 | print(format(sum(marks)/3,'.2f')) 17 | 18 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 19 | -------------------------------------------------------------------------------- /01. Basics/12. Lists.py: -------------------------------------------------------------------------------- 1 | Consider a list (list = []). You can perform the following commands: 2 | insert, print, remove, append, sort, pop, reverse 3 | 4 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 5 | 6 | CODE: 7 | if __name__ == '__main__': 8 | n = int(input()) 9 | arr1 = [] 10 | commands = [] 11 | for i in range(n): 12 | commands.extend(input().split()) 13 | # print(commands) 14 | for i in range(len(commands)): 15 | if commands[i] == 'insert': 16 | j = int(commands[i + 1]) 17 | k = int(commands[i + 2]) 18 | arr1.insert(j,k) 19 | i += 3 20 | elif commands[i] == 'print': 21 | print(arr1) 22 | elif commands[i] == 'remove': 23 | m = int(commands[i+1]) 24 | for item in arr1: 25 | if item == m: 26 | arr1.remove(m) 27 | break 28 | i += 2 29 | elif commands[i] == 'append': 30 | o = int(commands[i+1]) 31 | arr1.append(o) 32 | i += 2 33 | elif commands[i] == 'sort': 34 | arr1.sort() 35 | elif commands[i] == 'pop': 36 | arr1.pop() 37 | elif commands[i] == 'reverse': 38 | arr1.reverse() 39 | 40 | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- 41 | -------------------------------------------------------------------------------- /01. Basics/13. Tuples.py: -------------------------------------------------------------------------------- 1 | Print the result of hash(t) 2 | 3 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 4 | 5 | CODE: 6 | if __name__ == '__main__': 7 | n = int(input()) 8 | integer_list = map(int, input().split()) 9 | t = tuple(integer_list) 10 | print(hash(t)) 11 | 12 | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 13 | -------------------------------------------------------------------------------- /01. Basics/Classes.py: -------------------------------------------------------------------------------- 1 | class Human: 2 | def __init__(self, name, occupation): 3 | self.name = name 4 | self.occupation = occupation 5 | 6 | def do_work(self): 7 | if self.occupation == 'Developer': 8 | print(self.name, 'does coding') 9 | elif self.occupation == 'CEO': 10 | print(self.name, "leads Company") 11 | else: 12 | print(self.name, 'is an employee') 13 | 14 | def speaks(self): 15 | print(self.name, "says I can do it!!!") 16 | 17 | name = input('Enter name: ') 18 | occ = input('Enter occupation: ') 19 | person = Human(name, occ) 20 | person.do_work() 21 | person.speaks() -------------------------------------------------------------------------------- /01. Basics/README.md: -------------------------------------------------------------------------------- 1 | # HackerRank-Python 2 | URL - https://www.hackerrank.com/domains/python 3 | -------------------------------------------------------------------------------- /02. Array/01. Array implementation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | n = int(input("Enter the size of array: ")) 4 | array_ele = [] 5 | 6 | print("Enter", n, "elements: ") 7 | for i in range(n): 8 | val = input(" ") 9 | array_ele.append(val) 10 | 11 | arr = np.array(array_ele) 12 | 13 | print(arr) -------------------------------------------------------------------------------- /02. Array/02. Insert vs Append.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | n = int(input("Enter the size of array: ")) 4 | array_ele = [] 5 | 6 | print("Enter", n, "elements: ") 7 | for i in range(n): 8 | val = input(" ") 9 | array_ele.append(val) 10 | arr = np.array(array_ele) 11 | print("Initial array: \n",arr) 12 | 13 | # append - adds the element to the last position 14 | # np.append(array, element) 15 | new = input("Enter the element to append: ") 16 | arr = np.append(arr, new) 17 | print("Array after appending:\n",arr) 18 | 19 | # insert - adds the element to the specified position 20 | # np.insert(array, index, element 21 | new = input("Enter the element to insert: ") 22 | pos = int(input("Enter the position: ")) 23 | arr = np.insert(arr, pos, new) 24 | print("array after insertion:\n", arr) -------------------------------------------------------------------------------- /02. Array/03. Delete.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | n = int(input("Enter the size of array: ")) 4 | array_ele = [] 5 | 6 | print("Enter", n, "elements: ") 7 | for i in range(n): 8 | val = input(" ") 9 | array_ele.append(val) 10 | arr = np.array(array_ele) 11 | print("Initial array: \n",arr) 12 | 13 | # Delete - Delete the element from the specified location 14 | pos = int(input("Enter the position: ")) 15 | arr = np.delete(arr, pos) 16 | print("Array after Deletion:\n",arr) 17 | -------------------------------------------------------------------------------- /02. Array/04. Reverse.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | n = int(input("Enter the size of array: ")) 4 | array_ele = [] 5 | 6 | print("Enter", n, "elements: ") 7 | for i in range(n): 8 | val = input(" ") 9 | array_ele.append(val) 10 | arr = np.array(array_ele) 11 | print("Initial array: \n",arr) 12 | 13 | # Reverse the array using slicing 14 | rev_array = arr[::-1] 15 | print("Reversed Array:\n", rev_array) -------------------------------------------------------------------------------- /02. Array/05. Rotation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Get the dimensions of the 2D array from the user 4 | rows = int(input("Enter the number of rows: ")) 5 | cols = int(input("Enter the number of columns: ")) 6 | matx =rows*cols 7 | # Initialize an empty list to store the user input 8 | lst = [] 9 | 10 | # Loop to take user input for each element 11 | print("Enter", matx, "elements: ") 12 | for i in range(rows): 13 | row_input = [] 14 | for j in range(cols): 15 | element = float(input("")) 16 | row_input.append(element) 17 | lst.append(row_input) 18 | 19 | # Convert the list to a NumPy array 20 | array_2d = np.array(lst) 21 | 22 | # Print the resulting 2D NumPy array 23 | print("Initial 2D Array:\n", array_2d) 24 | 25 | # Rotation 26 | rotated_array = np.rot90(array_2d,k=1) 27 | print("Rotated array:\n", rotated_array) 28 | -------------------------------------------------------------------------------- /03. Linked List/01. LL implementation.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data=None, next=None): 3 | self.data = data 4 | self.next = next 5 | 6 | class LinkedList: 7 | def __init__(self): 8 | self.head = None 9 | 10 | if __name__ == '__main__': 11 | pass 12 | -------------------------------------------------------------------------------- /03. Linked List/02. Insertion.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data=None, next=None): 3 | self.data = data 4 | self.next = next 5 | 6 | class LinkedList: 7 | def __init__(self): 8 | self.head = None 9 | 10 | # Insert the element to the beginning of the Linked List 11 | def insert_at_beginning(self, data): 12 | node = Node(data, self.head) 13 | self.head = node 14 | 15 | # Insert the element to the end of the Linked List 16 | def insert_at_end(self, data): 17 | if self.head is None: 18 | self.head = Node(data, None) 19 | return 20 | itr = self.head 21 | while itr.next: 22 | itr = itr.next 23 | itr.next = Node(data, None) 24 | 25 | # Print the Linked List 26 | def print(self): 27 | if self.head is None: 28 | print("Linked list is empty") 29 | return 30 | itr = self.head 31 | llstr = '' 32 | while itr: 33 | llstr += str(itr.data) + '-->' 34 | itr = itr.next 35 | print(llstr) 36 | 37 | 38 | if __name__ == '__main__': 39 | ll = LinkedList() 40 | 41 | while True: 42 | choice = int(input("\n\n1. Insert at Beginning\n2. Insert at End\n3. Terminate\nEnter your choice: ")) 43 | if choice == 1: 44 | data = int(input("Enter the data: ")) 45 | ll.insert_at_beginning(data) 46 | elif choice == 2: 47 | data = int(input("Enter the data: ")) 48 | ll.insert_at_end(data) 49 | elif choice == 3: 50 | print("\nTerminated !!") 51 | break 52 | else: 53 | print("invalid input!!") 54 | print("Linked List:") 55 | ll.print() -------------------------------------------------------------------------------- /03. Linked List/03. Deletion.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data=None, next=None): 3 | self.data = data 4 | self.next = next 5 | 6 | 7 | class LinkedList: 8 | def __init__(self): 9 | self.head = None 10 | 11 | # Insert the element to the beginning of the Linked List 12 | def insert_at_beginning(self, data): 13 | node = Node(data, self.head) 14 | self.head = node 15 | 16 | # Insert the element to the end of the Linked List 17 | def insert_at_end(self, data): 18 | if self.head is None: 19 | self.head = Node(data, None) 20 | return 21 | itr = self.head 22 | while itr.next: 23 | itr = itr.next 24 | itr.next = Node(data, None) 25 | 26 | # Get the lenght of the linked list 27 | def get_lenght(self): 28 | count = 0 29 | itr = self.head 30 | while itr: 31 | count+=1 32 | itr = itr.next 33 | return count 34 | 35 | # Remove the element at a given index 36 | def remove_at(self, index): 37 | if index<0 or index>self.get_lenght() or index==self.get_lenght(): 38 | raise Exception("Invalid Index") 39 | if index==0: 40 | self.head = self.head.next 41 | return 42 | count=0 43 | itr = self.head 44 | while itr: 45 | if count == index-1: 46 | itr.next = itr.next.next 47 | break 48 | itr = itr.next 49 | count += 1 50 | 51 | # Print the Linked List 52 | def print(self): 53 | if self.head is None: 54 | print("Linked list is empty") 55 | return 56 | itr = self.head 57 | llstr = '' 58 | while itr: 59 | llstr += str(itr.data) + '-->' 60 | itr = itr.next 61 | print(llstr) 62 | 63 | 64 | if __name__ == '__main__': 65 | ll = LinkedList() 66 | 67 | while True: 68 | choice = int(input("\n\n1. Insert at Beginning\n2. Insert at End\n3. Remove at\n4. Terminate\nEnter your choice: ")) 69 | if choice == 1: 70 | data = int(input("Enter the data: ")) 71 | ll.insert_at_beginning(data) 72 | elif choice == 2: 73 | data = int(input("Enter the data: ")) 74 | ll.insert_at_end(data) 75 | elif choice == 3: 76 | index = int(input("Enter the index: ")) 77 | ll.remove_at(index) 78 | elif choice == 4: 79 | print("Terminating Program") 80 | break 81 | else: 82 | print("invalid input!!") 83 | ll.print() 84 | print("\n\nFinal Linked List:") 85 | ll.print() -------------------------------------------------------------------------------- /03. Linked List/04. Complete LL.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data=None, next=None): 3 | # Initialize a node with data and a reference to the next node 4 | self.data = data 5 | self.next = next 6 | 7 | 8 | class LinkedList: 9 | def __init__(self): 10 | # Initialize an empty linked list with a head that is initially None 11 | self.head = None 12 | 13 | # Insert the element to the beginning of the Linked List 14 | def insert_at_beginning(self, data): 15 | # Create a new node with data and make it the new head 16 | node = Node(data, self.head) 17 | self.head = node 18 | 19 | # Insert the element to the end of the Linked List 20 | def insert_at_end(self, data): 21 | if self.head is None: 22 | # If the list is empty, make the new node the head 23 | self.head = Node(data, None) 24 | return 25 | itr = self.head 26 | while itr.next: 27 | # Traverse the list to find the last node 28 | itr = itr.next 29 | # Insert the new node at the end 30 | itr.next = Node(data, None) 31 | 32 | # Get the length of the linked list 33 | def get_length(self): 34 | count = 0 35 | itr = self.head 36 | while itr: 37 | # Traverse the list and count nodes 38 | count += 1 39 | itr = itr.next 40 | return count 41 | 42 | # Remove an element at the beginning 43 | def remove_at_begin(self): 44 | if self.head is None: 45 | print("The list is empty") 46 | return 47 | # Set the head to the next node, effectively removing the first node 48 | 49 | # Remove the element at the end 50 | def remove_at_end(self): 51 | if self.head is None: 52 | print("The list is empty") 53 | return 54 | if self.head.next is None: 55 | # If there is only one element in the list, make the list empty 56 | self.head = None 57 | return 58 | itr = self.head 59 | while itr.next: 60 | # Traverse the list to find the second-to-last node 61 | self.prev = itr 62 | itr = itr.next 63 | # Remove the last node by updating the second-to-last node's next reference 64 | 65 | # Print the Linked List 66 | def print(self): 67 | if self.head is None: 68 | print("Linked list is empty") 69 | return 70 | itr = self.head 71 | llstr = '' 72 | while itr: 73 | # Traverse the list and build a string representation 74 | llstr += str(itr.data) + '-->' 75 | itr = itr.next 76 | print(llstr) 77 | 78 | # Main program 79 | if __name__ == '__main__': 80 | ll = LinkedList() 81 | 82 | while True: 83 | choice = int(input("\n\n1. Insert at Beginning\n2. Insert at End\n3. Remove at beginning\n4. Remove at End\n5. Terminate\nEnter your choice: ")) 84 | if choice == 1: 85 | data = int(input("Enter the data: ")) 86 | ll.insert_at_beginning(data) 87 | elif choice == 2: 88 | data = int(input("Enter the data: ")) 89 | ll.insert_at_end(data) 90 | elif choice == 3: 91 | ll.remove_at_begin() 92 | elif choice == 4: 93 | ll.remove_at_end() 94 | elif choice == 5: 95 | print("Terminating Program") 96 | break 97 | else: 98 | print("Invalid input!!") 99 | ll.print() 100 | print("\n\nFinal Linked List:") 101 | ll.print() 102 | -------------------------------------------------------------------------------- /04. Stack/01. Stack Implementation.py: -------------------------------------------------------------------------------- 1 | # Implementation of a stack using a list 2 | stack = [] 3 | 4 | if __name__ == '__main__': 5 | while True: 6 | print("1. Push\n2. Pop\n3. Return\n4. Terminate") 7 | choice = int(input("Enter your choice: ")) 8 | 9 | if choice == 1: 10 | data = int(input("Enter the item: ")) 11 | stack.append(data) # Push operation: Add an item to the stack 12 | elif choice == 2: 13 | if not stack: 14 | print("Stack is empty. Cannot pop.") 15 | else: 16 | popped_item = stack.pop() # Pop operation: Remove and return the top item from the stack 17 | print(f"Popped item: {popped_item}") 18 | elif choice == 3: 19 | if not stack: 20 | print("Stack is empty. Cannot return.") 21 | else: 22 | top_item = stack[-1] # Return operation: Return the top item from the stack without removing it 23 | print(f"Top item: {top_item}") 24 | elif choice == 4: 25 | print("Program terminated!!") 26 | break 27 | else: 28 | print("Invalid input!!") 29 | 30 | print("Current stack:", stack) # Print the stack after every iteration 31 | 32 | print("The final stack is:", stack) 33 | -------------------------------------------------------------------------------- /04. Stack/02. CollectionsDeque.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | class Stack: 4 | def __init__(self): 5 | self.container = deque() 6 | 7 | def push(self, val): 8 | self.container.append(val) 9 | 10 | def pop(self): 11 | return self.container.pop() 12 | 13 | def peek(self): 14 | return self.container[-1] 15 | 16 | def is_empty(self): 17 | return len(self.container) == 0 18 | 19 | def size(self): 20 | return len(self.container) 21 | 22 | # Create a Stack object 23 | stack = Stack() 24 | 25 | while True: 26 | print("Menu:") 27 | print("1. Push") 28 | print("2. Pop") 29 | print("3. Peek") 30 | print("4. Check if the stack is empty") 31 | print("5. Get the size of the stack") 32 | print("6. Terminate") 33 | 34 | choice = int(input("Enter your choice: ")) 35 | 36 | if choice == 1: 37 | val = int(input("Enter the value to push: ")) 38 | stack.push(val) 39 | 40 | elif choice == 2: 41 | if not stack.is_empty(): 42 | popped_val = stack.pop() 43 | print(f"Popped value: {popped_val}") 44 | else: 45 | print("Stack is empty. Cannot pop.") 46 | 47 | elif choice == 3: 48 | if not stack.is_empty(): 49 | top_val = stack.peek() 50 | print(f"Top value: {top_val}") 51 | else: 52 | print("Stack is empty. Nothing to peek.") 53 | 54 | elif choice == 4: 55 | if stack.is_empty(): 56 | print("Stack is empty.") 57 | else: 58 | print("Stack is not empty.") 59 | 60 | elif choice == 5: 61 | print(f"Size of the stack: {stack.size()}") 62 | 63 | elif choice == 6: 64 | print("Program terminated!!") 65 | break 66 | else: 67 | print("Invalid choice. Try again!!!") 68 | 69 | # Display the current state of the stack after each iteration 70 | print("Current Stack:", list(stack.container)) -------------------------------------------------------------------------------- /05. Queue/01. Queue Implementation.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | class Queue: 4 | def __init__(self): 5 | self.container = deque() 6 | 7 | def enqueue(self, val): 8 | self.container.appendleft(val) # Use appendleft to enqueue 9 | 10 | def dequeue(self): 11 | if not self.is_empty(): 12 | return self.container.pop() # Use pop to dequeue 13 | else: 14 | return "Queue is empty. Cannot dequeue." 15 | 16 | def front(self): 17 | if not self.is_empty(): 18 | return self.container[-1] 19 | else: 20 | return "Queue is empty. Nothing at the front." 21 | 22 | def is_empty(self): 23 | return len(self.container) == 0 24 | 25 | def size(self): 26 | return len(self.container) 27 | 28 | # Create a Queue object 29 | queue = Queue() 30 | 31 | while True: 32 | print("Menu:") 33 | print("1. Enqueue") 34 | print("2. Dequeue") 35 | print("3. Front") 36 | print("4. Check if the queue is empty") 37 | print("5. Get the size of the queue") 38 | print("6. Terminate") 39 | 40 | choice = int(input("Enter your choice: ")) 41 | 42 | if choice == 1: 43 | val = int(input("Enter the value to enqueue: ")) 44 | queue.enqueue(val) 45 | 46 | elif choice == 2: 47 | if not queue.is_empty(): 48 | dequeued_val = queue.dequeue() 49 | print(f"Dequeued value: {dequeued_val}") 50 | else: 51 | print("Queue is empty. Cannot dequeue.") 52 | 53 | elif choice == 3: 54 | if not queue.is_empty(): 55 | front_val = queue.front() 56 | print(f"Front value: {front_val}") 57 | else: 58 | print("Queue is empty. Nothing at the front.") 59 | 60 | elif choice == 4: 61 | if queue.is_empty(): 62 | print("Queue is empty.") 63 | else: 64 | print("Queue is not empty.") 65 | 66 | elif choice == 5: 67 | print(f"Size of the queue: {queue.size()}") 68 | 69 | elif choice == 6: 70 | print("Program terminated!!") 71 | break 72 | else: 73 | print("Invalid choice. Try again!!!") 74 | 75 | # Display the current state of the queue after each iteration 76 | print("Current Queue:", list(queue.container)) 77 | -------------------------------------------------------------------------------- /06. Trees/01. General tree.py: -------------------------------------------------------------------------------- 1 | class TreeNode: 2 | def __init__(self, data): 3 | self.data = data 4 | self.children = [] 5 | self.parent = None 6 | 7 | def add_child(self, child): 8 | child.parent = self 9 | self.children.append(child) 10 | 11 | def build_tree(): 12 | root_data = input("Enter data for the root node: ") 13 | root = TreeNode(root_data) 14 | 15 | queue = [root] 16 | 17 | while queue: 18 | current_node = queue.pop(0) 19 | num_children = int(input(f"Enter the number of children for '{current_node.data}': ")) 20 | for i in range(num_children): 21 | child_data = input(f"Enter data for child {i + 1} of '{current_node.data}': ") 22 | child_node = TreeNode(child_data) 23 | current_node.add_child(child_node) 24 | queue.append(child_node) 25 | 26 | return root 27 | 28 | def print_tree(node, indent=""): 29 | print(indent + node.data) 30 | for child in node.children: 31 | print_tree(child, indent + " ") 32 | 33 | if __name__ == '__main__': 34 | tree = build_tree() 35 | 36 | while True: 37 | print("\nMenu:") 38 | print("1. Print Tree") 39 | print("2. Quit") 40 | choice = input("Enter your choice: ") 41 | 42 | if choice == '1': 43 | print("\nTree Structure:") 44 | print_tree(tree) 45 | elif choice == '2': 46 | break 47 | else: 48 | print("Invalid choice. Please choose a valid option.") 49 | -------------------------------------------------------------------------------- /06. Trees/02. Binary Tree imp.py: -------------------------------------------------------------------------------- 1 | class BinarySearchTreeNode: 2 | 3 | def __init__(self, data): 4 | self.data = data 5 | self.left = None 6 | self.right = None 7 | 8 | def add_child(self, data): 9 | if data == self.data: 10 | return 11 | 12 | if data < self.data: 13 | # add data in left subtree 14 | if self.left: 15 | self.left.add_child(data) 16 | else: 17 | self.left = BinarySearchTreeNode(data) 18 | else: 19 | # add data in right subtree 20 | if self.right: 21 | self.right.add_child(data) 22 | else: 23 | self.right = BinarySearchTreeNode(data) 24 | -------------------------------------------------------------------------------- /06. Trees/03. Binary tree.py: -------------------------------------------------------------------------------- 1 | class BinarySearchTreeNode: 2 | 3 | def __init__(self, data): 4 | self.data = data 5 | self.left = None 6 | self.right = None 7 | 8 | def add_child(self, data): 9 | if data == self.data: 10 | return 11 | if data < self.data: 12 | # add data in left subtree 13 | if self.left: 14 | self.left.add_child(data) 15 | else: 16 | self.left = BinarySearchTreeNode(data) 17 | else: 18 | # add data in right subtree 19 | if self.right: 20 | self.right.add_child(data) 21 | else: 22 | self.right = BinarySearchTreeNode(data) 23 | 24 | def in_order_traversal(self): 25 | elements = [] 26 | # visit left tree 27 | if self.left: 28 | elements += self.left.in_order_traversal() 29 | # visit base node 30 | elements.append(self.data) 31 | # visit right node 32 | if self.right: 33 | elements += self.right.in_order_traversal() 34 | 35 | return elements 36 | 37 | def build_tree(elements): 38 | root = BinarySearchTreeNode(elements[0]) 39 | for i in range(1, len(elements)): 40 | root.add_child(elements[i]) 41 | 42 | return root 43 | 44 | if __name__ == '__main__': 45 | numbers = [] 46 | size = int(input("Enter number of elements: ")) 47 | print("Enter ",size,"elements: ") 48 | for i in range(size): 49 | num = int(input()) 50 | numbers.append(num) 51 | number_tree = build_tree(numbers) 52 | print(number_tree.in_order_traversal()) -------------------------------------------------------------------------------- /06. Trees/04. Binary search tree.py: -------------------------------------------------------------------------------- 1 | class BinarySearchTreeNode: 2 | 3 | def __init__(self, data): 4 | self.data = data 5 | self.left = None 6 | self.right = None 7 | 8 | def add_child(self, data): 9 | if data == self.data: 10 | return 11 | if data < self.data: 12 | # add data in left subtree 13 | if self.left: 14 | self.left.add_child(data) 15 | else: 16 | self.left = BinarySearchTreeNode(data) 17 | else: 18 | # add data in right subtree 19 | if self.right: 20 | self.right.add_child(data) 21 | else: 22 | self.right = BinarySearchTreeNode(data) 23 | 24 | def in_order_traversal(self): 25 | elements = [] 26 | # visit left tree 27 | if self.left: 28 | elements += self.left.in_order_traversal() 29 | # visit base node 30 | elements.append(self.data) 31 | # visit right node 32 | if self.right: 33 | elements += self.right.in_order_traversal() 34 | 35 | return elements 36 | 37 | def search(self, val): 38 | if self.data == val: 39 | return True 40 | if val < self.data: 41 | # value might be in left tree 42 | if self.left: 43 | return self.left.search(val) 44 | else: 45 | return False 46 | if val > self.data: 47 | # value might be in right tree 48 | if self.right: 49 | return self.right.search(val) 50 | else: 51 | return False 52 | def build_tree(elements): 53 | root = BinarySearchTreeNode(elements[0]) 54 | for i in range(1, len(elements)): 55 | root.add_child(elements[i]) 56 | 57 | return root 58 | 59 | if __name__ == '__main__': 60 | numbers = [] 61 | size = int(input("Enter number of elements: ")) 62 | print("Enter ",size,"elements: ") 63 | for i in range(size): 64 | num = int(input()) 65 | numbers.append(num) 66 | number_tree = build_tree(numbers) 67 | 68 | val_to_search = int(input("Enter the element to search: ")) 69 | print(number_tree.search(val_to_search)) 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures: 2 | 3 | ### Arrays and Strings 4 | * Arrays are collections of elements, while strings are sequences of characters. 5 | * Important operations include indexing, slicing, reversing, and searching. 6 | 7 | ### Linked Lists 8 | * Linked lists are linear data structures made up of nodes, each containing data and a reference to the next node. 9 | * Common operations include insertion, deletion, and traversal. 10 | 11 | ### Stacks 12 | * Stacks follow the Last-In-First-Out (LIFO) principle. 13 | * They support push (insertion) and pop (removal) operations. 14 | 15 | ### Queues 16 | * Queues follow the First-In-First-Out (FIFO) principle. 17 | * Common operations include enqueue (insertion) and dequeue (removal). 18 | 19 | ### Trees 20 | * Trees are hierarchical data structures with a root node, parent-child relationships, and leaf nodes. 21 | * Binary trees have at most two children per node. 22 | 23 | ### Graphs 24 | * Graphs represent connections between nodes (vertices) through edges. 25 | * Graph traversal algorithms such as BFS & DFS. 26 | 27 | ### Hash Tables 28 | * Hash tables use a hash function to map keys to values, providing constant-time average case lookup. 29 | * They handle collisions using techniques like chaining or open addressing. 30 | -------------------------------------------------------------------------------- /file.txt: -------------------------------------------------------------------------------- 1 | 1 days ago1 days ago1 days ago1 days ago2 days ago2 days ago3 days ago3 days ago3 days ago3 days ago3 days ago3 days ago3 days ago3 days ago3 days ago4 days ago4 days ago4 days ago4 days ago4 days ago4 days ago4 days ago4 days ago5 days ago5 days ago5 days ago5 days ago5 days ago5 days ago5 days ago5 days ago5 days ago5 days ago6 days ago7 days ago7 days ago8 days ago8 days ago8 days ago8 days ago8 days ago8 days ago8 days ago9 days ago9 days ago9 days ago9 days ago9 days ago9 days ago10 days ago10 days ago10 days ago10 days ago10 days ago10 days ago10 days ago10 days ago10 days ago10 days ago11 days ago11 days ago11 days ago11 days ago11 days ago11 days ago11 days ago11 days ago11 days ago11 days ago12 days ago12 days ago12 days ago12 days ago12 days ago12 days ago12 days ago13 days ago13 days ago13 days ago13 days ago13 days ago13 days ago13 days ago13 days ago13 days ago14 days ago15 days ago15 days ago15 days ago16 days ago16 days ago16 days ago16 days ago16 days ago16 days ago16 days ago16 days ago16 days ago16 days ago17 days ago17 days ago17 days ago18 days ago18 days ago18 days ago19 days ago19 days ago19 days ago19 days ago19 days ago19 days ago19 days ago19 days ago20 days ago20 days ago20 days ago20 days ago20 days ago20 days ago21 days ago21 days ago21 days ago21 days ago21 days ago21 days ago21 days ago21 days ago22 days ago22 days ago22 days ago22 days ago22 days ago23 days ago23 days ago23 days ago24 days ago24 days ago24 days ago24 days ago24 days ago24 days ago24 days ago25 days ago25 days ago25 days ago26 days ago27 days ago27 days ago27 days ago28 days ago28 days ago28 days ago28 days ago28 days ago28 days ago28 days ago28 days ago28 days ago28 days ago29 days ago29 days ago29 days ago29 days ago29 days ago30 days ago30 days ago30 days ago31 days ago31 days ago31 days ago31 days ago32 days ago32 days ago33 days ago33 days ago33 days ago30 days ago30 days ago31 days ago31 days ago31 days ago31 days ago31 days ago32 days ago33 days ago33 days ago33 days ago0 days ago1 days ago1 days ago1 days ago2 days ago3 days ago3 days ago4 days ago4 days ago4 days ago4 days ago4 days ago5 days ago6 days ago7 days ago7 days ago7 days ago8 days ago8 days ago8 days ago8 days ago9 days ago9 days ago10 days ago10 days ago10 days ago10 days ago10 days ago11 days ago12 days ago12 days ago12 days ago12 days ago12 days ago13 days ago13 days ago13 days ago13 days ago13 days ago14 days ago14 days ago14 days ago14 days ago15 days ago16 days ago16 days ago16 days ago17 days ago17 days ago17 days ago18 days ago18 days ago18 days ago19 days ago19 days ago20 days ago20 days ago20 days ago21 days ago21 days ago21 days ago21 days ago21 days ago22 days ago22 days ago22 days ago22 days ago22 days ago23 days ago23 days ago23 days ago23 days ago23 days ago24 days ago24 days ago25 days ago25 days ago25 days ago25 days ago25 days ago26 days ago26 days ago26 days ago27 days ago27 days ago27 days ago28 days ago29 days ago29 days ago29 days ago29 days ago29 days ago30 days ago30 days ago30 days ago31 days ago32 days ago32 days ago32 days ago32 days ago32 days ago33 days ago0 days ago0 days ago1 days ago1 days ago1 days ago1 days ago2 days ago2 days ago3 days ago3 days ago4 days ago4 days ago4 days ago4 days ago5 days ago5 days ago5 days ago5 days ago5 days ago6 days ago6 days ago6 days ago6 days ago6 days ago7 days ago7 days ago7 days ago7 days ago8 days ago8 days ago9 days ago9 days ago9 days ago9 days ago10 days ago10 days ago10 days ago11 days ago11 days ago11 days ago12 days ago12 days ago12 days ago13 days ago13 days ago13 days ago14 days ago14 days ago14 days ago14 days ago15 days ago15 days ago15 days ago15 days ago15 days ago16 days ago16 days ago16 days ago16 days ago16 days ago17 days ago18 days ago18 days ago18 days ago18 days ago18 days ago19 days ago19 days ago19 days ago19 days ago19 days ago20 days ago21 days ago21 days ago21 days ago22 days ago22 days ago23 days ago24 days ago24 days ago24 days ago25 days ago26 days ago26 days ago26 days ago26 days ago27 days ago27 days ago27 days ago28 days ago28 days ago28 days ago28 days ago29 days ago30 days ago30 days ago30 days ago30 days ago31 days ago31 days ago31 days ago31 days ago32 days ago33 days ago33 days ago33 days ago33 days ago33 days ago0 days ago0 days ago1 days ago1 days ago1 days ago1 days ago1 days ago2 days ago2 days ago3 days ago3 days ago3 days ago3 days ago3 days ago4 days ago4 days ago4 days ago4 days ago4 days ago5 days ago6 days ago6 days ago6 days ago7 days ago8 days ago8 days ago8 days ago8 days ago9 days ago9 days ago10 days ago10 days ago10 days ago10 days ago11 days ago11 days ago. 2 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os 2 | from random import randint 3 | 4 | for i in range(0, 12): 5 | for j in range(0, randint(1,5)): 6 | d = str(i) + ' days ago' 7 | with open('file.txt', 'a') as file: 8 | file.write(d) 9 | os.system('git add .') 10 | os.system('git commit --date="' + d + '" -m "commit"') 11 | 12 | os.system(('git push -u origin main')) --------------------------------------------------------------------------------