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
A binary matrix contains values such as Yes or NO, 1 or 0, or any other two values that represents opposite mostly and the globally accepted logical values are FALSE and TRUE. Therefore, to convert a binary matrix to logical matrix, we can use ifelse function and convert the one category of binary variable to appropriate logical value and for the rest returns the left-out value. This is a very easy task in R, check out the below examples to understand how it can be done.Example1Live Demo> M1 M1Output[, 1] [, 2] [1, ] "No" "Yes" [2, ] "No" "No" ... Read More
To split string vector elements, we can use strsplit function. And if we want to extract the string elements after splitting then double and single square brackets will be used. The double square bracket will extract the string vector element and the single square will extract the element after splitting. Check out the examples to understand how it works.Example1Live Demo> x1 x1Output[1] "Tutorialspoint is an E-learning platform" [2] "E-learning is important" [3] "It helps in learning and growing at a faster rate"Example> x1 x1Output[[1]] [1] "Tutorialspoint" "is" "an" "E-learning" [5] "platform" [[2]] [1] "E-learning" "is" "important" [[3]] [1] ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP