Server Side Programming Articles - Page 1530 of 2650

Program to find leftmost deepest node of a tree in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:34:00

390 Views

Suppose we have a binary tree; we have to find the value of the deepest node. If there are more than one deepest node, then return the leftmost deepest node.So, if the input is likethen the output will be 4 as 4 and 7 are deepest but 4 is left most.To solve this, we will follow these steps −queue := a queue with one node root.left_max := value of rootwhile size of queue > 0, dolevel_size := size of queuefor i in range 0 to level_size, dotemp := first element from queue and delete itif i is same as 0, ... Read More

Program to check whether all leaves are at same level or not in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:28:22

172 Views

Suppose we have a binary tree; we have to check whether all leaves are at the same level or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs() . This will take root, dif root is not null, thenif left of root is null and right of root is null, theninsert d at the end of depthotherwise, dfs(left of root, d + 1)dfs(right of root, d + 1)From the main method, do the following −depth := a new listdfs(root, 0)return true when depth has only one valueLet ... Read More

How to create a matrix using vector of string values in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:40:50

2K+ Views

You might have heard that matrix can contain only numerical values but it is also possible to create a matrix with string values, and of course calculations using these types of matrices would not be possible. To create a matrix using string values, we can first create a vector of strings then define its dimension with dim function and that will convert the vector into matrix of string values.Example Live DemoM1

How to find the position of one or more values in a vector into another vector that contains same values in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:33:10

739 Views

Finding the position of one of more values that are common in two vectors can be easily done with the help of match function. The match function will match the values in first and second vector then return the index or position of these common values in second vector.Example Live Demoset.seed(145) x1

Program to check whether we can fill square where each row and column will hold distinct elements in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:39:48

105 Views

Suppose we have one n × n matrix containing values from 0 to n. Here 0 represents an unfilled square, we have to check whether we can fill empty squares such that in each row and each column every number from 1 to n appears exactly once.So, if the input is like002201123then the output will be True, as we can set the matrix to312231123To solve this, we will follow these steps −Define a function find_empty_cell() . This will take matrix, nfor i in range 0 to n, dofor j in range 0 to n, doif matrix[i, j] is same as ... Read More

How to plot values with log scales on x and y axis or on a single axis in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:30:33

4K+ Views

We can plot numerical values in R with many scales and that includes log scale as well. Also, it is possible to plot the values with log scales on both the axes. In base R, the best way to do this is defining the axes values with decimal representation as shown in the below examples with well-defined log.Consider the below vector −Example Live Demoset.seed(555) x

How to set a level of a factor column in an R data frame to NA?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:28:27

745 Views

In data analysis, we often face inappropriate data and hence the data analysis becomes difficult. An example of inappropriate data is reading missing values with a different value by naming them as Missing or Not Available. It can be done by using below syntax −Syntaxlevels(“data_frame_name”$”Column_name”)[levels(“data_frame_name”$”Column_name”=="Missing"]

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

Advertisements