Python Articles - Page 377 of 929

Program to count maximum score from removing substrings in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:58:19

245 Views

Suppose we have a string s and two values x and y. We can perform given two types of operations any number of times.Search substring "ab", if present, then we can gain x points by removing it.Search substring "ba", if present, then we can gain y points by removing it.We have to find maximum points we can gain after applying the above operations on s.So, if the input is like s = "cbbaacdeabb" x = 4 y = 5, then the output will be 14 because initial string is "cbbaacdeabb", then remove "cbbaacde(ab)b" to get 4, now string is "cbbaacdeb", ... Read More

Program to find out the minimum path to deliver all letters in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:51:28

210 Views

Suppose there are n cities and that are connected with n -1 roads. A city can be visited from any other city. Now the postal system of the cities delivers k letters daily. The letter's destination can be any of the k different cities. A postal worker has to deliver all the letters to their addresses each day. We shall have to find out the minimum distance the worker has to travel to deliver all the letters. The worker can start from any given city.So, if the input is likeand the letters have to be delivered in cities (delv) 1, ... Read More

Program to find out special types of subgraphs in a given graph in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:48:43

527 Views

Suppose we have a special type of graph that has two types of vertices that are named head and feet. The graph has only one head and there are k edges that connect the head to each of the feet. So, if we are given an undirected, unweighted graph; we shall have to find out these special types of graphs in the vertex disjoint subgraphs of the graph. Any two graphs are said to be vertex disjoint if they have no vertex in common.So, if the input is likenumber of nodes (n) = 5, number of feet (t) = 2, ... Read More

Program to find out the minimum cost path between the lowest value vertex to the highest value vertex (Python)

Arnab Chakraborty
Updated on 06-Oct-2021 12:44:27

550 Views

Suppose we are given an undirected, weighted graph and are asked to find out the path with the minimum possible travel cost from a particular node to another particular node. The travel cost is calculated as the following: suppose there is a path between vertex A to vertex C as A-> B-> C. The cost of travel from A to B is 10 and the cost of travel from B to C is 20. The cost of travel from A to C will be (cost of traveling from A to B) + (difference of traveling cost from B to C ... Read More

Program to find out the path between two vertices in a graph that has the minimum penalty (Python)

Arnab Chakraborty
Updated on 06-Oct-2021 12:36:32

387 Views

Suppose we are given an undirected, weighted graph and are asked to find out the path with the minimum possible penalty from node a to node b. The penalty of a path is the bitwise OR of the weights of all the edges in the path. So, we must find out such a 'minimum penalty' path, and if there exists no path between the two nodes, we return -1.So, if the input is likestart (s) = 1, end (e) = 3; then the output will be 15.There exist two paths between vertices 1 and 3. The optimal path is 1->2->3, ... Read More

Program to find out the minimum size of the largest clique in a graph (Python)

Arnab Chakraborty
Updated on 06-Oct-2021 12:22:51

267 Views

Suppose we are given a graph and are asked to find out the minimum size of the largest clique in the graph. A clique of a graph is a subset of a graph where every pair of vertices are adjacent, i.e. there exists an edge between every pair of vertices. Finding the largest clique in a graph is not possible in polynomial time, so given the number of nodes and edges of a small graph we shall have to find out the largest clique in it.So, if the input is like nodes = 4, edges =4; then the output will ... Read More

Program to find number of ways to split array into three subarrays in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:39:56

772 Views

Suppose we have an array called nums, we have to find the number of good ways to split this array nums. Answer may be too large so return result modulo 10^9 + 7. Here a split of an array (with integer elements) is good if the array is split into three non-empty contiguous subarrays respectively from left to right, and the sum of the elements in left side is less than or equal to the sum of the elements in mid part, and the sum of the elements in mid part is less than or equal to the sum of ... Read More

Program to find out an MST using Prim's algorithm in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:19:34

2K+ Views

Suppose we are given a graph and asked to find out the 'Minimum Spanning Tree' (MST) from that graph. An MST of a graph is a subset of a weighted graph where all the vertices are present and connected, and there exists no cycle in the subset. MST is called minimum because the total edge weight of the MST is the minimum possible from the graph. So, here we use Prim's MST algorithm and find out the total edge weight of the MST from a given graph.So, if the input is like, number of vertices (n) is 4, and start ... Read More

Program to find out the minimum moves in a snakes and ladders game in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:10:39

1K+ Views

Suppose we are playing a game of snakes and ladders. We have a condition that we can roll any number that we can like on a dice. We start from position 0 and our destination is position 100, and we roll the dice several times to reach the destination. We must find out the least number of dice rolls required to reach the destination if we are provided with the position of the snakes and ladders on the board.The arrays snakes and ladders represent the positions of snakes and ladders in the board and each entry in the arrays contains ... Read More

Program to count good meals with exactly two items in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:10:13

323 Views

Suppose we have an array deli where deli[i] is the deliciousness of the ith food, we have to find the number of different good meals we can make from this list. If the answer is too large, then return result modulo 10^9 + 7. Here a good meal means a meal that contains exactly two different food items with a sum of deliciousness which is a power of two. We can select any two different foods to make a good meal.So, if the input is like deli = [1, 7, 3, 6, 5], then the output will be 3 because ... Read More

Advertisements