Finding Square Root of a Non-Negative Number Without Using Math.sqrt in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:52:47

334 Views

We are required to write a JavaScript function that takes in a non-negative Integer and computes and returns its square root. We can floor off a floating-point number to an integer.For example: For the number 15, we need not to return the precise value, we can just return the nearest smaller integer value that will be 3, in case of 15We will make use of the binary search algorithm to converse to the square root of the given number.The code for this will be −Exampleconst squareRoot = (num = 1) => {    let l = 0; let r = ... Read More

Reversing Vowels in a String using JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:51:39

388 Views

We are required to write a JavaScript function that takes a string as input and reverse only the vowels of a string.For example −If the input string is −const str = 'Hello';Then the output should be −const output = 'Holle';The code for this will be −const str = 'Hello'; const reverseVowels = (str = '') => {    const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);    let left = 0, right = str.length-1;    let foundLeft = false, foundRight = false;    str = str.split(""); while(left < right){       if(vowels.has(str[left])){   ... Read More

Perform Chi-Square Test for Goodness of Fit in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:50:53

4K+ Views

The chi square test for goodness of fit is a nonparametric test to test whether the observed values that falls into two or more categories follows a particular distribution of not. We can say that it compares the observed proportions with the expected chances. In R, we can perform this test by using chisq.test function. Check out the below examples to understand how it is done.Example1Live Demo> x1 x1Output[1] 9 4 1 9 6 6 1 6 0 0 5 8 8 3 7 8 0 3 3 9 6 0 3 8 2 0 8 5 9 1 3 ... Read More

Find Sets of Numbers Using Defined Edges in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:49:44

100 Views

Consider the following input and output arrays −const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [    [0, 1, 3],    [4, 5, 6, 8] ];Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined.That is, in graph theory terms, find the distinct connected components within such a graph. For instance, in the above arrays, there is no way to travel from 4 to 0 so they are ... Read More

Replace Substring with Its Reverse in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:48:52

137 Views

The chartr function in base R helps us to replace old strings with new strings and hence it can be also used to replace a subs-string with the reverse of that substring. For example, if we have a vector say x that contains tutorialpsoint and we want to convert it to tutorialspoint then it can be done as chartr("tutorialpsoint ", " tutorialspoint ", x).Example1Live Demo> x1 x1Output[1] "IDNIA"Example> chartr("DN", "ND", x1)Output[1] "INDIA" Example2Live Demo> x2 x2Output[1] "IDNIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNONESIA" [7] "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" "IDNONESIA" [13] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" [19] "IDNONESIA" "IDNONESIA" "IDNIA" ... Read More

Find Matches of Single Array in Multidimensional Array Using JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:47:48

149 Views

We are required to write a JavaScript function that takes in an array of arrays of Numbers as the first argument and an array of Numbers as the second argument. The function should pick a subarray from each array of the first array, (subarray that contains item common to both the second array and the corresponding array of first array.)For example −If the inputs are −Exampleconst arr1 = [ [1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12] ]; const arr2 = [9, 11, 13, 15, 1, 2, 5, ... Read More

Create a Vector of Lists in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:46:58

522 Views

If we have many lists but we want to use the values in the lists as a vector then we first need to combine those lists and create a vector. This can be done by using unlist function along with the combine function c to create the vector. For example, if we have two lists defined as List1 and List2 and we want to create a vector V using these lists then it can be created as:V x1 x1Output$a [1] -0.6972237 -1.5013768 -0.2451809 -0.2365569 -1.6304919 -1.1704378 [7] 1.1617054 -0.2349498 -1.2582229 0.4112065 $b [1] 2 0 2 6 0 0 ... Read More

Find Longest Repeating Series of Numbers in Array with JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:45:29

590 Views

We are required to write a JavaScript function that takes in an array of Numbers that may contain some repeating elements. The function should return the length of the longest repeating number sequence from the array.For example −If the input array is −const arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1];Then the output should be 3 because the number 2 is repeated 3 times consecutively in the array (and that's the highest number).Exampleconst arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; const findLongestSequence = (arr = []) => {    const res ... Read More

Sort Array Based on Another Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:44:03

421 Views

Suppose, we have two arrays like these −const input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"];We are required to write a JavaScript function that takes in two such arrays as first and second argument respectively.The function should sort the elements of the first array according to their position in the second array.The code for this will be −Exampleconst input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"]; const sortByReference = (arr1 = [], arr2 = ... Read More

Create Histogram with Main Title in Outer Margin of Plot Window in Base R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:43:53

2K+ Views

The main title of a histogram in base R can be inserted by using title function and if we want to have it in the outer margin then outer argument must be set to TRUE. If the outer argument does not fulfil our requirement then we can use par function to adjust the outer margin area and create the histogram. Check out the below example to understand how it works.Example> x hist(x) > title('Normal Distribution',outer=TRUE)OutputExample> par(oma=c(0,0,2,0)) > hist(x) > title('Normal Distribution',outer=TRUE)Output

Advertisements