Found 10476 Articles for Python

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

AmitDiwan
Updated on 17-Apr-2021 12:08:17

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

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

AmitDiwan
Updated on 17-Apr-2021 12:07:51

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 −Example Live Demomy_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 ... Read More

Python Program to Remove the nth Index Character from a Non-Empty String

AmitDiwan
Updated on 17-Apr-2021 12:07:12

844 Views

When it is required to remove a specific index character from a string which isn’t empty, it can be iterated over, and when the index doesn’t match, that character can be stored in another string.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" print("The string is :") print(my_string) index_removed = 2 changed_string = '' for char in range(0, len(my_string)):    if(char != index_removed):       changed_string += my_string[char] print("The string after removing ", index_removed, "nd character is : ") print(changed_string)OutputThe string is : Hi there how are you The ... Read More

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

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

306 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

445 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

723 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

786 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

243 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

900 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

338 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

Advertisements