Find Number of Substrings with Only 1s Using Python

Arnab Chakraborty
Updated on 29-May-2021 12:57:04

217 Views

Suppose we have a binary string s. We have to find the number of substrings with all characters 1's. The answer may be very large so return result mod 10^9 + 7.So, if the input is like s = "1011010", then the output will be 5 because 1. four times "1" 2. one time "11"To solve this, we will follow these steps −m := 10^9+7result := 0div := divide the binary string by splitting it using '0'for each x in div, doif x is empty, then go for next iterationresult := result + quotient of (size of x *(size of ... Read More

Find Path with Maximum Probability Using Python

Arnab Chakraborty
Updated on 29-May-2021 12:56:07

554 Views

Suppose we have an undirected weighted graph with n nodes (nodes are numbered from 0 onwards), This graph is given as input using edge list, for each edge e, it has a probability of success of traversing that edge probability[e]. We also have start and end nodes, we have to find the path with the maximum probability of success to go from start to end and return its success probability. If we cannot find any path, then return 0.So, if the input is likethen the output will be 0.24 because there are two paths from node 0 to 2, one ... Read More

Find Number of Nodes in Sub-tree with Same Label Using Python

Arnab Chakraborty
Updated on 29-May-2021 12:54:53

455 Views

Suppose we have a rooted general tree with n nodes, whose nodes are numbered from 0 to n-1. Each node has a label with lowercase English letter. The labels are given as input in labels array, where lables[i] contains label for ith node. The tree is represented by edge list where each edge e has [u, v] represents u is parent and v is child. We have to find an array A of size n, represents number of nodes in the subtree of the ith node with same label as iSo, if the input is likeHere n = 5 and ... Read More

Find Number of Sub-Arrays with Odd Sum Using Python

Arnab Chakraborty
Updated on 29-May-2021 12:53:52

249 Views

Suppose we have an array arr. We have to find the number of sub-arrays with odd sum. If the answer is too large then return result modulo 10^9+7.So, if the input is like arr = [8, 3, 7], then the output will be 3 because all sub arrays are [[8], [3], [7], [8, 3], [3, 7], [8, 3, 7]] Now their sum values are [8, 3, 7, 11, 10, 18] so there are three odd sum values [3, 7, 11].To solve this, we will follow these steps −freq := a list with two elements 1 and 0ans := 0prefix := ... Read More

Show Line Numbers in Arduino IDE

Yash Sanghvi
Updated on 29-May-2021 12:52:36

3K+ Views

Line numbers are often necessary when working with large files consisting of several functions. Most developers prefer to have the line numbers shown in their code editing software.By default, line numbers are hidden in the Arduino IDE. In order to display the line numbers, go to File → Preferences.In the dialog that opens, tick the box that says ‘Display Line Numbers’.The line numbers will now appear on the Sketch.Alternatively, the line number of your cursor can always be obtained from the bottom-left corner of the screen. This is one of the not-so-well-known features of Arduino IDE.

Find Number of Good Ways to Split a String Using Python

Arnab Chakraborty
Updated on 29-May-2021 12:51:55

390 Views

Suppose we have a string s. Now a split is said to be a good split when we can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the equal. We have to find the number of good splits we can make in s.So, if the input is like s = "xxzxyx", then the output will be 2 as there are multiple ways of splitting but if we split like ("xxz", "xyx") or ("xxzx", "yx") then they are good.To solve this, we ... Read More

Animate a Line Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 28-May-2021 15:16:11

3K+ Views

To animate the line plot in matplotlib, we can take the following steps −Create a figure and a set of subplots using subplots() method.Limit x and y axes scale.Create x and t data points using numpy.Return coordinate matrices from coordinate vectors, X2 and T2.Plot a line with x and F data points using plot() method.To make animation plot, update y data.Make an animation by repeatedly calling a function *func*, current fig, animate, and interval.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, animation plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = ... Read More

Add Two Polynomials Using Linked Lists in Python

Arnab Chakraborty
Updated on 28-May-2021 14:04:57

3K+ Views

Suppose, we are given two polynomials and we have to find out the addition of the two polynomials. The polynomials have to be represented as linked lists; the terms of the polynomials will be represented as a linked list node. Each linked list node will contain the coefficient value, power value, and the pointer to the next linked list node. We have to return a third linked list which is the addition of two linked list polynomials.So, if the input is like1x^1 + 1x^2 = 0 and 2x^1 + 3x^0 = 0, then the output will be 3x^1 + 1x^2 ... Read More

Build and Evaluate an Expression Tree Using Python

Arnab Chakraborty
Updated on 28-May-2021 14:03:32

2K+ Views

Suppose, we are given the post order traversal of an expression tree. We have to build an expression tree from the given post-order traversal, and then evaluate the expression. We return the root of the expression tree and the evaluated value of the tree.So, if the input is likethen the output will be -7.The postfix order given as input of the tree is ['1', '2', '+', '3', '4', '+', '*']. The expression if evaluated, becomes (1 – 2) * (3 + 4); which equals -7.To solve this, we will follow these steps −LEFT = 0 RIGHT = 1Define a function ... Read More

Find Lowest Common Ancestor of a Binary Tree Using Python

Arnab Chakraborty
Updated on 28-May-2021 14:02:53

187 Views

Suppose, we are given a binary tree and also two specific nodes x and y. We have to find out the lowest common ancestor of the two nodes from the binary tree. The lowest common ancestor in a binary tree is the lowest node of which both the nodes x and y are descendants of. Also, a particular node can also be a descendant of itself. We have to find the node and return it as an output.So, if the input is likeand x = 2, y = 4; then the output will be 3.The node of which the nodes ... Read More

Advertisements