Found 33676 Articles for Programming

How to combine lists of data frames in R?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:03:42

459 Views

If we have a list of data frames and the size of those data frames is same then we might want to combine the lists so that the data frames can be combined. This can be done by using mapply function along with cbind. For example, if we have two lists of data frames defined as List1 and List2 then we can combine them using the command −mapply(cbind, List1, List2, SIMPLIFY=FALSE).ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output      x1        x2 1   0.2378371  0.51433808 2   0.0638975 -1.66077353 3   0.3987209  0.68480587 ... Read More

How to increase the space between horizontal legends using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 09:58:35

3K+ Views

Generally, the space between two legend entries is not large enough and it becomes difficult to read the legend names if the names are long. In this case, we need to increase the margin between the legend entries/names but this would be required when the legends are horizontally aligned as vertical legends can be read as it is. For this purpose, we can use legend.text argument inside theme function of ggplot2 package.ExampleConsider the below data frame −Live Demo> x y df dfOutput x y 1 Male 501 2 Female 520Loading ggplot2 ... Read More

How to add a row in an R data frame at a specific place?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 09:53:56

563 Views

The data collected for the first time is utilised as it is but when we need to go for secondary data to conduct the same or similar study again, we can use new data as well as the primary data. In this type of situations, we might want to randomly organize data rows that includes new and old data. Also, there is a possibility of missing data row which is found at later stage in the study then it can be also added. Hence, a row might be required to added in the existing data frame. This can be done ... Read More

Check if each internal node of a BST has exactly one child in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:52:26

301 Views

Suppose we have the preorder traversal of a binary search tree (BST). We have to check whether each internal node has only one child or not.So, if the input is like preorder = [22, 12, 13, 15, 14], then the output will be True as BST is like −To solve this, we can follow one efficient approach. As all decedents of a node is either smaller or larger, then we can we can follow these steps −Get the next preorder successor of the nodeGet the last preorder successor of the nodeNow when both the successors are less than or greater ... Read More

Check if difference of areas of two squares is prime in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:50:47

269 Views

Suppose we have two numbers x and y. We have to check whether difference of their areas is prime or not.So, if the input is like x = 7, y = 6, then the output will be True as the difference of their square is 49 - 36 = 13 which is prime.To solve this, we will follow these steps −if (x + y) is prime number and (x - y) is 1, thenreturn Trueotherwise,return FalseLet us see the following implementation to get better understanding −Example Live Demodef is_prime(num) :    if num

Check if Decimal representation of an Octal number is divisible by 7 in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:48:54

199 Views

Suppose we have one octal number. We have to check whether the decimal representation of the given octal number is divisible by 7 or not.So, if the input is like n = 61, then the output will be True as the decimal representation of 61 is 6*8 + 1 = 48 + 1 = 49 which is divisible by 7.So, if the input is like n = 61, then the output will be True as the decimal representation of 61 is 6*8 + 1 = 48 + 1 = 49 which is divisible by 7.To solve this, we will follow ... Read More

Check if count of divisors is even or odd in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:47:18

832 Views

Suppose we have a number n, we have to find its total number of divisors are even or odd.So, if the input is like n = 75, then the output will be Even, as the divisors are [1, 3, 5, 15, 25, 75].To solve this we shall follow one simple and efficient approach. We have observed that when a number is perfect square then only it has odd number of divisors. So if the number is not perfect square then it will have even divisors. So here we will only check whether the number is perfect square or not and ... Read More

Check if concatenation of two strings is balanced or not in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:45:41

367 Views

Suppose we have two bracket sequences s and t with only these characters '(' and ')'. We have to check whether the concatenated string of s and t is balanced or not. The concatenation can be done by s | t or t | s.So, if the input is like s = "()()))", t = "()(()(", then the output will be True because if we concatenate t | s, then we will get "()(()(()()))", which is balanced.To solve this, we will follow these steps −Define a function is_balanced_parenthesis() . This will take stringstack := a new listfor i in range ... Read More

Check if characters of one string can be swapped to form other in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:43:28

178 Views

Suppose we have two strings s and t, we have to check whether we can generate t by swapping the character of the s.So, if the input is like s = "worldlloeh" t = "helloworld", then the output will be True as we can swap characters from "worldlloeh" to make "helloworld".To solve this, we will follow these steps −s_len := size of s, t_len := size of tif s_len is not same as t_len, thenreturn Falsefreq := a map to store all characters and their frequencies in sfor i in range 0 to t_len, dofreq[t[i]] := freq[t[i]] - 1if freq[t[i]] ... Read More

Check if characters of a given string can be rearranged to form a palindrome in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:41:36

1K+ Views

Suppose we have a string s, we have to check whether characters of the given string can be shuffled to make a palindrome or not.So, if the input is like s = "raaecrc", then the output will be True as we can rearrange this to "racecar" which is a palindrome.To solve this, we will follow these steps −freq := a map to store all characters and their frequencies in sodd_count := 0for each element i in the list of all values of freq, doif i is odd, thenodd_count := odd_count + 1if odd_count > 1, thenreturn Falsereturn TrueLet us see ... Read More

Advertisements