Found 33676 Articles for Programming

How to generate a normal random vector using the mean of a vector in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:32:07

978 Views

To create a normal random vector, we can use rnorm function with mean and standard deviation as well as without passing these arguments. If we have a different vector derived from another distribution or simply represent some numbers then we can use the mean of that vector in the rnorm function for mean argument.Example Live Demoset.seed(101) x1

How to find prime numbers between two values or up to a value in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:30:22

1K+ Views

We know that the prime numbers are the numbers that are not divisible by any number except by themselves or 1 and 1 is not considered as a prime number. In R, we can find the prime numbers up to a number or between two numbers by using Prime function of numbers package. If we want to find the total number of prime numbers between 1 and 10, then we just need to pass 10 inside the Prime function, otherwise range will be required.Loading numbers package −library("numbers")Primes(10)[1] 2 3 5 7Primes(100)[1] 2 3 5 7 11 13 17 19 23 ... Read More

Program to find longest equivalent sublist after K increments in Python

Niharikaa Aitam
Updated on 30-Jun-2025 16:52:55

217 Views

The longest equivalent sublist after K increments is known as the longest contiguous portion of a list where we can make all the elements equal by performing at most K increment operations. Suppose we have a list of numbers called nums and k. Now, consider an operation where we can increment any one element once. If we can perform operations at most k times, we have to find the longest sub-list containing equal elements. Input Output Scenario Let's consider the following scenario, as we can increment 9 once and 6 four times to get the sublist [10, 10, 10], ... Read More

Program to find length of longest distinct sublist in Python

Niharikaa Aitam
Updated on 01-Sep-2025 12:00:56

421 Views

A distinct sublist is a continuous portion of a list in which all the elements are different from each other. The goal is to find the maximum length of such a sublist within a given list of elements. In Python, a list is an ordered and mutable data structure in which sequence of elements are enclosed in square brackets []. The elements in a list can be of any datatype such as integer, float, string etc., and each element in a list has a unique index position which starts from 0. Using set() function The set() is a built-in function ... Read More

How to find the difference between regression line and the points in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:28:47

277 Views

The difference between regression line and the points on the scatterplot are actually the residuals, thus we need to calculate the residual from the model object. This can be simply done by using residuals function. For example, if we create a linear model defined as Model between x and y then the residuals will be found as residuals(Model).Consider the below data frame −Example Live Demoset.seed(999) x1

Program to find length of longest consecutive sequence in Python

Niharikaa Aitam
Updated on 01-Sep-2025 12:00:33

890 Views

A Consecutive sequence is a series of numbers where each number follows the previous one without any missing the order like 1, 2, 3, 4. It is a set of numbers where each number appears in increasing order by 1, even if not adjacent. We can find the Consecutive sequences in data structures of Python such as lists, strings, tuple, etc. Using set() function set() is the built-in function in Python which is used to create a Set data structure. This function is used to eliminate the duplicates from the given input. We can use set() function to find the ... Read More

How to convert rows in an R data frame to list?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:18:07

2K+ Views

Sometimes each row needs to be treated differently, therefore, we might want to convert those rows into list. This will help us to perform the operations on our row elements separately. To convert the rows into list, we can use split function by defining the number of rows in the data frame.Consider the below data frame −Example Live Demoset.seed(101) x1

Program to find length of longest common substring in C++

Arnab Chakraborty
Updated on 10-Oct-2020 12:15:42

2K+ Views

Suppose we have two lowercase strings X and Y, we have to find the length of their longest common substring.So, if the input is like X = "helloworld", Y = "worldbook", then the output will be 5, as "world" is the longest common substring and its length is 5.To solve this, we will follow these steps −Define an array longest of size: m+1 x n+1.len := 0for initialize i := 0, when i

Program to find length of longest common subsequence in C++

Arnab Chakraborty
Updated on 10-Oct-2020 12:05:58

247 Views

Suppose we have two strings text1 and text2, we have to find the length of their longest common subsequence. As we know a subsequence of a string is a new string generated from the original string with some characters deleted without changing the relative order of the remaining characters. (So for example "abe" is a subsequence of "abcde" but "adc" is not). A common subsequence of two strings is a subsequence that is common to both strings. So If there is no common subsequence, return 0. If the input is like “abcde”, and “ace”, then the result will be 3.To ... Read More

How to combine two matrices to create a block-diagonal matrix in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:10:13

878 Views

A block-diagonal matrix means that a matrix added to another matrix at the end the last element. For example, if we have a matrix with nine values and the other matrix also has nine values then the second matrix will be added to the first matrix and the elements below first matrix will be zero and the elements above the second matrix will also be zero.Example Live DemoM1

Advertisements