Found 33676 Articles for Programming

How to create a bar chart using plotly in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:19:13

403 Views

Plotly in R is a package specifically designed to create highly-interactive and publication-quality charts. The chart can be created by using plot_ly function of the package and there are three main arguments of plot_ly defined as x, y, and type, where x refers to the X-axis, y refers to the Y-axis and type refers to the chart type but the axes values are stored in a data frame or itself a shared.ExampleLoading plotly package:> library(plotly)Consider the below data frame:Live Demo> x count df dfOutputx count 1 A 321 2 B 324 3 C 320 4 D 328Creating the bar plot ... Read More

How to create a covariance matrix in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:15:11

670 Views

To create a covariance matrix, we first need to find the correlation matrix and a vector of standard deviations is also required. The correlation matrix can be found by using cor function with matrix object. For example, if we have matrix M then the correlation matrix can be found as cor(M). Now we can use this matrix to find the covariance matrix but we should make sure that we have the vector of standard deviations.Example1Live Demo> M1 M1Output [, 1] [, 2] [, 3] ... Read More

Why scale_fill_manual does not fill the bar with colors created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:05:40

2K+ Views

We can manually fill the color of bars in a bar plot which is created by using ggplot2 but for this we would be needing color aesthetics already used for the bars. That means we need to use filling of bars inside aes of ggplot2 and then the colors can be mentioned inside the scale_fill_manual function.ExampleConsider the below data frame:Live Demo> x count df dfOutputx count 1 A 321 2 B 324 3 C 320 4 D 328Loading ggplot2 package and creating bar plot:Example> library(ggplot2) > ggplot(df, aes(x, count))+geom_bar(stat="identity")Output:Creating the bar plot by using scale_fill_manual:Example> ggplot(df, aes(x, count))+geom_bar(stat="identity")+scale_fill_manual(values=c("blue", "green", "grey", ... Read More

Android Unlock Patterns in C++

Arnab Chakraborty
Updated on 18-Nov-2020 12:21:48

1K+ Views

Suppose we have an Android 3x3 key lock screen and two integers m and n, the values of m and n are in range 1 ≤ m ≤ n ≤ 9, We have to count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.The rule is like, each pattern must connect at least m keys and at most n keys. All the keys must be unique. If there is a line connecting two consecutive keys in the pattern passes through any other keys, the other keys must ... Read More

Largest BST Subtree in C++

Arnab Chakraborty
Updated on 18-Nov-2020 12:19:14

157 Views

Suppose we have a binary tree; we have to find the largest subtree of it where largest means subtree with largest number of nodes in it.So, if the input is like, then the output will be 3, as the Largest BST Subtree, in this case, is the highlighted one.To solve this, we will follow these steps −Define one structure called data, there will be four values, size, maxVal, minVal, and ok, the ok can hold only true/false valuessolve(TreeNode * node)if node is null, then &miuns;return Data by initializing (0, infinity, -infinity, true)left := solve(left of node)left := solve(right of node)Define ... Read More

Maximum Size Subarray Sum Equals k in C++

Arnab Chakraborty
Updated on 18-Nov-2020 12:15:47

793 Views

Suppose we have an array called nums and a target value k, we have to find the maximum length of a subarray that sums to k. If there is not present any, return 0 instead.So, if the input is like nums = [1, -1, 5, -2, 3], k = 3, then the output will be 4, as the subarray [1, - 1, 5, -2] sums to 3 and is the longest.To solve this, we will follow these steps −ret := 0Define one map mn := size of numstemp := 0, m[0] := -1for initialize i := 0, when i < ... Read More

Number of Connected Components in an Undirected Graph in C++

Arnab Chakraborty
Updated on 18-Nov-2020 12:13:59

2K+ Views

Suppose we have n nodes and they are labeled from 0 to n - 1 and a list of undirected edges, are also given, we have to define one function to find the number of connected components in an undirected graph.So, if the input is like n = 5 and edges = [[0, 1], [1, 2], [3, 4]], then the output will be 2To solve this, we will follow these steps −Define a function dfs(), this will take node, graph, an array called visited, if visited[node] is false, then −visited[node] := truefor initialize i := 0, when i < size ... Read More

Generalized Abbreviation in C++

Arnab Chakraborty
Updated on 18-Nov-2020 12:11:31

268 Views

Suppose there is a word. We have to define a function that can generate the generalized abbreviations of a word.So, if the input is like "word", then the output will be ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]To solve this, we will follow these steps −Define an array retDefine a function solve(), this will take s, idx, if idx >= size of s, then −insert s at the end of retreturny := substring of s from index 0 to idx - 1i := size of ynum := blank stringwhile (i >= ... Read More

Binary Tree Vertical Order Traversal in C++

Arnab Chakraborty
Updated on 18-Nov-2020 12:08:23

396 Views

Suppose there is a binary tree, we have to find the vertical order traversal of its nodes' values. If two nodes are in the same row and column, the order should be from left to right.So, if the input is like, then the output will be [[9], [3, 15], [20], [7]]To solve this, we will follow these steps −Define one map mDefine a function solve(), this will take node, x initialize it with 0, if node is null, then −returnsolve(left of node, x - 1)solve(right of node, x + 1)insert value of node at the end of m[x]From the main ... Read More

Sparse Matrix Multiplication in C++

Arnab Chakraborty
Updated on 18-Nov-2020 12:03:42

3K+ Views

Suppose we have two matrices A and B, we have to find the result of AB. We may assume that A's column number is equal to B's row number.So, if the input is like [[1, 0, 0], [-1, 0, 3]] [[7, 0, 0], [0, 0, 0], [0, 0, 1]], 100-103700000001then the output will be [[7, 0, 0], [-7, 0, 3]]700-703To solve this, we will follow these steps −r1 := size of A, r2 := size of Bc1 := size of A[0], c2 := size of B[0]Define one 2D array ret of order r1 x c2Define an array sparseA[r1] of pairsfor ... Read More

Advertisements