Found 26504 Articles for Server Side Programming

Program to find a list of numbers where each K-sized window has unique elements in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:17:28

285 Views

Suppose we have a list of numbers called nums and another number k, we have to find a list of count of distinct numbers in each window of size k.So, if the input is like nums = [2, 2, 3, 3, 4], k = 2, then the output will be [1, 2, 1, 2], as the windows are [2, 2], [2, 3], [3, 3], and [3, 4].To solve this, we will follow these steps −c := make a dictionary of elements in nums and their frequenciesans := a new listfor i in range k to size of nums, doinsert size ... Read More

Program to find the perimeter of an island shape in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:13:04

503 Views

Suppose we have a binary matrix where 0 shows empty cell and 1 shows a block that forms a shape, now we have to find the perimeter of the shape. The shape will not hold any hole inside it.So, if the input is like0000000111001100111000000then the output will be 14.To solve this, we will follow these steps −d := 0perimeter := 0height := row count of matrixlength := column count of matrixfor each row in matrix, doc := 0for each val in row, doif val is same as 1, thensurround := 4if c is not same as length - 1, thenif ... Read More

Program to interleave list elements from two linked lists in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:01:44

535 Views

Suppose we have two linked lists l1 and l2, we have to return one linked list by interleaving elements of these two lists starting with l1. If there are any leftover nodes in a linked list, they should be appended to the list.So, if the input is like l1 = [5, 4, 6, 3, 4, 7] l2 = [8, 6, 9], then the output will be [5, 8, 4, 6, 6, 9, 3, 4, 7]To solve this, we will follow these steps −ans := l1while l2 is not null, doif ans is not null, thenif next of ans is not ... Read More

Program to count n digit integers where digits are strictly increasing in Python

Arnab Chakraborty
Updated on 09-Oct-2020 13:45:58

258 Views

Suppose we have a number n, we have to find the number of n-digit positive integers such that the digits are in strictly increasing order.So, if the input is like n = 3, then the output will be 84, as numbers are 123, 124, 125, ..., 678,789To solve this, we will follow these steps −if n < 9 is non-zero, thenreturn Combination (9Cn)otherwise,return 0Let us see the following implementation to get better understanding −Example Live Demofrom math import factorial as f class Solution:    def solve(self, n):       if n < 9:          return f(9) / f(n) / f(9 - n)       else:          return 0 ob = Solution() print(ob.solve(3))Input3Output84

Program to check whether we can reach last position from index 0 in Python

Arnab Chakraborty
Updated on 09-Oct-2020 13:40:03

212 Views

Suppose we have a list of numbers called nums where each number shows the maximum number of jumps we can make; we have to check whether we can reach to the last index starting at index 0 or not.So, if the input is like nums = [2, 5, 0, 2, 0], then the output will be True, as we can jump from index 0 to 1, then jump from index 1 to end.To solve this, we will follow these steps−n := size of numsarr := an array of size n and fill with falsearr[n - 1] := Truefor i in ... Read More

How to subset an R data frame based on string values of a columns with OR condition?

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

3K+ Views

We might want to create a subset of an R data frame using one or more values of a particular column. For example, suppose we have a data frame df that contain columns C1, C2, C3, C4, and C5 and each of these columns contain values from A to Z. If we want to select rows using values A or B in column C1 then it can be done as df[df$C1=="A"|df$C1=="B",].Consider the below data frame −Exampleset.seed(99) x1

How to find contingency table of means from an R data frame using cast function?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:20:00

275 Views

The contingency table considers the numerical values for two categorical variables. Often, we require contingency table for counts, especially in non-parametric analysis but it is also possible that we want to use means for our analysis. Hence, we can use cast function from reshape package which solves the problem of creating contingency table easily.Consider the below data frame −Example Live Demoset.seed(99) x1

How to find the number of NA’s in each column of an R data frame?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:19:24

854 Views

Sometimes the data frame is filled with too many missing values/ NA’s and each column of the data frame contains at least one NA. In this case, we might want to find out how many missing values exists in each of the columns. Therefore, we can use colSums function along with is.na in the following manner: colSums(is.na(df)) #here df refers to data frame name.Consider the below data frame −Example Live Demoset.seed(109) x1

How to simulate normal distribution for a fixed limit in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 13:26:24

661 Views

To simulate the normal distribution, we can use rnorm function in R but we cannot put a limit on the range of values for the simulation. If we want simulate this distribution for a fixed limit then truncnorm function of truncnorm package can be used. In this function, we can pass the limits with and without mean and standard deviation.Loading and installing truncnorm package −>install.packages("truncnorm") >library(truncnorm)Examplertruncnorm(n=10, a=0, b=10)[1] 0.76595522 0.33315633 1.29565988 0.67154230 0.04957334 0.38338705 [7] 0.75753005 0.65265304 0.63616552 0.45710877rtruncnorm(n=50, a=0, b=100)[1] 0.904997947 0.035692016 0.402963452 1.001102057 1.445190636 0.109245234 [7] 0.205630845 0.312428027 0.465876772 0.424647787 0.309222394 0.442172805 [13] 0.365503292 1.277570451 0.235747661 1.128447123 ... Read More

How to create a transparent histogram using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 15:23:20

3K+ Views

When we create a histogram using ggplot2 package, the area covered by the histogram is filled with grey color but we can remove that color to make the histogram look transparent. This can be done by using fill="transparent" and color="black" arguments in geom_histogram, we need to use color argument because if we don’t use then the borders of the histogram bars will also be removed and this color is not restricted to black color only.ExampleConsider the below data frame −set.seed(987) x

Advertisements