Server Side Programming Articles - Page 1453 of 2650

How to perform cartesian join for two data.table objects in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:27:49

1K+ Views

The cartesian join is the joining of two objects that creates the combination of each value in object with all the values in the other object. For example, if we have a vector x that contains 1, 2, 3 and the other object y contains a, b, c then the cartesian join will be 1a, 2a, 3a, 1b, 2b, 3b, 1c, 2c, and 3c. Check out the below examples to understand how it can be done.Example> library(data.table) > DT1 DT1Output x 1: 1 2: 2 3: 3 4: 4Example> DT2 DT2Output y 1: 25 2: ... Read More

Program to find length of longest sublist where difference between min and max smaller than k in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:24:34

237 Views

Suppose we have a list of numbers called nums and another value k, we have to find the length of longest sublist where the absolute difference between the largest and smallest element is ≤ k.So, if the input is like nums = [2, 4, 6, 10] k = 4, then the output will be 3, as we can select pick [2, 4, 6] here the absolute difference is 4.To solve this, we will follow these steps −Create two double ended queue maxd, mindi := 0, res := 1for each index j and value a in A, dowhile maxd is not ... Read More

How to find the column number of minimum values in each row for a data frame in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:25:26

751 Views

To find the column number of minimum values in each row for a data frame, we can use apply function but if we want to return the output in tabular form then matrix function should be used. For example, if we have a data frame df then our problem can be solved by using the code: as.matrix(apply(df, 1, which.min)).ExampleConsider the below data frame:Live Demo> set.seed(37) > x1 x2 x3 x4 x5 df1 df1Outputx1 x2 x3 x4 x5 1 1 2 4 9 3 2 0 5 8 10 4 3 1 3 8 6 1 4 1 5 5 8 ... Read More

Program to find length of longest set of 1s by flipping k bits in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:22:23

240 Views

Suppose we have a binary list, so here only 1s and 0s are available and we also have another number k. We can set at most k 0s to 1s, we have to find the length of the longest sublist containing all 1s.So, if the input is like nums = [0, 1, 1, 0, 0, 1, 1] k = 2, then the output will be 6, as we can set the two middle 0s to 1s and then the list becomes [0, 1, 1, 1, 1, 1, 1].To solve this, we will follow these steps −zeros := 0, ans := ... Read More

How to replace numbers with ordinal strings for a survey in an R vector?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:22:16

2K+ Views

The easiest way to replace numbers with ordinal strings is using ifelse function. The ifelse function in R works as ifelse(test_expression, x, y). Here, test_expression must be a logical vector or an object that can be coerced to logical). The return value is a vector with the same length as test_expression.Example1Live Demo> x1 x1Output[1] 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 0 1 [38] 0 1 1 0 0 1 0 1 ... Read More

Program to find length of longest strictly increasing then decreasing sublist in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:20:36

488 Views

Suppose we have a list of numbers called nums. We have to find the length of the longest sublist such that (minimum length 3) its values are strictly increasing and then decreasing.So, if the input is like nums = [7, 1, 3, 5, 2, 0], then the output will be 5, as the sublist is [2, 4, 6, 3, 1] is strictly increasing then decreasing.To solve this, we will follow these steps −i := 0, n := size of a, res := -infinitywhile i < n - 2, dost := ilinc := 0, ldec := 0while i < n - ... Read More

Program to find length of longest sign alternating subsequence from a list of numbers in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:18:32

221 Views

Suppose we have a list of numbers called nums, we have to find the length of longest subsequence that flips sign on each consecutive number.So, if the input is like nums = [1, 3, -6, 4, -3], then the output will be 4, as we can pick [1, -6, 4, -3].To solve this, we will follow these steps −pos := 0, neg := 0for each n in nums, doif n < 0, thenneg := pos + 1otherwise, pos := neg + 1return maximum of pos and negLet us see the following implementation to get better understanding −Example Live Democlass Solution:   ... Read More

How to create a blank csv file in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:19:09

659 Views

We can create a blank csv file using a single line code in R and the function that can do this is cat. If we want to have the file blank then NULL value will be passed inside the function and the file name must be used. For example, if we want to create a blank file named as BlankCSV then it can be created by using the below code:> cat(NULL, file="BlankCSV.csv")Output:This is the output from documents folder of the system where all the R files are stored by default(we can change that location if we want to):The output of ... Read More

Program to find length of longest interval from a list of intervals in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:16:22

527 Views

Suppose we have a list of intervals where each interval is in form [start, end]. We have to find the longest interval that we can make by merging any number of overlapping intervals.So, if the input is like [[1, 6],[4, 9],[5, 6],[11, 14],[16, 20]], then the output will be 9, as after merging, we have the interval [1, 9] of a length 9.To solve this, we will follow these steps −sort the list intervalsunion := first interval from the intervals listbest := union[end] - union[start] + 1for each start time s and end time e in intervals except the first one, doif s

Program to find length of longest Fibonacci subsequence from a given list in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:14:18

379 Views

Suppose we have a list of strictly increasing positive numbers called nums. We have to find the length of the longest subsequence A (of length minimum 3) such that A[i] = A[i - 1] + A[i - 2] for all i > 1.So, if the input is like nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], then the output will be 6, as we can pick [1, 2, 3, 5, 8, 13].To solve this, we will follow these steps −A := numsn := size of AmaxLen := 0S := a new set ... Read More

Advertisements