Found 26504 Articles for Server Side Programming

Program to find length of longest palindromic substring in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:41:59

643 Views

Suppose we have a string S. We have to find the length of longest palindromic substring in S. We are assuming that the length of the string S is 1000. So if the string is “BABAC”, then the longest palindromic substring is “BAB” and length is 3.To solve this, we will follow these steps −Define one square matrix of order same as the length of string, and fill it with FalseSet the major diagonal elements as true, so DP[i, i] = True for all i from 0 to order – 1start := 0for l in range 2 to length of ... Read More

How to calculate population variance in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 13:22:31

568 Views

There is no function in R to calculate the population variance but we can use the population size and sample variance to find it. We know that the divisor in population variance is the population size and if we multiply the output of var(it calculates sample variance) function with (population size – 1)/population size then the output will be population variance.Example Live Demoset.seed(141) x1

How to select only one column from an R data frame and return it as a data frame instead of vector?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 13:20:40

368 Views

Generally, if we extract a single column from an R data frame then it is extracted as a vector but we might want it in data frame form so that we can apply operations of a data frame on it. Therefore, we can use single square brackets for the extraction with T (TRUE) or (FALSE) values and drop = FALSE so that the output becomes a data frame.Consider the below data frame −Example Live Demoset.seed(999) x1

How to find the indexes of minimum values in a vector if there are ties in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 13:16:40

420 Views

If we have repeated values in a vector that means we have ties in the vector, therefore, the indexes of values will help us to identify the positions of a particular value in the vector. We can use which function with min function to find the positions of minimum values in a vector, if there exists more than one minimum then the output will show all the relevant positions.Example Live Demox1

Program to find length of longest alternating inequality elements sublist in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:36:00

230 Views

Suppose we have a list of mumbers called nums, and find the length of the longest sublist in nums such that the equality relation between every consecutive numbers changes alternatively between less-than and greater-than operation. The first two numbers' inequality may be either less-than or greater-than.So, if the input is like nums = [1, 2, 6, 4, 5], then the output will be 4, as the longest inequality alternating sublist is [2, 6, 4, 5] as 2 < 6 > 4 < 5.To solve this, we will follow these steps −Define a function get_direction(). This will take a, breturn 0 ... Read More

How to create train, test and validation samples from an R data frame?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 13:14:30

692 Views

To create predictive models, it is necessary to create three subsets of a data set for the purpose of training the model, testing the model and checking the validation of the model. These subsets are usually called train, test and validation. For this purpose, we can use different type of sampling methods and the most common is random sampling. In the below example, you can see how it can be done.Consider the mtcars data set in base R −Example Live Demodata(mtcars) str(mtcars)Output'data.frame':32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... ... Read More

How to create a histogram using weights in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 13:07:31

1K+ Views

A histogram using weights represent the weighted distribution of the values. In R, we can use weighted.hist function of plotrix package to create this type of histogram and we just need the values and weights corresponding to each value. Since plotrix is not frequently used, we must make sure that we install this package using install.packages("plotrix") then load it in R environment.Loading plotrix package −library("plotrix")Consider the below vector and the weight associated with that vector −Examplex

How to find critical value for one-sided and two-sided t test in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 13:05:17

759 Views

To find the critical value for t test in R, we need to use qt function. This function requires level of significance and the sample size and returns the tabulated or critical value of t distribution. Below examples shows the calculation of critical value for different situations such as left-side test, right-side test or two-sided test.Left side critical value with sample size 30 and 95% confidence level −Example Live Demoqt(0.05, 30)Output[1] -1.697261Right side critical value with sample size 30 and 95% confidence level −Example Live Demoabs(qt(0.05, 30))Output[1] 1.697261 Example Live Demoqt(0.05, 50)Output[1] -1.675905Example Live Demoabs(qt(0.05, 50))Output[1] 1.675905 Example Live Demoqt(0.01, 50)Output[1] -2.403272Example Live Demoabs(qt(0.01, 50))Output[1] 2.403272 ... Read More

How to find row minimum for an R data frame?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 13:03:37

1K+ Views

Data analysis is a difficult task because it has so much variation in terms of the smaller objectives of a big project. One of the smallest tasks could be finding the minimum value in each row contained in a data frame. For this purpose, we cam use apply function and pass the FUN argument as min so that we can get minimum values.Consider the below data frame −Example Live Demoset.seed(101) x1

How to extract correlation coefficient value from correlation test in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:59:49

1K+ Views

To perform the correlation test in R, we need to use cor.test function with two variables and it returns so many values such as test statistic value, degrees of freedom, the p-value, the confidence interval, and the correlation coefficient value. If we want to extract the correlation coefficient value from the correlation test output then estimate function could be used as shown in below examples.Example Live Demox1

Advertisements