diff --git a/.idea/python-programs.iml b/.idea/python-programs.iml index 8b5717a..66b1709 100644 --- a/.idea/python-programs.iml +++ b/.idea/python-programs.iml @@ -1,14 +1,7 @@ - + - - - - - \ No newline at end of file diff --git a/Tuple.py b/Tuple.py new file mode 100644 index 0000000..5fe7c5a --- /dev/null +++ b/Tuple.py @@ -0,0 +1,7 @@ +# 8-1-22 +# Tuple + +a = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) +print(a[1:8]) +print(a[1:]) +print(a[:5]) diff --git a/Tuple1.py b/Tuple1.py new file mode 100644 index 0000000..d4e43e7 --- /dev/null +++ b/Tuple1.py @@ -0,0 +1,14 @@ +# 9-1-23 +# tuple program +# Tuple with integer types +a = (30, 10, 20) +# Tuple with string types +b = ("Suresh", "Rohini", "Trisha", "Hashish") +# Tuple with mix types +c = (10, "Never Give up", 20) +# Empty Tuple +d = () +print("a = ", a) +print("b = ", b) +print("c = ", c) +print("d = ", d) diff --git a/binary.py b/binary.py new file mode 100644 index 0000000..b02822a --- /dev/null +++ b/binary.py @@ -0,0 +1,60 @@ +# 31-12-22 +# binary search in delete operation +# Definition: Binary tree node. +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None + + +def delete_Node(root, key): + # if root doesn't exist, just return it + if not root: + return root + # Find the node in the left subtree if key value is less than root value + if root.val > key: + root.left = delete_Node(root.left, key) + # Find the node in right subtree if key value is greater than root value, + elif root.val < key: + root.right = delete_Node(root.right, key) + # Delete the node if root.value == key + else: + # If there is no right children delete the node and new root would be root.left + if not root.right: + return root.left + # If there is no left children delete the node and new root would be root.right + if not root.left: + return root.right + # If both left and right children exist in the node replace its value with + # the minmimum value in the right subtree. Now delete that minimum node + # in the right subtree + temp_val = root.right + mini_val = temp_val.val + while temp_val.left: + temp_val = temp_val.left + mini_val = temp_val.val + # Delete the minimum node in right subtree + root.right = deleteNode(root.right, root.val) + return root + + +def preOrder(node): + if not node: + return + print(node.val) + preOrder(node.left) + preOrder(node.right) + + +root = TreeNode(5) +root.left = TreeNode(3) +root.right = TreeNode(6) +root.left.left = TreeNode(2) +root.left.right = TreeNode(4) +root.left.right.left = TreeNode(7) +print("Original node:") +print(preOrder(root)) +result = delete_Node(root, 4) +print("After deleting specified node:") +print(preOrder(result)) diff --git a/binary_Tree.py b/binary_Tree.py index 6dca3c0..06d4335 100644 --- a/binary_Tree.py +++ b/binary_Tree.py @@ -1,19 +1,39 @@ -# 7-12-22 +# 28-12-22 # binary tree -#Creating binary tree -# from given list -from binarytree import build +class Node: + def __init__(self, data): + self.left = None + self.right = None + self.data = data + def insert(self, data): +# Compare the new value with the parent node + if self.data: + if data < self.data: + if self.left is None: + self.left = Node(data) + else: + self.left.insert(data) + else: + data > self.data + if self.right is None: + self.right = Node(data) + else: + self.right.insert(data) + else: + self.data = data -# List of nodes -nodes =[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] +# Print the tree + def PrintTree(self): + if self.left: + self.left.PrintTree() + print( self.data), + if self.right: + self.right.PrintTree() -# Building the binary tree -binary_tree = build(nodes) -print('Binary tree from list :\n', - binary_tree) - -# Getting list of nodes from -# binarytree -print('\nList from binary tree :', - binary_tree.values) +# Use the insert method to add nodes +root = Node(12) +root.insert(6) +root.insert(14) +root.insert(3) +root.PrintTree() diff --git a/binary_search_.py b/binary_search_.py new file mode 100644 index 0000000..32f64f4 --- /dev/null +++ b/binary_search_.py @@ -0,0 +1 @@ +t \ No newline at end of file diff --git a/binary_search_tree.py b/binary_search_tree.py index 823d1af..6db2a3c 100644 --- a/binary_search_tree.py +++ b/binary_search_tree.py @@ -1,16 +1,53 @@ -#7-12-22 +# 30-12-22 # binary search tree -# Creating node class + class Node: - def __init__(self, data): - self.data = data - self.leftChild = None - self.rightChild = None - - # print function - def PrintTree(self): - print(self.data) - -# Creating a root node -root = Node(27) -root.PrintTree() \ No newline at end of file + def __init__(self, key): + self.left = None + self.right = None + self.val = key + + +# A utility function to insert +# a new node with the given key +def insert(root, key): + if root is None: + return Node(key) + else: + if root.val == key: + return root + elif root.val < key: + root.right = insert(root.right, key) + else: + root.left = insert(root.left, key) + return root + + +# A utility function to do inorder tree traversal +def inorder(root): + if root: + inorder(root.left) + print(root.val, end =" ") + inorder(root.right) + + +# Driver program to test the above functions +if __name__ == '__main__': + + # Let us create the following BST + # 50 + # / \ + # 30 70 + # / \ / \ + # 20 40 60 80 + + r = Node(50) + r = insert(r, 30) + r = insert(r, 20) + r = insert(r, 40) + r = insert(r, 70) + r = insert(r, 60) + r = insert(r, 80) + + # Print inoder traversal of the BST + inorder(r) diff --git a/binary_search_tree_example.py b/binary_search_tree_example.py new file mode 100644 index 0000000..0bd8728 --- /dev/null +++ b/binary_search_tree_example.py @@ -0,0 +1,80 @@ +# 30-12-22 +# binary search tree +class BinaryTreeNode: + def __init__(self, data): + self.data = data + self.leftChild = None + self.rightChild = None + + +def insert(root, newValue): + # if binary search tree is empty, make a new node and declare it as root + if root is None: + root = BinaryTreeNode(newValue) + return root + # binary search tree is not empty, so we will insert it into the tree + # if newValue is less than value of data in root, add it to left subtree and proceed recursively + if newValue < root.data: + root.leftChild = insert(root.leftChild, newValue) + else: + # if newValue is greater than value of data in root, add it to right subtree and proceed recursively + root.rightChild = insert(root.rightChild, newValue) + return root + + +root = insert(None, 15) +insert(root, 10) +insert(root, 25) +insert(root, 6) +insert(root, 14) +insert(root, 20) +insert(root, 60) +a1 = root +a2 = a1.leftChild +a3 = a1.rightChild +a4 = a2.leftChild +a5 = a2.rightChild +a6 = a3.leftChild +a7 = a3.rightChild +print("Root Node is:") +print(a1.data) +print("left child of node is:") +print(a1.leftChild.data) +print("right child of node is:") +print(a1.rightChild.data) +print("Node is:") +print(a2.data) +print("left child of node is:") +print(a2.leftChild.data) +print("right child of node is:") +print(a2.rightChild.data) +print("Node is:") +print(a3.data) +print("left child of node is:") +print(a3.leftChild.data) +print("right child of node is:") +print(a3.rightChild.data) +print("Node is:") +print(a4.data) +print("left child of node is:") +print(a4.leftChild) +print("right child of node is:") +print(a4.rightChild) +print("Node is:") +print(a5.data) +print("left child of node is:") +print(a5.leftChild) +print("right child of node is:") +print(a5.rightChild) +print("Node is:") +print(a6.data) +print("left child of node is:") +print(a6.leftChild) +print("right child of node is:") +print(a6.rightChild) +print("Node is:") +print(a7.data) +print("left child of node is:") +print(a7.leftChild) +print("right child of node is:") +print(a7.rightChild) \ No newline at end of file diff --git a/binary_search_tree_using_deletion.py b/binary_search_tree_using_deletion.py new file mode 100644 index 0000000..7a5b888 --- /dev/null +++ b/binary_search_tree_using_deletion.py @@ -0,0 +1,130 @@ +# 31-12-22 +# binary search tree deletion +## Implement binary search tree deletion function +class Node(object): + # Initializing to None + def __init__(self): + self.left = None + self.right = None + self.data = None + + +# Inserting a node in Binary search tree +def insertion(val): + # Condition if this is a first node then it will be considered as a root + if (root.data == None): + print(val, " Inserted as root") + root.data = val + # Else part will be executed for all the other insertions + else: + # Pointer to move around tree to search for a place of new node + p = root + + # Creating a new node and writing a value in the data part + n = Node() + n.data = val + + # Iterating to search for an appropriate place of new node + while (1): + # if val is less than the current node p indicates that new node will be inserted on left subtree + if (val < p.data): + if (p.left == None): + print(val, " Inserted on left of ", p.data) + p.left = n + break + else: + p = p.left + # if val is greater than the current node p indicates that new node will be inserted on right subtree + else: + if (p.right == None): + print(val, " Inserted on right of", p.data) + p.right = n + break + else: + p = p.right + + +def inorder(node): + if node: + # Traversing left subtree + inorder(node.left) + # Visiting node + print(node.data) + # Traversing right subtree + inorder(node.right) + + +def deletion(value): + # If tree is empty + if (root == None): + print("Tree is empty") + + # Initializing the pointers to traverse a tree + p = root + q = root + + # Searching for a node to be deleted + while (1): + if (value > p.data): + q = p + p = p.right + elif (value < p.data): + q = p + p = p.left + else: + break + + # If the desired node is a leaf node this condition will be true + if (p.left == None and p.right == None): + if (q.left == p): + q.left = None + else: + q.right = None + print("Value deleted from leaf") + return p.data + + # deletion of node with one child on right + if (p.left == None and p.right != None): + print("\ndeleting node having only right subtree") + if (q.left == p): + q.left = p.right + else: + q.right = p.right + return p.data + + # Deletion of node with one child on left + if (p.left != None and p.right == None): + print("\ndeleting node having only left subtree") + if (q.left == p): + q.left = p.left + else: + q.right = p.left + return p.data + + # deletion of node having 2 childs + if (p.left != None and p.right != None): + temp = p + temp1 = p.right + while (temp1.left != None): + temp = temp1 + temp1 = temp1.left + if (temp1 == temp.left): + print("\ndeleting node having two childs & right subtree") + temp.left = temp1.right + else: + print("\ndeleting node having two childs & Only right node ") + temp.right = temp1.right + p.data = temp1.data + return value + + +root = Node() +insertion(10) +insertion(5) +insertion(20) +insertion(30) +insertion(25) +insertion(2) + +print("Value ", deletion(25), "deleted successfully") +print("Value ", deletion(10), "deleted successfully") \ No newline at end of file diff --git a/binary_search_tree_using_queue.py b/binary_search_tree_using_queue.py new file mode 100644 index 0000000..855363e --- /dev/null +++ b/binary_search_tree_using_queue.py @@ -0,0 +1,47 @@ +# 2-01-23 +# binary search tree using queue +from queue import Queue + + +class BinaryTreeNode: + def __init__(self, data): + self.data = data + self.leftChild = None + self.rightChild = None + + +def insert(root, newValue): + # if binary search tree is empty, make a new node and declare it as root + if root is None: + root = BinaryTreeNode(newValue) + return root + # binary search tree is not empty, so we will insert it into the tree + # if newValue is less than value of data in root, add it to left subtree and proceed recursively + if newValue < root.data: + root.leftChild = insert(root.leftChild, newValue) + else: + # if newValue is greater than value of data in root, add it to right subtree and proceed recursively + root.rightChild = insert(root.rightChild, newValue) + return root + + +def deleteTree(root): + if root: + # delete left subtree + deleteTree(root.leftChild) + # delete right subtree + deleteTree(root.rightChild) + # traverse root + print("Deleting Node:", root.data) + del root + + +root = insert(None, 15) +insert(root, 10) +insert(root, 25) +insert(root, 6) +insert(root, 14) +insert(root, 20) +insert(root, 60) +print("deleting all the elements of the binary tree.") +deleteTree(root) \ No newline at end of file diff --git a/binary_tree_example.py b/binary_tree_example.py new file mode 100644 index 0000000..d6b1112 --- /dev/null +++ b/binary_tree_example.py @@ -0,0 +1,86 @@ +# 29-12-22 +# binary tree example +class BinaryTreeNode: + def __init__(self, data): + self.data = data + self.leftChild = None + self.rightChild = None + + +a1 = BinaryTreeNode(10) +a2 = BinaryTreeNode(15) +a3 = BinaryTreeNode(20) +a4 = BinaryTreeNode(60) +a5 = BinaryTreeNode(14) +a6 = BinaryTreeNode(25) +a7 = BinaryTreeNode(6) + +a1.leftChild = a2 +a1.rightChild = a3b +a2.leftChild = a4 +a2.rightChild = a5 +a3.leftChild = a6 +a3.rightChild = a7 + +print("Root Node is:") +print(a1.data) + +print("left child of node is:") +print(a1.leftChild.data) + +print("right child of node is:") +print(a1.rightChild.data) + +print("Node is:") +print(a2.data) + +print("left child of node is:") +print(a2.leftChild.data) + +print("right child of node is:") +print(a2.rightChild.data) + +print("Node is:") +print(a3.data) + +print("left child of node is:") +print(a3.leftChild.data) + +print("right child of node is:") +print(a3.rightChild.data) + +print("Node is:") +print(a4.data) + +print("left child of node is:") +print(a4.leftChild) + +print("right child of node is:") +print(a4.rightChild) + +print("Node is:") +print(a5.data) + +print("left child of node is:") +print(a5.leftChild) + +print("right child of node is:") +print(a5.rightChild) + +print("Node is:") +print(a6.data) + +print("left child of node is:") +print(a6.leftChild) + +print("right child of node is:") +print(a6.rightChild) + +print("Node is:") +print(a7.data) + +print("left child of node is:") +print(a7.leftChild) + +print("right child of node is:") +print(a7.rightChild) \ No newline at end of file diff --git a/binary_tree_iterative_method.py b/binary_tree_iterative_method.py new file mode 100644 index 0000000..597f2e6 --- /dev/null +++ b/binary_tree_iterative_method.py @@ -0,0 +1,43 @@ +# 29-12-22 +# binary tree iterative method +class Node: + def __init__(self, data): + self.left = None + self.right = None + self.data = data + + def PrintTree ( self ) : + if self.left : + self.left.PrintTree () + print ( self.data, end= ' ' ) , + if self.right : + self.right.PrintTree () + +class Solution: + ''' + Function to invert the tree + ''' + def invertTree(self, root): + if root == None: + return + root.left, root.right = self.invertTree(root.right),self.invertTree(root.left) + return root + +if __name__ == '__main__': + ''' + 10 10 + / \ / \ + 20 30 ========>> 30 20 + / \ / \ + 40 50 50 40 + ''' + Tree = Node(10) + Tree.left = Node(20) + Tree.right = Node(30) + Tree.left.left = Node(40) + Tree.right.right = Node(50) + print('Initial Tree :',end = ' ' ) + Tree.PrintTree() + Solution().invertTree(root=Tree) + print('\nInverted Tree :', end=' ') + Tree.PrintTree() \ No newline at end of file diff --git a/break_statement_in_a_for_loop.py b/break_statement_in_a_for_loop.py new file mode 100644 index 0000000..a4dc357 --- /dev/null +++ b/break_statement_in_a_for_loop.py @@ -0,0 +1,13 @@ +# 7-1-23 +# print first 5 multiples of any number +# program to find first 5 multiples of 6 + +i = 1 + +while i <= 10: + print('10 * ', i, '=', 10 * i) + + if i >= 5: + break + + i = i + 1 \ No newline at end of file diff --git a/break_statement_using_for_loop_ex1.py b/break_statement_using_for_loop_ex1.py new file mode 100644 index 0000000..8fcc199 --- /dev/null +++ b/break_statement_using_for_loop_ex1.py @@ -0,0 +1,9 @@ +# 7-2-23 +for i in range(1, 11): + print('Multiplication table of', i) + for j in range(1, 11): + # condition to break inner loop + if i > 5 and j > 5: + break + print(i * j, end=' ') + print('') diff --git a/break_while_loop.py b/break_while_loop.py new file mode 100644 index 0000000..91f5adf --- /dev/null +++ b/break_while_loop.py @@ -0,0 +1,8 @@ +# 8-2-23 +# break while loop +i = 1 +while i <= 100: + print(i) + if i == 15: + break + i += 1 diff --git a/continue_statement_in_for_loop.py b/continue_statement_in_for_loop.py new file mode 100644 index 0000000..b88b52b --- /dev/null +++ b/continue_statement_in_for_loop.py @@ -0,0 +1,13 @@ +# 10-2-23 +# remove any number in the range of 1 to 10 +for i in range(1, 11): + + # If i is equals to 6, + # continue to next iteration + # without printing + if i == 6: + continue + else: + # otherwise print the value + # of i + print(i, end=" ") diff --git a/continue_statement_in_for_loop_example.py b/continue_statement_in_for_loop_example.py new file mode 100644 index 0000000..7ec78e2 --- /dev/null +++ b/continue_statement_in_for_loop_example.py @@ -0,0 +1,8 @@ +# 10-2-23 +# to print any number of multiplication +for number in range(1, 51): + if (number % 7) != 0: + # the current number is not a multiple of 7, so continue until the next number + continue + + print(f'{number} is a multiple of 7') diff --git a/dictionary_.py b/dictionary_.py new file mode 100644 index 0000000..7e43fcf --- /dev/null +++ b/dictionary_.py @@ -0,0 +1,5 @@ +# 16-1-23 +# dictionary +dict = {'Name': 'Jalpa', 'Age': 18, 'Class': 'MCA'} +print("dict['Name']: ", dict['Name']) +print("dict['Age']: ", dict['Age']) diff --git a/dictionary_example.py b/dictionary_example.py new file mode 100644 index 0000000..c71a081 --- /dev/null +++ b/dictionary_example.py @@ -0,0 +1,13 @@ +# 16-1-23 +# dictionary example +Employee = {"Name": "David", "Age": 30, "salary":55000,"Company":"GOOGLE"} +print(type(Employee)) +print("printing Employee data .... ") +print(Employee) +print("Enter the details of the new employee....") +Employee["Name"] = input("Name: ") +Employee["Age"] = int(input("Age: ")) +Employee["salary"] = int(input("Salary: ")) +Employee["Company"] = input("Company:") +print("printing the new data") +print(Employee) diff --git a/do_while_loop.py b/do_while_loop.py new file mode 100644 index 0000000..590393f --- /dev/null +++ b/do_while_loop.py @@ -0,0 +1,9 @@ +# 4-2-23 +# to print any 5 numbers using do while loop +i = 1 + +while True: + print(i) + i = i + 1 + if (i > 5): + break diff --git a/for_loop.py b/for_loop.py new file mode 100644 index 0000000..40cd334 --- /dev/null +++ b/for_loop.py @@ -0,0 +1,7 @@ +# 30-1-23 +# for loop +forecast = {'Mon': 'Rainy', 'Tues': 'Partly Cloudy', 'Wed': 'Sunny', 'Thu': 'Windy', 'Fri': 'Warm', 'Sat': 'Hot', + 'Sun': 'Clear'} + +for day, weather in forecast.items(): + print(day + ' will be ' + weather) \ No newline at end of file diff --git a/for_loop_ex1.py b/for_loop_ex1.py new file mode 100644 index 0000000..f6c1729 --- /dev/null +++ b/for_loop_ex1.py @@ -0,0 +1,13 @@ +# 27-1-23 +# sum of list of elements using for loop +# creating the list of numbers +numbers = [3, 5, 23, 6, 5, 1, 2, 9, 8] + +# initializing a variable that will store the sum +sum = 0 + +# using for loop to iterate over the list +for num in numbers: + sum = sum + num ** 2 + +print("The sum of squares is: ", sum) diff --git a/for_loop_example.py b/for_loop_example.py new file mode 100644 index 0000000..30ceec5 --- /dev/null +++ b/for_loop_example.py @@ -0,0 +1,8 @@ +# 27-8-23 +# for loop example +digits = [0, 1, 5] + +for i in digits: + print(i) +else: + print("No items left.") \ No newline at end of file diff --git a/if.py b/if.py new file mode 100644 index 0000000..5c6e89b --- /dev/null +++ b/if.py @@ -0,0 +1,9 @@ +# 19-1-23 +# if__else_example1 +x = 5 +y = 3 + +if x < y: + print("x is smaller than y.") +else: + print("x is greater than y.") diff --git a/if__elif__example1.py b/if__elif__example1.py new file mode 100644 index 0000000..5977d03 --- /dev/null +++ b/if__elif__example1.py @@ -0,0 +1,11 @@ +# 20-1-23 +# if__elif__example1 +number = int(input("Enter the number?")) +if number == 10: + print("number is equals to 10") +elif number == 50: + print("number is equal to 50") +elif number == 100: + print("number is equal to 100") +else: + print("number is not equal to 10, 50 or 100") diff --git a/if__else.py b/if__else.py new file mode 100644 index 0000000..9d70845 --- /dev/null +++ b/if__else.py @@ -0,0 +1,6 @@ +# 18-1-23 +age = int(input("Enter your age? ")) +if age >= 18: + print("You are eligible to vote !!") +else: + print("Sorry! you are not eligible for vote!") diff --git a/if__else__example.py b/if__else__example.py new file mode 100644 index 0000000..145f092 --- /dev/null +++ b/if__else__example.py @@ -0,0 +1,11 @@ +# 19-1-2022 +# if__else__example +a = 5 +b = 6 + +if a > b: + print("First number is the largest") +elif a < b: + print("Second number is the largest") +else: + print("a and b are equal ") diff --git a/if_else_if_example.py b/if_else_if_example.py new file mode 100644 index 0000000..ff6d6cf --- /dev/null +++ b/if_else_if_example.py @@ -0,0 +1,10 @@ +# 20-1-2023 +# if__else__if__example +number = 10 + +if number > 0: + print(" positive numbers") +elif number == 0: + print(" zeros") +else: + Print("Negative numbers") diff --git a/if_else_if_program.py b/if_else_if_program.py new file mode 100644 index 0000000..7b2ec35 --- /dev/null +++ b/if_else_if_program.py @@ -0,0 +1,15 @@ +# 23-01-22 +# if__else__if +x = 10 +y = 15 + +if x == 10: + print("x equals 10.") + if y > 20: + print("y is greater than 20.") + elif y < 20: + print("y is less than 20.") + else: + print("y equals 20.") +else: + print("x does not match.") diff --git a/inorder_traversal_in_tree.py b/inorder_traversal_in_tree.py new file mode 100644 index 0000000..c152070 --- /dev/null +++ b/inorder_traversal_in_tree.py @@ -0,0 +1,11 @@ +# 28-12-22 +# inorder traversal in tree +class BTNode: + def __init__(self, val): + self.left = None + self.right = None + self.val = val + def rootNode(self): + print(self.val) +root = BTNode('A') +root.rootNode() \ No newline at end of file diff --git a/linear_search.py b/linear_search.py index a18e454..7529d3e 100644 --- a/linear_search.py +++ b/linear_search.py @@ -1,5 +1,5 @@ -# 11-12-2022 -# linear search +# 3-01-22 +# linear_search def linear_Search(list1, n, key): # Searching list1 sequentially for i in range(0, n): @@ -8,12 +8,12 @@ def linear_Search(list1, n, key): return -1 -list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] -key = 10 +list1 = [1, 3, 5, 4, 7, 9] +key = 7 n = len(list1) res = linear_Search(list1, n, key) if (res == -1): print("Element not found") else: - print("Element found at index: ", res) \ No newline at end of file + print("Element found at index: ", res) \ No newline at end of file diff --git a/linear_search_ex1.py b/linear_search_ex1.py new file mode 100644 index 0000000..e48715a --- /dev/null +++ b/linear_search_ex1.py @@ -0,0 +1,10 @@ +# 3-1-23 +# binary search example +def linearsearch(arr, x): + for i in range(len(arr)): + if arr[i] == x: + return i + return -1 +arr = ['p','y','t','h','o','n','3'] +x = '3' +print("element found at index "+str(linearsearch(arr,x))) \ No newline at end of file diff --git a/linear_search_tree.py b/linear_search_tree.py new file mode 100644 index 0000000..da23f8a --- /dev/null +++ b/linear_search_tree.py @@ -0,0 +1,21 @@ +# 2-01-23 +# Linear Search in Python + + +def linearSearch(array, n, x): + + # Going through array sequencially + for i in range(0, n): + if (array[i] == x): + return i + return -1 + + +array = [2, 4, 0, 1, 9] +x = 1 +n = len(array) +result = linearSearch(array, n, x) +if(result == -1): + print("Element not found") +else: + print("Element found at index: ", result) \ No newline at end of file diff --git a/linear_search_using_array.py b/linear_search_using_array.py index c9ce5df..381c4d3 100644 --- a/linear_search_using_array.py +++ b/linear_search_using_array.py @@ -1,22 +1,20 @@ -# 11-12-22 -# linear search using array -# Linear Search in Python +# 4-1-2023 +# linear search array +def linear_search(arr, a, b): - -def linearSearch(array, n, x): - - # array using sequencially - for i in range(0, n): - if (array[i] == x): + # Going through array + for i in range(0, a): + if (arr[i] == b): return i return -1 - -array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] -x = 100 -n = len(array) -result = linearSearch(array, n, x) -if(result == -1): - print("Element not found") +arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] +print("The array given is ", arr) +b = 80 +print("Element to be found is ", b) +a = len(arr) +index = linear_search(arr, a, b) +if(index == -1): + print("Element is not in the list") else: - print("Element found at index: ", result) \ No newline at end of file + print("Index of the element is: ", index) \ No newline at end of file diff --git a/linear_search_using_list.py b/linear_search_using_list.py new file mode 100644 index 0000000..8c56108 --- /dev/null +++ b/linear_search_using_list.py @@ -0,0 +1,20 @@ +# 4-1-23 +# linear search using list +def linear_search(list, key): + """Return index of key in alist. Return -1 if key not present.""" + for i in range(len(list)): + if list[i] == key: + return i + return -1 + + +list = input('Enter the list of numbers: ') +list = list.split() +list = [int(x) for x in list] +key = int(input('The number to search for: ')) + +index = linear_search(list, key) +if index < 0: + print('{} was not found.'.format(key)) +else: + print('{} was found at index {}.'.format(key, index)) \ No newline at end of file diff --git a/list_example.py b/list_example.py new file mode 100644 index 0000000..8514e66 --- /dev/null +++ b/list_example.py @@ -0,0 +1,9 @@ +# 5-1-22 +#!/usr/bin/python + +list = ['Python', 'Machine Learning', 1997, 7886]; +print ("Value available at index 0") +print (list[0]) +list[2] = 7886; +print ("New value available at index 1 ") +print (list[1]) \ No newline at end of file diff --git a/list_example1.py b/list_example1.py new file mode 100644 index 0000000..e7e231d --- /dev/null +++ b/list_example1.py @@ -0,0 +1,13 @@ +# 7-1-22 +# list example +# Taking one variable and integer +a = 20 +# creating an empty list +lst = [] +# number of elements as input +n = int(input("Enter number of elements : ")) +# iterating till the range +for i in range(0, n): + ele = int(input()) + lst.append(ele) # adding the element +print(lst) diff --git a/list_using_slicing.py b/list_using_slicing.py new file mode 100644 index 0000000..f1c0b8d --- /dev/null +++ b/list_using_slicing.py @@ -0,0 +1,13 @@ +# 6-01-23 +# list using slicing +list = [1, 2, 3, 4, 5, 6, 7] +print(list[0]) +print(list[1]) +print(list[2]) +print(list[3]) +# Slicing the elements +print(list[0:6]) +# By default, the index value is 0 so its starts from the 0th element and go for index -1. +print(list[:]) +print(list[2:5]) +print(list[1:6:2]) diff --git a/merge_dictionary_ex1.py b/merge_dictionary_ex1.py new file mode 100644 index 0000000..9b4029f --- /dev/null +++ b/merge_dictionary_ex1.py @@ -0,0 +1,6 @@ +# 17-1-23 +# merge dictionary +D1 = {'name': 'Jay'} +D2 = {'job': 'Devops engineer', 'age': 25} +D1.update(D2) +print(D1) diff --git a/merge_two_dictionaries.py b/merge_two_dictionaries.py new file mode 100644 index 0000000..789d27e --- /dev/null +++ b/merge_two_dictionaries.py @@ -0,0 +1,11 @@ +# 17-1-23 +# merge two dictionaries + +def merge(dict1, dict2): + result = dict1 | dict2 # merge operator (|) + return result + + +dict1 = {'A': 'Jay ', 'B': 'Messi ', 'C': 'Ronaldo'} +dict2 = {'D': 'Hardik Pandiya ', 'E': 'Rishabh pant', 'F': 'Kohli'} +print(merge(dict1, dict2)) # print dict3 diff --git a/nested_.py b/nested_.py new file mode 100644 index 0000000..597a6db --- /dev/null +++ b/nested_.py @@ -0,0 +1 @@ +i \ No newline at end of file diff --git a/nested_if.py b/nested_if.py new file mode 100644 index 0000000..58b70ec --- /dev/null +++ b/nested_if.py @@ -0,0 +1,17 @@ +# 24-1-23 +# nested if +a = 40 +b = 80 +c = 70 + +if a > b: + if a > c: + print("a is greater") + +if b > a: + if b > c: + print("b is greatest") + +if c > a: + if c > b: + print("c is greatest") diff --git a/nested_if_ex1.py b/nested_if_ex1.py new file mode 100644 index 0000000..b2a0c78 --- /dev/null +++ b/nested_if_ex1.py @@ -0,0 +1,18 @@ +# 25-1-23 +# nested if example +number = -5 + +# outer if statement +if number >= 0: + # inner if statement + if number == 0: + print('Number is 0') + + # inner else statement + else: + print('Number is positive') + +# outer else statement +else: + print('Number is negative') + diff --git a/nested_if_example.py b/nested_if_example.py new file mode 100644 index 0000000..1f6a7c0 --- /dev/null +++ b/nested_if_example.py @@ -0,0 +1,20 @@ +# 24-1-23 +# nested if example + +x = int(input("Enter your age: ")) + +y = int(input('Enter your age: ')) + +if x > 19: + + if y > 95: + + print('You are too old, go away!') + + else: + + print('Welcome, you are of the right age!') + +else: + + print('You are too young, go away!') diff --git a/nested_if_example2.py b/nested_if_example2.py new file mode 100644 index 0000000..0a30e91 --- /dev/null +++ b/nested_if_example2.py @@ -0,0 +1,12 @@ +# 25-01-22 +n = 9 +if n % 5 == 0: + if n % 3 == 0: + print("The number is divisible by both 5 and 3") + else: + print("The number is divisible by both 5 and 3") +else: + if n % 3 == 0: + print("The number is divisible by 3 but not 5") + else: + print("The number is not divisible by 5 and not divisible by 3") diff --git a/python_lists.py b/python_lists.py new file mode 100644 index 0000000..a532a52 --- /dev/null +++ b/python_lists.py @@ -0,0 +1,12 @@ +# 6-1-23 +# python list + + +# Creating a List with +# the use of multiple values +List = ["I ", "Love", "My", "India"] + +# accessing a element from the +# list using index number +print("Accessing a element from the list") +print(List[3]) diff --git a/queue using list.py b/queue using list.py index 8263b13..923144d 100644 --- a/queue using list.py +++ b/queue using list.py @@ -2,7 +2,7 @@ # queue using list # create a empty queue queue = [] -# # Adding elements to the queue +# Adding elements to the queue queue.append('b') queue.append('j') queue.append('s') diff --git a/quick_sort_ex1.py b/quick_sort_ex1.py new file mode 100644 index 0000000..4117f8a --- /dev/null +++ b/quick_sort_ex1.py @@ -0,0 +1,36 @@ +# 24-12-22 +# quick sort example1 + +def quick_sort(alist, start, end): + # Sorts the list from indexes start to end - 1 inclusive + if end - start > 1: + p = partition(alist, start, end) + quick_sort(alist, start, p) + quick_sort(alist, p + 1, end) + + +def partition(alist, start, end): + pivot = alist[start] + i = start + 1 + j = end - 1 + + while True: + while (i <= j and alist[i] <= pivot): + i = i + 1 + while (i <= j and alist[j] >= pivot): + j = j - 1 + + if i <= j: + alist[i], alist[j] = alist[j], alist[i] + else: + alist[start], alist[j] = alist[j], alist[start] + return j + + +# input list +alist = [1, 74, 96, 5, 42, 63] +print('Input List\n', alist) + +# sort list +quick_sort(alist, 0, len(alist)) +print('Sorted List\n', alist) \ No newline at end of file diff --git a/reverse_while_loop.py b/reverse_while_loop.py new file mode 100644 index 0000000..bde1efe --- /dev/null +++ b/reverse_while_loop.py @@ -0,0 +1,6 @@ +# 2-2-23 +# to print numbers in reverse order +i = 10 +while i >= 0: + print(i, end=' ') + i = i - 1 diff --git a/set.py b/set.py new file mode 100644 index 0000000..d3d23b6 --- /dev/null +++ b/set.py @@ -0,0 +1,23 @@ +# 10-1-23 +# set +# A Python program to +# demonstrate adding elements + + +# Creating a Set +people = {"Jay", "Jalpa", "Anjali"} + +print("People:", end=" ") +print(people) + +# This will add Suman +# in the set +people.add("Suman") + +# Adding elements to the +# set using iterator +for i in range(1, 6): + people.add(i) + +print("\nSet after adding element:", end=" ") +print(people) diff --git a/set1.py b/set1.py new file mode 100644 index 0000000..97378f6 --- /dev/null +++ b/set1.py @@ -0,0 +1,8 @@ +# 11-1-23 +# set another program +Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} +print(Days) +print(type(Days)) +print("Looping through the set elements ... ") +for i in Days: + print(i) diff --git a/set_ex1.py b/set_ex1.py new file mode 100644 index 0000000..618aade --- /dev/null +++ b/set_ex1.py @@ -0,0 +1,10 @@ +# 11-01-22 +# set example 1 +months = {"January", "February", "March", "April", "May", "June"} +print("\nprinting the original set ... ") +print(months) +print("\nRemoving some months from the set...") +months.remove("January") +months.remove("May") +print("\nPrinting the modified set...") +print(months) diff --git a/set_example.py b/set_example.py new file mode 100644 index 0000000..3bd31d5 --- /dev/null +++ b/set_example.py @@ -0,0 +1,28 @@ +# 10-1-23 +# set programs +# Python program to +# demonstrate intersection +# of two sets + +set1 = set() +set2 = set() + +for i in range(5): + set1.add(i) + +for i in range(3, 9): + set2.add(i) + +# Intersection using +# intersection() function +set3 = set1.intersection(set2) + +print("Intersection using intersection() function") +print(set3) + +# Intersection using +# "&" operator +set3 = set1 & set2 + +print("\nIntersection using '&' operator") +print(set3) diff --git a/shell.py b/shell.py new file mode 100644 index 0000000..f40a71f --- /dev/null +++ b/shell.py @@ -0,0 +1 @@ +sort \ No newline at end of file diff --git a/shell_sort.py b/shell_sort.py new file mode 100644 index 0000000..2b64eb7 --- /dev/null +++ b/shell_sort.py @@ -0,0 +1,22 @@ +# 25-12-2022 +def shellSort(array, n): + + # Rearrange elements at each n/2, n/4, n/8, ... intervals + interval = n // 2 + while interval > 0: + for i in range(interval, n): + temp = array[i] + j = i + while j >= interval and array[j - interval] > temp: + array[j] = array[j - interval] + j -= interval + + array[j] = temp + interval //= 2 + + +data = [9, 8, 3, 7, 5, 6, 4, 1] +size = len(data) +shellSort(data, size) +print('Sorted Array in Ascending Order:') +print(data) \ No newline at end of file diff --git a/shell_sort_ex1.py b/shell_sort_ex1.py new file mode 100644 index 0000000..710f171 --- /dev/null +++ b/shell_sort_ex1.py @@ -0,0 +1,24 @@ +# 27-12-22 +# shell sort example 1 +from math import floor + + +def shellSort(arr): + n = len(arr) + # Gap sequence + gap = floor(n / 2) + while gap > 0: + for i in range(gap, n): + temp = arr[i] + j = i + # Compare elements at equal gap. + while j >= gap and temp < arr[j - gap]: + arr[j] = arr[j - gap] + j -= gap + arr[j] = temp + gap = floor(gap / 2) + + +arr = [3, 2, 1, 4, 6, 9, 5, 10, 0] +shellSort(arr) +print(arr) diff --git a/shell_sort_example.py b/shell_sort_example.py new file mode 100644 index 0000000..c89ec31 --- /dev/null +++ b/shell_sort_example.py @@ -0,0 +1,37 @@ +# 25-12-22 +# shell-sort example + + +def shellSort(arr, n): + # code here + gap = n // 2 + + while gap > 0: + j = gap + # Check the array in from left to right + # Till the last possible index of j + while j < n: + i = j - gap # This will keep help in maintain gap value + + while i >= 0: + # If value on right side is already greater than left side value + # We don't do swap else we swap + if arr[i + gap] > arr[i]: + + break + else: + arr[i + gap], arr[i] = arr[i], arr[i + gap] + + i = i - gap # To check left side also + # If the element present is greater than current element + j += 1 + gap = gap // 2 + + +# driver to check the code +arr2 = [12, 34, 54, 2, 3] +print("input array:", arr2) + +shellSort(arr2, len(arr2)) +print("sorted array", arr2) + diff --git a/shell_sort_using_array.py b/shell_sort_using_array.py new file mode 100644 index 0000000..a3ec779 --- /dev/null +++ b/shell_sort_using_array.py @@ -0,0 +1,22 @@ +# 26-12-22 +def shell_sort(array, n): + h = n // 2 + while h > 0: + for i in range(h, n): + t = array[i] + j = i + while j >= h and array[j - h] > t: + array[j] = array[j - h] + j -= h + + array[j] = t + h = h // 2 + + +array = [34, 12, 20, 7, 13, 15, 2, 23] +n = len(array) +print('Array before Sorting:') +print(array) +shell_sort(array, n) +print('Array after Sorting:') +print(array) \ No newline at end of file diff --git a/shell_sort_using_list.py b/shell_sort_using_list.py new file mode 100644 index 0000000..cdd7432 --- /dev/null +++ b/shell_sort_using_list.py @@ -0,0 +1,18 @@ +# 26-12-22 +# shell sort list +def ShellSort(array): + inter = len(array) // 2 + while inter >= 1: + i = 0 + while i < inter: + j = i + inter + while j < len(array): + if array[j] < array[j - inter]: + array[j], array[j - inter] = array[j - inter], array[j] + j = j + inter + i = i + 1 + inter = inter // 2 + +array1 = [8, 9, 2, 0, 7, 1, 6, 4, 3, 5] +ShellSort(array1) +print(array1) \ No newline at end of file diff --git a/slicing_in_a_list.py b/slicing_in_a_list.py new file mode 100644 index 0000000..cf99c17 --- /dev/null +++ b/slicing_in_a_list.py @@ -0,0 +1,14 @@ +# 5-01-23 +# slicing using a list +# List slicing in Python + +my_list = ['I', 'Love', 'Python', 'Programming', 'Language'] + +# items from index 2 to index 3 +print(my_list[2:4]) + +# items from index 4 to end +print(my_list[4:]) + +# items beginning to end +print(my_list[:]) \ No newline at end of file diff --git a/swap_elements_using_list.py b/swap_elements_using_list.py new file mode 100644 index 0000000..a98f83f --- /dev/null +++ b/swap_elements_using_list.py @@ -0,0 +1,13 @@ +# 11-2-23 +# swap elements using list +# Swap function +def swapPositions(list, pos1, pos2): + list[pos1], list[pos2] = list[pos2], list[pos1] + return list + + +# Driver function +List = [23, 65, 19, 90] +pos1, pos2 = 1, 3 + +print(swapPositions(List, pos1 - 1, pos2 - 1)) diff --git a/tree_traversal.py b/tree_traversal.py new file mode 100644 index 0000000..10dbde8 --- /dev/null +++ b/tree_traversal.py @@ -0,0 +1,76 @@ +# 27-12-22 +# tree traversal +class Node: + def __init__(self, item): + self.left = None + self.right = None + self.val = item + + +def inorder(root): + + if root: + # Traverse left + inorder(root.left) + # Traverse root + print(str(root.val) + "->", end='') + # Traverse right + inorder(root.right) + + +def postorder(root): + + if root: + # Traverse left + postorder(root.left) + # Traverse right + postorder(root.right) + # Traverse root + print(str(root.val) + "->", end='') + + +def preorder(root): + + if root: + # Traverse root + print(str(root.val) + "->", end='') + # Traverse left + preorder(root.left) + # Traverse right + preorder(root.right) + + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) + +print("Inorder traversal ") +inorder(root) + +print("\nPreorder traversal ") +preorder(root) + +print("\nPostorder traversal ") +postorder(root) + + + + + + + + + + + + + + + + + + + + diff --git a/tuple_ex.py b/tuple_ex.py new file mode 100644 index 0000000..8c37170 --- /dev/null +++ b/tuple_ex.py @@ -0,0 +1,12 @@ +# 8-1-22 +# tuple example +names = ('Jay', 'Anjali', 'Jalpa', 'Yash') +print(names[0]) # prints 'Jay' +print(names[1]) # prints 'Anjali' +print(names[2]) # prints 'Jalpa' +print(names[3]) # prints 'Yash' + +nums = (1, 2, 3, 4, 5) +print(nums[0]) # prints 1 +print(nums[1]) # prints 2 +print(nums[4]) # prints 5 diff --git a/tuple_ex1.py b/tuple_ex1.py new file mode 100644 index 0000000..d997d8f --- /dev/null +++ b/tuple_ex1.py @@ -0,0 +1,11 @@ +# 9-1-22 +# tuple example +def my_fun(): + name = 'John' + ID = 23567 + Title = 'Software Engineer' + return name, ID, Title + + +employee = my_fun() +print('Employee detail is:', employee) diff --git a/while_loop.py b/while_loop.py new file mode 100644 index 0000000..a41a747 --- /dev/null +++ b/while_loop.py @@ -0,0 +1,11 @@ +# 31-1-2023 +# print any 5 numbers using while loop + +# initialize the variable +i = 1 +n = 5 + +# while loop from i = 1 to 5 +while i <= n: + print(i) + i = i + 1 diff --git a/while_loop_ex.py b/while_loop_ex.py new file mode 100644 index 0000000..4465473 --- /dev/null +++ b/while_loop_ex.py @@ -0,0 +1,8 @@ +# 1-2-23 +# print any 5 variable using while loop +num = 0 +while num <= 10: + num += 1 + if num % 2 != 0: + continue + print(num) diff --git a/while_loop_ex1.py b/while_loop_ex1.py new file mode 100644 index 0000000..adbe737 --- /dev/null +++ b/while_loop_ex1.py @@ -0,0 +1,7 @@ +# 1-2-23 +# to print reverse string +while True: + word = input('Enter a string and I will print it backwards(type q to quit): ') + if word == 'q': + break + print(word[::-1]) \ No newline at end of file diff --git a/while_loop_example.py b/while_loop_example.py new file mode 100644 index 0000000..a455697 --- /dev/null +++ b/while_loop_example.py @@ -0,0 +1,8 @@ +# 31-1-23 +# to print odd number in the range of 1 to 20 while using while loop +i = 0 +while i <= 20: + i += 1 + if i % 2 == 1: + continue + print(i) diff --git a/while_loop_using_else_statement.py b/while_loop_using_else_statement.py new file mode 100644 index 0000000..982aa31 --- /dev/null +++ b/while_loop_using_else_statement.py @@ -0,0 +1,8 @@ +# 2-2-23 +# to check any numbers is less than 10 +value = 0 +while value < 10: + print(value, " is smaller than 10") + value = value + 1 +else: # executes once the condition in the while loop turns false + print(value, " is equal to 10")