ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the only argument. Our function should encode the input string and compare its size with the original string and return the string which is smaller in size.The rule to encode a particular string is −n[s], where the s inside the square brackets is being repeated exactly k times.For instance, ddd can be encoded to 3[d] but 3[d] has a length of 4 whereas ddd is only 3 characters long so our function should eventually return ddd.For example, if the input to the function ... Read More
Convex PolygonA convex polygon is defined as a polygon with all its interior angles less than 180°.ProblemWe are required to write a JavaScript function that takes in an array of coordinates, basically the array will be an array of arrays each subarray containing exactly two numbers, specifying a point on a 2-D plane.Our function should determine whether the polygon formed by these points is a convex polygon or not. If yes, the function should return true, false otherwise.For example, if the input to the function is −const arr = [[0, 0], [0, 1], [1, 1], [1, 0]];Then the output should be ... Read More
Problemconst random7 = () => Math.ceil(Math.random() * 7);Suppose we have the above fat arrow function. This function yields a random number between 0 (exclusive) and 7 (inclusive) everytime we make a call to it.We are required to write a similar random10() JavaScript function that takes no argument and makes no use of the JavaScript library or any third party library. And only making use of this random7() function, our function should return random number between 0 (exclusive) and 10(inclusive).ExampleThe code for this will be − Live Democonst random7 = () => Math.ceil(Math.random() * 7); const random10 = () => { ... Read More
ProblemSuppose we have a S, str. which is an infinite wraparound string of the string −"abcdefghijklmnopqrstuvwxyz".Therefore, S will look like this −"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".We are required to write a JavaScript function that takes in str, let’s call that string str, as the only argument.Our function should find out how many unique non-empty substrings of str are present in S.Our function should finally return the number of different non-empty substrings of str in the string S.For example, if the input to the function is −const str = "zab";Then the output should be −const output = 6;Output ExplanationThere are six substrings "z", "a", "b", ... Read More
To create a histogram with dots instead of bars, we can use the plot function in base R. The x values will be based on the sorting of the vector values and the y values will be based on the sequence of the table for the vector values. Therefore, we would be needing sorting and table with sequence. Check out the below examples to understand how it can be done.Example1> x1 plot(sort(x1),sequence(table(x1)))OutputExample2> x2 plot(sort(x2),sequence(table(x2)))Output
If we have a vector and a data frame, and the data frame has a column that contains the values similar as in the vector then we can create a subset of the data frame based on that vector. This can be done with the help of single square brackets and %in% operator. The %in% operator will help us to find the values in the data frame column that matches with the vector values. Check out the below examples to understand how it works.Example1Consider the below data frame df1 and vector v1 −Live Demo> x1 x2 df1 df1Outputx1 x2 1 ... Read More
To display the average line for y variable using ggplot2, we can use geom_hline function along with the yintercept. In the yintercept, we would need to calculate the mean of the y variable and we can also change the colour of the line using color argument inside the geom_hline function.ExampleConsider the below data frame −Live Demo> x y df dfOutputx y 1 -1.07323904 0.368641641 2 0.92531148 -0.196530651 3 -0.57433739 0.710957804 4 1.17367100 0.300110517 5 0.00769624 -1.287517035 6 0.64901161 -0.476105351 7 0.70197701 -0.683592585 8 -0.80807441 -1.716264317 9 0.10827026 0.116964308 10 -1.10451308 0.660382307 11 -0.01612692 -1.182533283 12 2.20292198 -1.890223763 13 -1.03368161 -0.526983486 ... Read More
To create a vector of random integers we can use the function sample.int and if we want to create the matrix of such integers then matrix function will be used along with it. For example, if we want to create a matrix with random integers between 1 to 100 of size 20 with 4 columns and 5 rows then it can be done by using the below command −matrix(sample.int(100,size=20),nrow=5,ncol=4)Example1Live Demo> M1 M1Output[,1] [,2] [,3] [,4] [1,] 61 8 68 81 [2,] 34 33 40 70 [3,] 76 29 51 41 [4,] 31 77 8 94 [5,] 35 57 50 29 [6,] 96 28 83 3 [7,] 11 68 71 81 [8,] 63 50 94 85 [9,] 21 53 99 94 [10,] 31 67 23 62 [11,] 56 47 68 66 [12,] 56 5 77 27 [13,] 59 95 88 64 [14,] 21 1 86 55 [15,] 8 3 72 17 [16,] 29 41 61 99 [17,] 7 62 48 56 [18,] 80 78 97 57 [19,] 26 96 34 19 [20,] 73 88 57 72Example2Live Demo> M2 M2Output[,1] [,2] [,3] [,4] [,5] [1,] 956 707 421 995 589 [2,] 525 300 595 548 109 [3,] 610 216 754 888 864 [4,] 744 240 997 246 371 [5,] 848 535 477 127 938 [6,] 836 648 241 597 608 [7,] 675 629 517 758 469 [8,] 238 433 296 249 776 [9,] 226 552 933 917 625 [10,] 111 584 643 699 573 [11,] 168 239 409 844 850 [12,] 587 387 587 899 672 [13,] 55 612 315 572 574 [14,] 765 646 925 848 584 [15,] 158 191 235 435 19 [16,] 68 631 493 604 65 [17,] 740 976 498 755 534 [18,] 241 548 921 265 343 [19,] 907 364 318 502 141 [20,] 150 739 614 444 189Example3Live Demo> M3 M3Output[,1] [,2] [1,] 2025 4158 [2,] 1372 4495 [3,] 2208 2306 [4,] 1091 476 [5,] 2635 4873 [6,] 1724 3327 [7,] 580 4051 [8,] 546 3927 [9,] 4115 2399 [10,] 1520 4577 [11,] 420 2441 [12,] 2251 1323 [13,] 2908 1415 [14,] 733 3886 [15,] 3556 844 [16,] 2181 2161 [17,] 2771 2349 [18,] 4805 2057 [19,] 2269 4561 [20,] 3110 1250Example4Live Demo> M4 M4Output[,1] [,2] [,3] [,4] [,5] [1,] 10 7 6 8 4 [2,] 2 3 9 7 6 [3,] 1 6 10 3 9 [4,] 8 1 8 7 3 [5,] 10 6 10 8 3 [6,] 2 3 9 7 10 [7,] 5 9 2 1 5 [8,] 6 4 10 1 7 [9,] 3 5 2 5 5 [10,] 2 9 8 8 7 [11,] 6 5 8 3 9 [12,] 4 1 9 6 7 [13,] 2 10 9 4 5 [14,] 10 5 2 1 2 [15,] 3 10 9 2 10 [16,] 3 6 8 8 7 [17,] 3 2 9 3 4 [18,] 9 3 3 5 4 [19,] 6 3 1 3 2 [20,] 3 7 1 7 5
To convert NaN values to NA, we would need to detect the NaN values in the data frame and the set them to NA. For example, if we have a data frame called df that contains a column x which has some NaN values then we can convert those NaN to NA by using the command df$x[is.nan(df$x)] x1 x2 df1 df1Outputx1 x2 1 NaN -0.44923302 2 NaN -0.12670027 3 1 0.59120380 4 1 -0.18782341 5 NaN -0.28730385 6 1 0.57412261 7 NaN -0.33620181 8 1 1.37168545 9 NaN -2.24121448 10 NaN 1.05990104 11 1 1.95544957 12 NaN -2.19544854 13 1 ... Read More
To highlight a bar in base R histogram, we need to understand the X-axis values and pass the col argument inside hist function appropriately. We just need to put a separate value for the bar that we want to highlight and set the colouring of the rest of the bars to 0 (that is default in base R). Check out the below examples to understand how it works.Example1> x hist(x,col = c(rep(0,5),4,rep(0,5)))OutputExample2> y hist(y,col = c(rep(0,3),4,rep(0,9)))Output
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP