Found 26504 Articles for Server Side Programming

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

AmitDiwan
Updated on 17-Apr-2021 12:06:47

303 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 −Example Live Demodef 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 Find if Undirected Graph contains Cycle using BFS

AmitDiwan
Updated on 17-Apr-2021 11:50:55

438 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 −Example Live Demofrom collections import deque def add_edge(adj: list, u, v):    adj[u].append(v)    adj[v].append(u) def detect_cycle(adj: list, s, V, visited: list):    parent = [-1] * ... Read More

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

AmitDiwan
Updated on 17-Apr-2021 11:50:37

720 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 −Example Live Democlass Graph_structure:    def __init__(self, V):       self.V = V       self.adj = [[] for i in range(V)]    def DFS_Utility(self, temp, ... Read More

Python Program to Find All Nodes Reachable from a Node using BFS in a Graph

AmitDiwan
Updated on 17-Apr-2021 11:50:18

780 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 −Example Live Demofrom collections import deque def add_edge(v, w):    global visited_node, adj    adj[v].append(w)    adj[w].append(v) def BFS_operation(component_num, src):    global visited_node, adj    queue ... Read More

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

AmitDiwan
Updated on 17-Apr-2021 11:49:29

240 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 −Example Live Democlass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = ... Read More

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

AmitDiwan
Updated on 17-Apr-2021 11:49:49

889 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 −Example Live Democlass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data   ... Read More

Python Program to Print All Permutations of a String in Lexicographic Order using Recursion

AmitDiwan
Updated on 16-Apr-2021 12:43:57

335 Views

When it is required to print all the permutations of a string in lexicographic order using recursion, a method is defined, that uses the ‘for’ loop to iterate over the sequence of elements, and use the ‘join’ method to join the elements.Below is the demonstration of the same −Example Live Demofrom math import factorial def lexicographic_permutation_order(s):    my_sequence = list(s)    for _ in range(factorial(len(my_sequence))):       print(''.join(my_sequence))       next = next_in_permutation(my_sequence)       if next is None:          my_sequence.reverse()       else:          my_sequence = next def ... Read More

Python Program to Print only Nodes in Left SubTree

AmitDiwan
Updated on 16-Apr-2021 12:41:26

275 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 −Example Live Democlass BinaryTree_struct:    def __init__(self, data=None):       self.key = data       self.left = None       self.right = ... Read More

Python Program to Print All Permutations of a String in Lexicographic Order without Recursion

AmitDiwan
Updated on 16-Apr-2021 12:38:58

455 Views

When it is required to print all the permutations of a string in the lexicographic order without using recursion, a method is defined, that takes the string as the parameter. It uses a simple ‘for’ loop to iterate over the string elements and uses ‘while’ condition to check for certain constraints.Below is the demonstration of the same −Example Live Demofrom math import factorial def lex_permutation(my_string): for i in range(factorial(len(my_string))):    print(''.join(my_string))    i = len(my_string) - 1    while i > 0 and my_string[i-1] > my_string[i]:       i -= 1    my_string[i:] = reversed(my_string[i:])    if i > 0: ... Read More

Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String

AmitDiwan
Updated on 16-Apr-2021 12:38:18

719 Views

When it is required to form a new string that is made from the first two and last two characters of a given string, a counter can be defined, and indexing can be used to access specific range of elements.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" my_counter = 0 for i in my_string:    my_counter = my_counter + 1 new_string = my_string[0:2] + my_string [my_counter - 2: my_counter ] print("The string is ") print(my_string) print("The new string is ") print(new_string)OutputThe string is Hi there how are you The new ... Read More

Advertisements