Found 33676 Articles for Programming

Program to check whether a tree is height balanced or not in C++

Arnab Chakraborty
Updated on 08-Oct-2020 14:46:10

614 Views

Suppose we have a binary tree; we have to check whether its height is balanced or not. We know that for a height balanced tree, for every node in the tree, the absolute difference of the height of its left subtree and the height of its right subtree is 0 or 1.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs(), this will take node, if node is null, then −return 0l := 1 + dfs(left of node)r := 1 + dfs(right of node)if |l - r| > ... Read More

Program to group a set of points into k different groups in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:28:00

1K+ Views

Suppose we have a list of points and a number k. The points are in the form (x, y) representing Cartesian coordinates. We can group any two point p1 and p2 if the Euclidean distance between them is

How to find the statistical summary of an R data frame with all the descriptive statistics?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:36:44

435 Views

When we find statistical summary of an R data frame, we only get the minimum value, first quartile, median, mean, third quartile, and maximum value but in descriptive there are many other useful measures such as variance, standard deviation, skewness, kurtosis, etc. Therefore, we can use basicStats function of fBasics package for this purpose.Loading fBasics package −library(fBasics)Consider mtcars data in base R −Example Live Demodata(mtcars) head(mtcars, 20)Output          mpg    cyl     disp    hp    drat    wt qsec vs am gear carb Mazda RX4         21.0    6 160.0 110    3.90 ... Read More

Program to count number of strings we can make using grammar rules in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:22:32

251 Views

Suppose we have a number n, we have to find the number of strings of length n can be generated using the following rules −Each character is a lower case vowel [a, e, i, o, u]"a" may only be followed by one "e""e" may only be followed by any of "a" and "i""i" may not be followed by another "i""o" may only be followed by any of "i" and "u""u" may only be followed by one "a"If the result is very large, mod the result by 10^9 + 7.So, if the input is like n = 2, then the output ... Read More

How to display R-squared value on scatterplot with regression model line in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:20:27

8K+ Views

The R-squared value is the coefficient of determination, it gives us the percentage or proportion of variation in dependent variable explained by the independent variable. To display this value on the scatterplot with regression model line without taking help from any package, we can use plot function with abline and legend functions.Consider the below data frame −Example Live Demoset.seed(1234) x

How to find the absolute pairwise difference among values of a vector in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:15:54

485 Views

If a vector contains five values then there will be ten pairwise differences. For example, suppose we have five numbers starting from 1, then the pairwise combinations for these values will be (1,2), (1,3), (1,4), (1,5), (2,3), (2,4), (2,5), (3,4), (3,5), (4,5). Now to find the absolute pairwise differences, we would be need to find the differences between each of these combinations and take the absolute value of the answer hence the result will be 1, 2, 3, 4, 1, 2, 3, 1, 2, 1.Example Live Demox1

Program to find total mutation group of genes in C++

Arnab Chakraborty
Updated on 08-Oct-2020 14:17:07

357 Views

Suppose we have a list of strings called genes where each element has the same length and each element contains characters "A", "C", "G" and/or "T". Now there are some rules −When two strings s1 and s2 are the same string except for one character, then s1 and s2 are in the same mutation group.When two strings s1 and s2 are in a group and s2 and s3 are in a group, then s1 and s3 are in the same group.We have to find the total number of mutation groups we can generate.So, if the input is like genes = ... Read More

How to create a sequence of dates by using starting date in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:11:47

1K+ Views

The best way to create a sequence of anything is creating it with the help of seq function and this also applies to sequences of dates. But in case of dates, we need to read the dates in date format so that R can understand the input type and create the appropriate vector. If we do not use the date format for the date value then it won’t make sense to R and it will result in error.Examples Live Demox1

Program to implement the fractional knapsack problem in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:10:45

2K+ Views

Suppose we have two lists, weights and values of same length and another value capacity. The weights[i] and values[i] represent the weight and value of ith element. So if we can take at most capacity weights, and that we can take a fraction of an item's weight with proportionate value, we have to find the maximum amount of value we can get (rounded down to the nearest integer)So, if the input is like weights = [6, 7, 3] values = [110, 120, 2] capacity = 10, then the output will be 178.To solve this, we will follow these steps −res ... Read More

How to create a contingency table with sum on the margins from an R data frame?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:09:35

2K+ Views

The sum of rows and columns on the margins in a contingency table are always useful because they are used for different type of calculations such as odds ratio, probability etc. If an R data frame has factor columns then we can create a contingency table for that data frame and it can be done by using addmargins function.ExampleConsider the below data frame − Live Demox1

Advertisements