Programming Articles - Page 1678 of 3366

Program to find the largest sum of the path between two nodes in a binary tree in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:29:23

256 Views

Suppose we have a binary tree; we have to find the maximum sum of any path between any two nodes.So, if the input is likethen the output will be 62 as the nodes are [12, 13, 14, 16, 7].To solve this, we will follow these steps −Define a function utils() . This will take rootif root null, thenreturn 0l := utils(left of root)r := utils(right of root)max_single := maximum of (max of l and r) + value of root) and value of rootmax_top := maximum of max_single and l + r + value of rootres := maximum of res and ... Read More

How to create a subset based on levels of a character column in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:22:03

1K+ Views

In R programming, mostly the columns with string values can be either represented by character data type or factor data type. For example, if we have a column Group with four unique values as A, B, C, and D then it can be of character or factor with four levels. If we want to take the subset of these columns then subset function can be used. Check out the example below.Consider the below data frame −Exampleset.seed(888) Grp

How to check whether a data frame exists or not in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:17:31

2K+ Views

Sometimes we keep writing codes in the programming console and suddenly we need to use something that was used in the upper side of programming console then recalling it becomes a little ambiguous if we forget about it. In this case, we might want to check whether something exists or not and that something could be a data frame in R programming. For this purpose, we can use the below syntax −Syntaxexists("data_frame_name")&&is.data.frame(get("data_frame_name "))Consider the below data frame −Example Live Demoset.seed(101) x1

Program to find largest sum of non-adjacent elements of a list in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:18:41

889 Views

Suppose we have a list of numbers called nums, we will define a function that returns the largest sum of non-adjacent numbers. Here the numbers can be 0 or negative.So, if the input is like [3, 5, 7, 3, 6], then the output will be 16, as we can take 3, 7, and 6 to get 16.To solve this, we will follow these steps−if size of nums

How to find the rank of a matrix in R?

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

8K+ Views

The rank of a matrix is defined as the maximum number of linearly independent vectors in rows or columns. If we have a matrix with dimensions R x C, having R number of rows and C number of columns, and if R is less than C then the rank of the matrix would be R. To find the rank of a matrix in R, we can use rankMatrix function in Matrix package.Loading Matrix package −library(Matrix)Example Live DemoM1

How to find the sum of corresponding elements in multiple vectors even if they contain NA’s in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:05:29

337 Views

If we have multiple vectors then the sum of the corresponding elements can be found by using rowSums functions and the vectors can be combined by using cbind so that R can easily read the corresponding elements. But if there are NA values in one or more vectors then we also need to add na.rm=TRUE argument.Example Live Demoset.seed(100) x1

Program to find sum of contiguous sublist with maximum sum in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:15:29

1K+ Views

Suppose we have an array A. We have to find the contiguous sublist which has the maximum sum, and also return its sum. So if the array A is like A = [-2, 1, -3, 4, -1, 2, 1, -5, 4], then the sum will be 6. And the subarray will be [4, -1, 2, 1].To solve this we will try to use Dynamic programming approach.define an array dp same as the size of A, and fill it with 0dp[0] := A[0]for i := 1 to size of A – 1dp[i] := maximum of dp[i – 1] + A[i] and ... Read More

Program to find the maximum sum of circular sublist in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:04:22

267 Views

Suppose we have a list of numbers nums, now consider a circular list of nums where the start and end of nums are neighbors. We have to find the maximum sum of a non-empty sublist in the circular list.So, if the input is like nums = [2, 3, -7, 4, 5], then the output will be 14, as we can take the sub list [4, 5, 2, 3] which sums to 14.To solve this, we will follow these steps −max_sum := negative infinity, cur_max := 0min_sum := positive infinity, cur_min := 0for each num in nums, docur_max := maximum of ... Read More

How to renumber rows if they are unordered in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:03:32

513 Views

When we create a sample using inbuilt or imported data set then the numbering for selected rows as in the original data set, therefore, the numbering becomes unordered. To change this unordered numbering to a sequence say starting from one to the total number of rows in the sample, we can use 1:nrow(“sample_object_name”).Consider the below data frame −Example Live Demoset.seed(999) x

Program to find k-sized list where difference between largest and smallest item is minimum in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:55:35

228 Views

Suppose we have a list of numbers called nums and an integer k, we have to select elements from nums to create a list of size k such that the difference between the largest integer in the list and the smallest integer is as low as possible. And we will return this difference.So, if the input is like nums = [3, 11, 6, 2, 9], k = 3, then the output will be 4, as the best list we can make is [2, 3, 6].To solve this, we will follow these steps −sort the list numsls := a new listfor ... Read More

Advertisements