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
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
The different types of keys in DBMS are −Candidate Key - The candidate keys in a table are defined as the set of keys that is minimal and can uniquely identify any data row in the table.Primary Key - The primary key is selected from one of the candidate keys and becomes the identifying key of a table. It can uniquely identify any data row of the table.Super Key - Super Key is the superset of primary key. The super key contains a set of attributes, including the primary key, which can uniquely identify any data row in the table.Composite Key - If ... Read More
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
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
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
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
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
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
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