From 978729a96def03ff2fe55bae8ef7854e3aeee0cc Mon Sep 17 00:00:00 2001 From: JS Date: Sun, 25 Dec 2022 00:02:52 +0530 Subject: [PATCH 01/31] practice programs --- quick_sort_ex1.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 quick_sort_ex1.py 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 From 7feb56d97166e600c3c86bf016a9d3a13d84b1c8 Mon Sep 17 00:00:00 2001 From: JS Date: Tue, 27 Dec 2022 00:30:22 +0530 Subject: [PATCH 02/31] practice programs --- .idea/shell_sort.py | 22 ++++++++++++++++++++ .idea/shell_sort_using_array.py | 22 ++++++++++++++++++++ .idea/shell_sort_using_list.py | 18 ++++++++++++++++ shell.py | 1 + shell_sort_example.py | 37 +++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 .idea/shell_sort.py create mode 100644 .idea/shell_sort_using_array.py create mode 100644 .idea/shell_sort_using_list.py create mode 100644 shell.py create mode 100644 shell_sort_example.py diff --git a/.idea/shell_sort.py b/.idea/shell_sort.py new file mode 100644 index 0000000..2b64eb7 --- /dev/null +++ b/.idea/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/.idea/shell_sort_using_array.py b/.idea/shell_sort_using_array.py new file mode 100644 index 0000000..a3ec779 --- /dev/null +++ b/.idea/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/.idea/shell_sort_using_list.py b/.idea/shell_sort_using_list.py new file mode 100644 index 0000000..cdd7432 --- /dev/null +++ b/.idea/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/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_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) + From dbaf249962638114db8ef56fe5c3cd86e17ab116 Mon Sep 17 00:00:00 2001 From: JS Date: Wed, 28 Dec 2022 20:10:10 +0530 Subject: [PATCH 03/31] practice program --- .idea/binary_tree.py | 39 +++++++++++++++ .idea/inorder_traversal_in_tree.py | 11 +++++ .idea/shell_sort_ex1.py | 24 ++++++++++ .idea/tree_traversal.py | 76 ++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 .idea/binary_tree.py create mode 100644 .idea/inorder_traversal_in_tree.py create mode 100644 .idea/shell_sort_ex1.py create mode 100644 .idea/tree_traversal.py diff --git a/.idea/binary_tree.py b/.idea/binary_tree.py new file mode 100644 index 0000000..06d4335 --- /dev/null +++ b/.idea/binary_tree.py @@ -0,0 +1,39 @@ +# 28-12-22 +# binary tree +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 + +# Print the tree + def PrintTree(self): + if self.left: + self.left.PrintTree() + print( self.data), + if self.right: + self.right.PrintTree() + +# Use the insert method to add nodes +root = Node(12) +root.insert(6) +root.insert(14) +root.insert(3) +root.PrintTree() diff --git a/.idea/inorder_traversal_in_tree.py b/.idea/inorder_traversal_in_tree.py new file mode 100644 index 0000000..c152070 --- /dev/null +++ b/.idea/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/.idea/shell_sort_ex1.py b/.idea/shell_sort_ex1.py new file mode 100644 index 0000000..710f171 --- /dev/null +++ b/.idea/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/.idea/tree_traversal.py b/.idea/tree_traversal.py new file mode 100644 index 0000000..10dbde8 --- /dev/null +++ b/.idea/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) + + + + + + + + + + + + + + + + + + + + From 0aa9b34781d37db9c7ff555ca223e7affe6f5f8c Mon Sep 17 00:00:00 2001 From: JS Date: Thu, 29 Dec 2022 20:47:45 +0530 Subject: [PATCH 04/31] practice programs --- .idea/binary_tree_example.py | 86 +++++++++++++++++++++++++++ .idea/binary_tree_iterative_method.py | 43 ++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 .idea/binary_tree_example.py create mode 100644 .idea/binary_tree_iterative_method.py diff --git a/.idea/binary_tree_example.py b/.idea/binary_tree_example.py new file mode 100644 index 0000000..8e8c533 --- /dev/null +++ b/.idea/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 = a3 +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/.idea/binary_tree_iterative_method.py b/.idea/binary_tree_iterative_method.py new file mode 100644 index 0000000..597f2e6 --- /dev/null +++ b/.idea/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 From ab45868f9136b17818d6bc91d5b0fdd59c8e0fc2 Mon Sep 17 00:00:00 2001 From: JS Date: Fri, 30 Dec 2022 19:29:26 +0530 Subject: [PATCH 05/31] Practice --- .idea/binary_search_tree.py | 53 +++++++++++++++++++ .idea/binary_search_tree_example.py | 80 +++++++++++++++++++++++++++++ .idea/binary_tree_example.py | 2 +- 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 .idea/binary_search_tree.py create mode 100644 .idea/binary_search_tree_example.py diff --git a/.idea/binary_search_tree.py b/.idea/binary_search_tree.py new file mode 100644 index 0000000..6db2a3c --- /dev/null +++ b/.idea/binary_search_tree.py @@ -0,0 +1,53 @@ +# 30-12-22 +# binary search tree + +class Node: + 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/.idea/binary_search_tree_example.py b/.idea/binary_search_tree_example.py new file mode 100644 index 0000000..0bd8728 --- /dev/null +++ b/.idea/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/.idea/binary_tree_example.py b/.idea/binary_tree_example.py index 8e8c533..d6b1112 100644 --- a/.idea/binary_tree_example.py +++ b/.idea/binary_tree_example.py @@ -16,7 +16,7 @@ def __init__(self, data): a7 = BinaryTreeNode(6) a1.leftChild = a2 -a1.rightChild = a3 +a1.rightChild = a3b a2.leftChild = a4 a2.rightChild = a5 a3.leftChild = a6 From 1f4ed351fdb14341552212ffbe58b98725bfa7a3 Mon Sep 17 00:00:00 2001 From: JS Date: Sat, 31 Dec 2022 21:25:03 +0530 Subject: [PATCH 06/31] practice programs --- .idea/binary.py | 60 ++++++++++ .idea/binary_search_tree_using_deletion.py | 130 +++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 .idea/binary.py create mode 100644 .idea/binary_search_tree_using_deletion.py diff --git a/.idea/binary.py b/.idea/binary.py new file mode 100644 index 0000000..b02822a --- /dev/null +++ b/.idea/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/.idea/binary_search_tree_using_deletion.py b/.idea/binary_search_tree_using_deletion.py new file mode 100644 index 0000000..7a5b888 --- /dev/null +++ b/.idea/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 From 136571d8fe2e9391922883a7f5148b89ae88d65e Mon Sep 17 00:00:00 2001 From: JS Date: Tue, 3 Jan 2023 19:38:23 +0530 Subject: [PATCH 07/31] practice programs --- .idea/binary_search_.py | 1 + .idea/binary_search_tree_using_queue.py | 47 +++++++++++++++++++++++++ .idea/linear_search.py | 19 ++++++++++ .idea/linear_search_ex1.py | 10 ++++++ .idea/linear_search_tree.py | 21 +++++++++++ 5 files changed, 98 insertions(+) create mode 100644 .idea/binary_search_.py create mode 100644 .idea/binary_search_tree_using_queue.py create mode 100644 .idea/linear_search.py create mode 100644 .idea/linear_search_ex1.py create mode 100644 .idea/linear_search_tree.py diff --git a/.idea/binary_search_.py b/.idea/binary_search_.py new file mode 100644 index 0000000..32f64f4 --- /dev/null +++ b/.idea/binary_search_.py @@ -0,0 +1 @@ +t \ No newline at end of file diff --git a/.idea/binary_search_tree_using_queue.py b/.idea/binary_search_tree_using_queue.py new file mode 100644 index 0000000..855363e --- /dev/null +++ b/.idea/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/.idea/linear_search.py b/.idea/linear_search.py new file mode 100644 index 0000000..7529d3e --- /dev/null +++ b/.idea/linear_search.py @@ -0,0 +1,19 @@ +# 3-01-22 +# linear_search +def linear_Search(list1, n, key): + # Searching list1 sequentially + for i in range(0, n): + if (list1[i] == key): + return i + return -1 + + +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 diff --git a/.idea/linear_search_ex1.py b/.idea/linear_search_ex1.py new file mode 100644 index 0000000..e48715a --- /dev/null +++ b/.idea/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/.idea/linear_search_tree.py b/.idea/linear_search_tree.py new file mode 100644 index 0000000..da23f8a --- /dev/null +++ b/.idea/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 From f65e901b2895eaeb92ff5fb98e02ae83c4769b24 Mon Sep 17 00:00:00 2001 From: JS Date: Wed, 4 Jan 2023 23:11:02 +0530 Subject: [PATCH 08/31] practice programs --- .idea/linear_search_using_array.py | 20 ++++++++++++++++++++ .idea/linear_search_using_list.py | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 .idea/linear_search_using_array.py create mode 100644 .idea/linear_search_using_list.py diff --git a/.idea/linear_search_using_array.py b/.idea/linear_search_using_array.py new file mode 100644 index 0000000..381c4d3 --- /dev/null +++ b/.idea/linear_search_using_array.py @@ -0,0 +1,20 @@ +# 4-1-2023 +# linear search array +def linear_search(arr, a, b): + + # Going through array + for i in range(0, a): + if (arr[i] == b): + return i + return -1 + +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("Index of the element is: ", index) \ No newline at end of file diff --git a/.idea/linear_search_using_list.py b/.idea/linear_search_using_list.py new file mode 100644 index 0000000..8c56108 --- /dev/null +++ b/.idea/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 From ec1415bee43f68b8c6d260295039dc6972cdc105 Mon Sep 17 00:00:00 2001 From: JS Date: Thu, 5 Jan 2023 17:33:30 +0530 Subject: [PATCH 09/31] Practice programs --- .idea/list_example.py | 9 +++++++++ .idea/slicing_in_a_list.py | 14 ++++++++++++++ .idea/vcs.xml | 1 + 3 files changed, 24 insertions(+) create mode 100644 .idea/list_example.py create mode 100644 .idea/slicing_in_a_list.py diff --git a/.idea/list_example.py b/.idea/list_example.py new file mode 100644 index 0000000..8514e66 --- /dev/null +++ b/.idea/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/.idea/slicing_in_a_list.py b/.idea/slicing_in_a_list.py new file mode 100644 index 0000000..cf99c17 --- /dev/null +++ b/.idea/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/.idea/vcs.xml b/.idea/vcs.xml index 35eb1dd..8e6b93f 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file From 18de25155eb3a1557fe7d4125b02bdcc146986fd Mon Sep 17 00:00:00 2001 From: JS Date: Fri, 6 Jan 2023 18:56:41 +0530 Subject: [PATCH 10/31] Practice programs --- .idea/list_using_slicing.py | 13 +++++++++++++ .idea/python_lists.py | 12 ++++++++++++ .idea/vcs.xml | 1 - 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 .idea/list_using_slicing.py create mode 100644 .idea/python_lists.py diff --git a/.idea/list_using_slicing.py b/.idea/list_using_slicing.py new file mode 100644 index 0000000..aeee1aa --- /dev/null +++ b/.idea/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]) \ No newline at end of file diff --git a/.idea/python_lists.py b/.idea/python_lists.py new file mode 100644 index 0000000..a532a52 --- /dev/null +++ b/.idea/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/.idea/vcs.xml b/.idea/vcs.xml index 8e6b93f..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,6 +2,5 @@ - \ No newline at end of file From 5cf7977addc1d294d829194c92c6137a46ca636c Mon Sep 17 00:00:00 2001 From: JS Date: Fri, 6 Jan 2023 19:00:14 +0530 Subject: [PATCH 11/31] Practice programs --- .idea/binary_search_tree.py | 53 --------------- .idea/binary_tree.py | 39 ----------- .idea/linear_search.py | 19 ------ .idea/linear_search_using_array.py | 20 ------ .idea/python-programs.iml | 14 ---- .idea/binary.py => binary.py | 0 binary_Tree.py | 50 +++++++++----- .idea/binary_search_.py => binary_search_.py | 0 binary_search_tree.py | 65 +++++++++++++++---- ...xample.py => binary_search_tree_example.py | 0 ...py => binary_search_tree_using_deletion.py | 0 ...ue.py => binary_search_tree_using_queue.py | 0 ..._tree_example.py => binary_tree_example.py | 0 ...thod.py => binary_tree_iterative_method.py | 0 ...in_tree.py => inorder_traversal_in_tree.py | 0 linear_search.py | 10 +-- ...near_search_ex1.py => linear_search_ex1.py | 0 ...ar_search_tree.py => linear_search_tree.py | 0 linear_search_using_array.py | 32 +++++---- ...ing_list.py => linear_search_using_list.py | 0 .idea/list_example.py => list_example.py | 0 ..._using_slicing.py => list_using_slicing.py | 0 .idea/python_lists.py => python_lists.py | 0 .idea/shell_sort.py => shell_sort.py | 0 .idea/shell_sort_ex1.py => shell_sort_ex1.py | 0 ...sing_array.py => shell_sort_using_array.py | 0 ..._using_list.py => shell_sort_using_list.py | 0 ...icing_in_a_list.py => slicing_in_a_list.py | 0 .idea/tree_traversal.py => tree_traversal.py | 0 29 files changed, 106 insertions(+), 196 deletions(-) delete mode 100644 .idea/binary_search_tree.py delete mode 100644 .idea/binary_tree.py delete mode 100644 .idea/linear_search.py delete mode 100644 .idea/linear_search_using_array.py delete mode 100644 .idea/python-programs.iml rename .idea/binary.py => binary.py (100%) rename .idea/binary_search_.py => binary_search_.py (100%) rename .idea/binary_search_tree_example.py => binary_search_tree_example.py (100%) rename .idea/binary_search_tree_using_deletion.py => binary_search_tree_using_deletion.py (100%) rename .idea/binary_search_tree_using_queue.py => binary_search_tree_using_queue.py (100%) rename .idea/binary_tree_example.py => binary_tree_example.py (100%) rename .idea/binary_tree_iterative_method.py => binary_tree_iterative_method.py (100%) rename .idea/inorder_traversal_in_tree.py => inorder_traversal_in_tree.py (100%) rename .idea/linear_search_ex1.py => linear_search_ex1.py (100%) rename .idea/linear_search_tree.py => linear_search_tree.py (100%) rename .idea/linear_search_using_list.py => linear_search_using_list.py (100%) rename .idea/list_example.py => list_example.py (100%) rename .idea/list_using_slicing.py => list_using_slicing.py (100%) rename .idea/python_lists.py => python_lists.py (100%) rename .idea/shell_sort.py => shell_sort.py (100%) rename .idea/shell_sort_ex1.py => shell_sort_ex1.py (100%) rename .idea/shell_sort_using_array.py => shell_sort_using_array.py (100%) rename .idea/shell_sort_using_list.py => shell_sort_using_list.py (100%) rename .idea/slicing_in_a_list.py => slicing_in_a_list.py (100%) rename .idea/tree_traversal.py => tree_traversal.py (100%) diff --git a/.idea/binary_search_tree.py b/.idea/binary_search_tree.py deleted file mode 100644 index 6db2a3c..0000000 --- a/.idea/binary_search_tree.py +++ /dev/null @@ -1,53 +0,0 @@ -# 30-12-22 -# binary search tree - -class Node: - 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/.idea/binary_tree.py b/.idea/binary_tree.py deleted file mode 100644 index 06d4335..0000000 --- a/.idea/binary_tree.py +++ /dev/null @@ -1,39 +0,0 @@ -# 28-12-22 -# binary tree -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 - -# Print the tree - def PrintTree(self): - if self.left: - self.left.PrintTree() - print( self.data), - if self.right: - self.right.PrintTree() - -# Use the insert method to add nodes -root = Node(12) -root.insert(6) -root.insert(14) -root.insert(3) -root.PrintTree() diff --git a/.idea/linear_search.py b/.idea/linear_search.py deleted file mode 100644 index 7529d3e..0000000 --- a/.idea/linear_search.py +++ /dev/null @@ -1,19 +0,0 @@ -# 3-01-22 -# linear_search -def linear_Search(list1, n, key): - # Searching list1 sequentially - for i in range(0, n): - if (list1[i] == key): - return i - return -1 - - -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 diff --git a/.idea/linear_search_using_array.py b/.idea/linear_search_using_array.py deleted file mode 100644 index 381c4d3..0000000 --- a/.idea/linear_search_using_array.py +++ /dev/null @@ -1,20 +0,0 @@ -# 4-1-2023 -# linear search array -def linear_search(arr, a, b): - - # Going through array - for i in range(0, a): - if (arr[i] == b): - return i - return -1 - -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("Index of the element is: ", index) \ No newline at end of file diff --git a/.idea/python-programs.iml b/.idea/python-programs.iml deleted file mode 100644 index 8b5717a..0000000 --- a/.idea/python-programs.iml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/binary.py b/binary.py similarity index 100% rename from .idea/binary.py rename to binary.py 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/.idea/binary_search_.py b/binary_search_.py similarity index 100% rename from .idea/binary_search_.py rename to binary_search_.py 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/.idea/binary_search_tree_example.py b/binary_search_tree_example.py similarity index 100% rename from .idea/binary_search_tree_example.py rename to binary_search_tree_example.py diff --git a/.idea/binary_search_tree_using_deletion.py b/binary_search_tree_using_deletion.py similarity index 100% rename from .idea/binary_search_tree_using_deletion.py rename to binary_search_tree_using_deletion.py diff --git a/.idea/binary_search_tree_using_queue.py b/binary_search_tree_using_queue.py similarity index 100% rename from .idea/binary_search_tree_using_queue.py rename to binary_search_tree_using_queue.py diff --git a/.idea/binary_tree_example.py b/binary_tree_example.py similarity index 100% rename from .idea/binary_tree_example.py rename to binary_tree_example.py diff --git a/.idea/binary_tree_iterative_method.py b/binary_tree_iterative_method.py similarity index 100% rename from .idea/binary_tree_iterative_method.py rename to binary_tree_iterative_method.py diff --git a/.idea/inorder_traversal_in_tree.py b/inorder_traversal_in_tree.py similarity index 100% rename from .idea/inorder_traversal_in_tree.py rename to inorder_traversal_in_tree.py 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/.idea/linear_search_ex1.py b/linear_search_ex1.py similarity index 100% rename from .idea/linear_search_ex1.py rename to linear_search_ex1.py diff --git a/.idea/linear_search_tree.py b/linear_search_tree.py similarity index 100% rename from .idea/linear_search_tree.py rename to linear_search_tree.py 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/.idea/linear_search_using_list.py b/linear_search_using_list.py similarity index 100% rename from .idea/linear_search_using_list.py rename to linear_search_using_list.py diff --git a/.idea/list_example.py b/list_example.py similarity index 100% rename from .idea/list_example.py rename to list_example.py diff --git a/.idea/list_using_slicing.py b/list_using_slicing.py similarity index 100% rename from .idea/list_using_slicing.py rename to list_using_slicing.py diff --git a/.idea/python_lists.py b/python_lists.py similarity index 100% rename from .idea/python_lists.py rename to python_lists.py diff --git a/.idea/shell_sort.py b/shell_sort.py similarity index 100% rename from .idea/shell_sort.py rename to shell_sort.py diff --git a/.idea/shell_sort_ex1.py b/shell_sort_ex1.py similarity index 100% rename from .idea/shell_sort_ex1.py rename to shell_sort_ex1.py diff --git a/.idea/shell_sort_using_array.py b/shell_sort_using_array.py similarity index 100% rename from .idea/shell_sort_using_array.py rename to shell_sort_using_array.py diff --git a/.idea/shell_sort_using_list.py b/shell_sort_using_list.py similarity index 100% rename from .idea/shell_sort_using_list.py rename to shell_sort_using_list.py diff --git a/.idea/slicing_in_a_list.py b/slicing_in_a_list.py similarity index 100% rename from .idea/slicing_in_a_list.py rename to slicing_in_a_list.py diff --git a/.idea/tree_traversal.py b/tree_traversal.py similarity index 100% rename from .idea/tree_traversal.py rename to tree_traversal.py From f7ecdca79d32675f120919f032839adf25b20bee Mon Sep 17 00:00:00 2001 From: JS Date: Sat, 7 Jan 2023 23:49:02 +0530 Subject: [PATCH 12/31] Practice program --- .idea/python-programs.iml | 7 +++++++ list_example1.py | 13 +++++++++++++ list_using_slicing.py | 4 ++-- queue using list.py | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 .idea/python-programs.iml create mode 100644 list_example1.py diff --git a/.idea/python-programs.iml b/.idea/python-programs.iml new file mode 100644 index 0000000..66b1709 --- /dev/null +++ b/.idea/python-programs.iml @@ -0,0 +1,7 @@ + + + + + + + \ 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 index aeee1aa..f1c0b8d 100644 --- a/list_using_slicing.py +++ b/list_using_slicing.py @@ -1,6 +1,6 @@ # 6-01-23 # list using slicing -list = [1,2,3,4,5,6,7] +list = [1, 2, 3, 4, 5, 6, 7] print(list[0]) print(list[1]) print(list[2]) @@ -10,4 +10,4 @@ # 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]) \ No newline at end of file +print(list[1:6:2]) 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') From c0851a81d30d52df1b3cc316a0d707159e608461 Mon Sep 17 00:00:00 2001 From: JS Date: Sun, 8 Jan 2023 19:59:50 +0530 Subject: [PATCH 13/31] Practice --- Tuple.py | 7 +++++++ tuple_ex.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 Tuple.py create mode 100644 tuple_ex.py 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/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 From e9578d40a49188534a56512c191ad828d867b4f7 Mon Sep 17 00:00:00 2001 From: JS Date: Mon, 9 Jan 2023 22:40:18 +0530 Subject: [PATCH 14/31] Practice --- Tuple1.py | 14 ++++++++++++++ tuple_ex1.py | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 Tuple1.py create mode 100644 tuple_ex1.py 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/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) From 6b862d17be053d3f2078c22d7991e2ce4cb3e49c Mon Sep 17 00:00:00 2001 From: JS Date: Tue, 10 Jan 2023 19:48:09 +0530 Subject: [PATCH 15/31] Practice --- set.py | 23 +++++++++++++++++++++++ set_example.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 set.py create mode 100644 set_example.py 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/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) From 25940aafbe4807d5aa95fb38d2003631cbecacf4 Mon Sep 17 00:00:00 2001 From: JS Date: Wed, 11 Jan 2023 19:23:04 +0530 Subject: [PATCH 16/31] Practice --- set1.py | 8 ++++++++ set_ex1.py | 10 ++++++++++ 2 files changed, 18 insertions(+) create mode 100644 set1.py create mode 100644 set_ex1.py 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) From f1996f15336c79c95e7c9b5f0ef4937a737626c6 Mon Sep 17 00:00:00 2001 From: JS Date: Mon, 16 Jan 2023 23:54:13 +0530 Subject: [PATCH 17/31] Practice programs --- dictionary_.py | 5 +++++ dictionary_example.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 dictionary_.py create mode 100644 dictionary_example.py 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) From e369779acc7bc71dcdb331f4a4ac807fe749de09 Mon Sep 17 00:00:00 2001 From: JS Date: Tue, 17 Jan 2023 18:43:22 +0530 Subject: [PATCH 18/31] Practice program --- key_or_value_exists_in_a_dictionary.py | 10 ++++++++++ merge_two_dictionaries.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 key_or_value_exists_in_a_dictionary.py create mode 100644 merge_two_dictionaries.py diff --git a/key_or_value_exists_in_a_dictionary.py b/key_or_value_exists_in_a_dictionary.py new file mode 100644 index 0000000..9cf8f99 --- /dev/null +++ b/key_or_value_exists_in_a_dictionary.py @@ -0,0 +1,10 @@ +# 17-1-23 +# check if key or value exists in a dictionary +D = {'name': 'Jay', + 'age': 25, + 'job': 'Devops engineer'} + +print('name' in D) +# Prints True +print('salary' in D) +# Prints False diff --git a/merge_two_dictionaries.py b/merge_two_dictionaries.py new file mode 100644 index 0000000..aa215e0 --- /dev/null +++ b/merge_two_dictionaries.py @@ -0,0 +1,16 @@ +# 17-1-23 +# merge two dictionaries +D1 = {'name': 'Jay', + 'age': 25, + 'job': 'Devops engineer', + 'salary': '300000' + } + +D2 = {'Name': 'Messi', + 'age': 30, + 'city': 'Argentina', + 'email': 'lionMessi@gmail.com', + 'profession': 'footballer'} + +D1.update(D2) +print(D1) From 3c073efbdc20b6add4d727298ba357e4552d5902 Mon Sep 17 00:00:00 2001 From: JS Date: Wed, 18 Jan 2023 23:48:09 +0530 Subject: [PATCH 19/31] Practice program --- if__else.py | 6 ++++++ key_or_value_exists_in_a_dictionary.py | 10 ---------- merge_dictionary_ex1.py | 6 ++++++ merge_two_dictionaries.py | 19 +++++++------------ 4 files changed, 19 insertions(+), 22 deletions(-) create mode 100644 if__else.py delete mode 100644 key_or_value_exists_in_a_dictionary.py create mode 100644 merge_dictionary_ex1.py 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/key_or_value_exists_in_a_dictionary.py b/key_or_value_exists_in_a_dictionary.py deleted file mode 100644 index 9cf8f99..0000000 --- a/key_or_value_exists_in_a_dictionary.py +++ /dev/null @@ -1,10 +0,0 @@ -# 17-1-23 -# check if key or value exists in a dictionary -D = {'name': 'Jay', - 'age': 25, - 'job': 'Devops engineer'} - -print('name' in D) -# Prints True -print('salary' in D) -# Prints False 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 index aa215e0..789d27e 100644 --- a/merge_two_dictionaries.py +++ b/merge_two_dictionaries.py @@ -1,16 +1,11 @@ # 17-1-23 # merge two dictionaries -D1 = {'name': 'Jay', - 'age': 25, - 'job': 'Devops engineer', - 'salary': '300000' - } -D2 = {'Name': 'Messi', - 'age': 30, - 'city': 'Argentina', - 'email': 'lionMessi@gmail.com', - 'profession': 'footballer'} +def merge(dict1, dict2): + result = dict1 | dict2 # merge operator (|) + return result -D1.update(D2) -print(D1) + +dict1 = {'A': 'Jay ', 'B': 'Messi ', 'C': 'Ronaldo'} +dict2 = {'D': 'Hardik Pandiya ', 'E': 'Rishabh pant', 'F': 'Kohli'} +print(merge(dict1, dict2)) # print dict3 From ce7b0f2b29b8d96ca1ed622846ef6b957d7a7f72 Mon Sep 17 00:00:00 2001 From: JS Date: Thu, 19 Jan 2023 20:14:31 +0530 Subject: [PATCH 20/31] Practice program --- if.py | 9 +++++++++ if__else__example.py | 11 +++++++++++ 2 files changed, 20 insertions(+) create mode 100644 if.py create mode 100644 if__else__example.py 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__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 ") From 3fcf6ba9f1499ec2973ddf71b4b86a8a697d5f8e Mon Sep 17 00:00:00 2001 From: JS Date: Tue, 24 Jan 2023 23:20:52 +0530 Subject: [PATCH 21/31] Practice program --- if__elif__example1.py | 11 +++++++++++ if_else_if_example.py | 10 ++++++++++ if_else_if_program.py | 15 +++++++++++++++ nested_if.py | 17 +++++++++++++++++ nested_if_example.py | 20 ++++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 if__elif__example1.py create mode 100644 if_else_if_example.py create mode 100644 if_else_if_program.py create mode 100644 nested_if.py create mode 100644 nested_if_example.py 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_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/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_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!') From 57dccd4319b082c67ced894668757227eab6a4da Mon Sep 17 00:00:00 2001 From: JS Date: Wed, 25 Jan 2023 21:48:54 +0530 Subject: [PATCH 22/31] Practice program --- nested_.py | 1 + nested_if_ex1.py | 18 ++++++++++++++++++ nested_if_example2.py | 12 ++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 nested_.py create mode 100644 nested_if_ex1.py create mode 100644 nested_if_example2.py 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_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_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") From b7c4bea35404feaa326f2483ac18069dd47df667 Mon Sep 17 00:00:00 2001 From: JS Date: Fri, 27 Jan 2023 23:34:55 +0530 Subject: [PATCH 23/31] practice program --- for_loop_ex1.py | 13 +++++++++++++ for_loop_example.py | 8 ++++++++ 2 files changed, 21 insertions(+) create mode 100644 for_loop_ex1.py create mode 100644 for_loop_example.py 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 From f8acdb3bcfcfc5cad43ac265d12f7e9567ebfb66 Mon Sep 17 00:00:00 2001 From: JS Date: Mon, 30 Jan 2023 23:57:05 +0530 Subject: [PATCH 24/31] Practice program --- for_loop.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 for_loop.py 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 From 96211e0461a38c2ea04544caeea71e238d87d6d3 Mon Sep 17 00:00:00 2001 From: JS Date: Tue, 31 Jan 2023 23:28:39 +0530 Subject: [PATCH 25/31] Practice program --- while_loop.py | 11 +++++++++++ while_loop_example.py | 8 ++++++++ 2 files changed, 19 insertions(+) create mode 100644 while_loop.py create mode 100644 while_loop_example.py 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_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) From c1484ab402c79b2640e1c185d99de022989f2de4 Mon Sep 17 00:00:00 2001 From: JS Date: Wed, 1 Feb 2023 23:36:20 +0530 Subject: [PATCH 26/31] Practice program --- while_loop_ex.py | 8 ++++++++ while_loop_ex1.py | 7 +++++++ 2 files changed, 15 insertions(+) create mode 100644 while_loop_ex.py create mode 100644 while_loop_ex1.py diff --git a/while_loop_ex.py b/while_loop_ex.py new file mode 100644 index 0000000..5159cea --- /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) \ No newline at end of file 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 From bc10ac5e737942fa421588f75d27b83120286cbf Mon Sep 17 00:00:00 2001 From: JS Date: Fri, 3 Feb 2023 01:10:43 +0530 Subject: [PATCH 27/31] Practice program --- reverse_while_loop.py | 6 ++++++ while_loop_ex.py | 2 +- while_loop_using_else_statement.py | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 reverse_while_loop.py create mode 100644 while_loop_using_else_statement.py 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/while_loop_ex.py b/while_loop_ex.py index 5159cea..4465473 100644 --- a/while_loop_ex.py +++ b/while_loop_ex.py @@ -5,4 +5,4 @@ num += 1 if num % 2 != 0: continue - print(num) \ No newline at end of file + print(num) 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") From 16002ed5d2fdfe5d70040f8365bb933fcf9eef93 Mon Sep 17 00:00:00 2001 From: JS Date: Sun, 5 Feb 2023 00:31:33 +0530 Subject: [PATCH 28/31] Practice program --- do_while_loop.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 do_while_loop.py 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 From 0fe296c9517f5fc06387ed852a2bea1d3ee701c0 Mon Sep 17 00:00:00 2001 From: JS Date: Wed, 8 Feb 2023 00:18:26 +0530 Subject: [PATCH 29/31] Practice program --- break_statement_in_a_for_loop.py | 13 +++++++++++++ break_statement_using_for_loop_ex1.py | 9 +++++++++ 2 files changed, 22 insertions(+) create mode 100644 break_statement_in_a_for_loop.py create mode 100644 break_statement_using_for_loop_ex1.py 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('') From e4b2b6cbe8deb001cea07b8761357a141b7bb40d Mon Sep 17 00:00:00 2001 From: JS Date: Wed, 8 Feb 2023 22:06:04 +0530 Subject: [PATCH 30/31] Practice program --- break_while_loop.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 break_while_loop.py 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 From 1197e45037d9cc0614356f7814045a0d80b94e13 Mon Sep 17 00:00:00 2001 From: JS Date: Sun, 12 Feb 2023 00:52:20 +0530 Subject: [PATCH 31/31] Practice programs --- continue_statement_in_for_loop.py | 13 +++++++++++++ continue_statement_in_for_loop_example.py | 8 ++++++++ swap_elements_using_list.py | 13 +++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 continue_statement_in_for_loop.py create mode 100644 continue_statement_in_for_loop_example.py create mode 100644 swap_elements_using_list.py 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/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))