Programming Articles

Page 231 of 2544

Program to find number of arithmetic sequences from a list of numbers in Python?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have a list of numbers called nums, we have to find the number of contiguous arithmetic sequences of length ≥ 3. As we know an arithmetic sequence is a list of numbers where the difference between one number and the next number is the same.So, if the input is like nums = [6, 8, 10, 12, 13, 14], then the output will be 4, as we have the arithmetic sequences like: [6, 8, 10] [8, 10, 12] [6, 8, 10, 12] [12, 13, 14]To solve this, we will follow these steps −count := 0, ans := 0for i ...

Read More

How to find the 95% confidence interval for the glm model in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 7K+ Views

To find the confidence interval for a lm model (linear regression model), we can use confint function and there is no need to pass the confidence level because the default is 95%. This can be also used for a glm model (general linear model). Check out the below examples to see the output of confint for a glm model.Example1> set.seed(3214) > x1 y1 Model1 summary(Model1)OutputCall: glm(formula = y1 ~ x1, family = "binomial") Deviance Residuals: Min 1Q Median 3Q Max -1.6360 -1.4156 0.7800 0.8567 0.9946 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.34851 1.17554 0.296 0.767 x1 ...

Read More

Program to find number of ways we can arrange symbols to get target in Python?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 210 Views

Suppose we have a list of non-negative numbers called nums and also have an integer target. We have to find the the number of ways to arrange + and - in nums such that the expression equals to target.So, if the input is like nums = [2, 3, 3, 3, 2] target = 9, then the output will be 2, as we can have -2 + 3 + 3 + 3 + 2 and 2 + 3 + 3 + 3 – 2.To solve this, we will follow these steps:s := sum of all numbers in numsif (s + target) ...

Read More

How to replace $ sign combined with some specific values in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 167 Views

Sometimes we get very dirty data and that is the reason data analysis is a difficult task. Most of the data scientists look for clean data but it is almost impossible due to data warehouses often just focus on the data availability instead of the quality of data. One of the head scratching situations is getting an unnecessary value placed at different position in a random manner, $ sign is also a that type of value. We can remove this from an R data frame by using lapply function.ExampleConsider the below data frame:> x y df1 df1Outputx y 1 C ...

Read More

Program to find minimum number of cells it will take to reach bottom right corner in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 271 Views

Suppose we have a 2D grid representing a maze where 0 is for empty space, and 1 is the wall. We will start at grid[0, 0], we have to find the minimum number of squares it would take to get to bottom right corner of the grid. If we cannot reach, return −1.So, if the input is like000100100then the output will be 5To solve this, we will follow these steps −R := row count of grid, C := column count of gridq := [0, 0, 1] when A[0, 0] is 1 otherwise a new listA[0, 0] := 1for each (r, ...

Read More

Program to find minimum distance that needs to be covered to meet all person in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 276 Views

Suppose we have a 2D matrix there are few values like below −0 represents an empty cell.1 represents a wall.2 represents a person.Here a person can walk any of these four directions (up, down, left and right). We have to find a cell that is not wall such that it minimizes the total travel distance each person has to walk to and finally find the distance.So, if the input is like201010120022then the output will be 7 as the best meeting point is the bottom right corner.To solve this, we will follow these steps −twos := a new map, costs := ...

Read More

How to name a data frame column with a vector value that has the same name in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 621 Views

To change the column name of a data frame in R, we can use setNames function. For example, if we have a data frame called df that contains column x and we want to change it to value “Ratings” which is stored in a vector called x then we can use the code df x y yOutput x 1 3 2 8 3 3 4 9 5 5 6 5 7 10 8 2 9 6 10 6 11 3 12 5 13 9 14 1 15 1 16 6 17 2 18 6 19 10 20 6Changing x in y to Ratings:Example> y yOutputRatings 1 3 2 8 3 3 4 9 5 5 6 5 7 10 8 2 9 6 10 6 11 3 12 5 13 9 14 1 15 1 16 6 17 2 18 6 19 10 20 6Let’s have a look at another example:Example> S df_Salary df_SalaryOutput S 1 31827 2 24697 3 45790 4 45345 5 22294 6 30749 7 37721 8 33535 9 45941 10 24028 11 48927 12 33818 13 49152 14 43334 15 20294 16 29664 17 23358 18 20475 19 39355 20 40386Changing S in df_Salary to Salary:Example> df_Salary df_SalaryOutputSalary 1 31827 2 24697 3 45790 4 45345 5 22294 6 30749 7 37721 8 33535 9 45941 10 24028 11 48927 12 33818 13 49152 14 43334 15 20294 16 29664 17 23358 18 20475 19 39355 20 40386

Read More

How to create a matrix using vector generated with rep function in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 423 Views

Matrix can be only generated if we pass an even number of elements for it. If we want to create a matrix using vector generated with rep function then the length of this vector must be divisible by 2. For example, if we have a vector x that is created with rep function and it’s length is 20 then the matrix say M of size 10x2 using that vector can be constructed by using matrix(x, ncol=2).Example 1> x M1 M1Output[, 1] [, 2] [1, ] 10 10 [2, ] 4 4 [3, ] 7 7 [4, ] 3 3 [5, ...

Read More

Program to find column index where left most 1 is present in a binary matrix in Python?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 281 Views

Suppose we have a 2D binary matrix. Here each row is sorted in ascending order with 0s coming before 1s, we have to find the leftmost column index with the value of 1. If there's no such result, return -1.So, if the input is like0001001100110010then the output will be 2, as the second column has left most 1 in entire matrix.To solve this, we will follow these steps:if matrix is empty, thenreturn -1N := row count of matrixM := column count of matrixi := 0, j := M - 1leftmost := -1while i < N and j >= 0, doif ...

Read More

How to replace missing values in a column with corresponding values in other column of an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 546 Views

Often, we get missing data for analysis and we need to replace those missing values with something. Sometimes, we might want to replace them with the corresponding values in other column especially in situations when both the columns depict similar characteristics. This type of replacement can be easily done with the help of mutate function of dplyr package as shown in the below examples.Example1Consider the below data frame:> set.seed(214) > x1 x2 df1 df1Output x1 x2 1 4 75 2 8 24 3 5 38 4 4 38 5 7 NA 6 6 24 7 10 75 8 4 75 9 ...

Read More
Showing 2301–2310 of 25,433 articles
« Prev 1 229 230 231 232 233 2544 Next »
Advertisements