Find K-Sized List with Minimum Difference Between Largest and Smallest Item in Python

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

218 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

Create Two Plots Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:54:43

675 Views

The two plots created by using ggplot2 can be arranged in a vertical manner with the help gridExtra package, we simply needs to use grid.arrange function to do so. For example, if we have two plots created by using ggplot2 and saved in objects p1 and p2 then they can be vertically arranged as grid.arrange(p1,p2)Consider the below data frame −Exampleset.seed(151) x

Find Largest Grouping of Anagrams from a Word List in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:52:52

237 Views

Suppose we have a list of strings words, we have to group all anagrams together and return the size of the largest grouping.So, if the input is like words = ["xy", "yx", "xyz", "zyx", "yzx", "wwwww"], then the output will be 3, as ["xyz", "zyx", "yzx"] is the largest grouping.To solve this, we will follow these steps −lookup := a new map, initially emptyres := 0for each i in words, dop := sort i in lexicographical wayif p is in lookup, increase count, otherwise 1res := maximum of res and lookup[p]return resLet us see the following implementation to get better ... Read More

Create Barplot from Data Frame in R Using Rows as Categories

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:51:45

7K+ Views

If we have small number of rows then we might want to create bar plot for rows instead of using columns as categories. This can be done by using barplot function but we need to convert the data frame to matrix and take the transpose of it. For example, if we have a data frame data_frame with 4 rows and 4 columns, then the barplot with rows as categories can be created as barplot(t(as.matrix(data_frame)),beside=TRUE)Consider the below data frame −Example Live Demox1

Split String Values Containing Special Characters in R

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:48:49

1K+ Views

When we have a single long string or a vector of string values and the values within the string are separated by some special characters then splitting the values can help us to properly understand those strings. This could happen in situations when the string data is recorded with mistakes or have some other purpose. We can do the splitting using strsplit function.Example Live Demox1

Find Minimum Required Chances to Form a String with K Unique Characters in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:47:31

466 Views

Suppose we have a string s of lowercase alphabet characters, and another number k, we have to find the minimum number of required changes in the string so that the resulting string has at most k distinct characters. In this case The change is actually changing a single character to any other character.So, if the input is like s = "wxxyyzzxx", k = 3, then the output will be 1, as we can remove the letter "w" to get 3 distinct characters (x, y, and z).To solve this, we will follow these steps −count := a map of each character ... Read More

Plot All Values of an R Data Frame

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:46:43

447 Views

To plot all the values of an R data frame, we can use matplot function. This function plots all the values based on the columns of an R data frame and represent them by the column number. For example, if we have five columns in an R data frame then matplot will represent the first column by 1, second column by 2, third column by 3 and so on.Consider the below data frame −Example Live Demoset.seed(555) v1

Find Proportion of Row Values in an R Data Frame

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:42:06

538 Views

The proportion of row values can be calculated if we divide each row value with the sum of all values in a particular row. Therefore, the total sum of proportions will be equal to 1. This can be done by dividing the data frame with the row sums and for this purpose we can use the below syntax −Syntaxdata_frame_name/rowSums(data_frame_name)Consider the below data frame −Example Live Demoset.seed(111) x1

Find the Kth Missing Number from a List in Python

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

542 Views

Suppose we have a list of sorted unique numbers called nums and an integer k, we have to find the kth missing number from the first element of the given list.So, if the input is like nums = [5, 6, 8, 10, 11], k = 1, then the output will be 9, as 9 is the second (index 1) missing number.To solve this, we will follow these steps −for i in range 1 to size of nums, dodiff := nums[i] - nums[i - 1] - 1if k >= diff, thenk := k - diffotherwise, return nums[i - 1] + k ... Read More

Create Transparent Polygon Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:30:57

780 Views

A transparent polygon just represents the border lines and a hollow area; thus, we can only understand the area covered but it becomes a little difficult to understand the scales. Hence, this visualisation technique is not as useful as others that fills the area with a different color. But it could be used if the range of the data is not large.Consider the below data frame −Example Live Demoset.seed(123) x

Advertisements