Python Articles

Page 17 of 854

Python Program to solve Maximum Subarray Problem using Kadane’s Algorithm

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 369 Views

When it is required to find the maximum sub array using Kadane’s algorithm, a method is defined that helps find the maximum of the sub array. Iterators are used to keep track of the maximum sub array.Below is the demonstration of the same −Exampledef find_max_sub_array(my_list, beg, end):    max_end_at_i = max_seen_till_now = my_list[beg]    max_left_at_i = max_left_till_now = beg    max_right_till_now = beg + 1    for i in range(beg + 1, end):       if max_end_at_i > 0:          max_end_at_i += my_list[i]       else:          max_end_at_i = my_list[i]     ...

Read More

Python Program to Calculate the Length of a String Without Using a Library Function

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

When it is required to calculate the length of a string without using library methods, a counter is used to increment every time an element of the string is encountered.Below is the demonstration of the same −Examplemy_string = "Hi Will" print("The string is :") print(my_string) my_counter=0 for i in my_string:    my_counter=my_counter+1 print("The length of the string is ") print(my_counter)OutputThe string is : Hi Will The length of the string is 7ExplanationA string is defined, and is displayed on the console.A counter is initialized to 0.The string is iterated over, and after every element is iterated over, the counter is ...

Read More

Python Program for Depth First Binary Tree Search using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 456 Views

When it is required to perform depth first search on a tree using recursion, a class is defined, and methods are defined on it that help perform breadth first search.Below is a demonstration for the same −Exampleclass BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key    def insert_at_left(self, new_node):       self.left = new_node    def insert_at_right(self, new_node):       self.right = new_node    def search_elem(self, key):     ...

Read More

Python Program to Find the Largest value in a Tree using Inorder Traversal

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 248 Views

When it is required to find the largest value in a tree using in order traversal, a binary tree class is created with methods to set the root element, perform in order traversal using recursion and so on.An instance of the class is created, and it can be used to access the methods.Below is the demonstration of the same −Exampleclass BinaryTree_Struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key    def inorder_traversal_largest(self):     ...

Read More

Python Program to Print only Nodes in Left SubTree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 323 Views

When it is required to print the nodes in the left subtree, a class can be created that consists of methods can be defined to set the root node, perform in order traversal, insert elements to the right of the root node, to the left of the root node, and so on. An instance of the class is created, and the methods can be used to perform the required operations.Below is a demonstration of the same −Exampleclass BinaryTree_struct:    def __init__(self, data=None):       self.key = data       self.left = None       self.right = None ...

Read More

Python Program to Display the Nodes of a Tree using BFS Traversal

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 958 Views

When it is required to display the nodes of a tree using the breadth first search traversal, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, perform ‘bfs’ (breadth first search) and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Exampleclass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data    def ...

Read More

Python Program to Find All Connected Components using DFS in an Undirected Graph

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

When it is required to find all the connected components using depth first search in an undirected graph, a class is defined that contains methods to initialize values, perform depth first search traversal, find the connected components, add nodes to the graph and so on. The instance of the class can be created and the methods can be accessed and operations and be performed on it.Below is a demonstration of the same −Exampleclass Graph_struct:    def __init__(self, V):       self.V = V       self.adj = [[] for i in range(V)]    def DFS_Utililty(self, temp, v, ...

Read More

Python Program to Find the Sum of All Nodes in a Binary Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 289 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Exampleclass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data ...

Read More

Take in Two Strings and Display the Larger String without Using Built-in Functions in Python Program

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 895 Views

When it is required to take two strings and display the larger string without using any built-in function, the counter can be used to get the length of the strings, and ‘if’ condition can be used to compare their lengths.Below is the demonstration of the same −Examplestring_1= "Hi there" string_2= "Hi how are ya" print("The first string is :") print(string_1) print("The second string is :") print(string_2) count_1 = 0 count_2 = 0 for i in string_1:    count_1=count_1+1 for j in string_2:    count_2=count_2+1 if(count_1

Read More

Python Program to Find All Connected Components using BFS in an Undirected Graph

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 765 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Exampleclass Graph_structure:    def __init__(self, V):       self.V = V       self.adj = [[] for i in range(V)]    def DFS_Utility(self, temp, v, ...

Read More
Showing 161–170 of 8,532 articles
« Prev 1 15 16 17 18 19 854 Next »
Advertisements