Sort in Multi-Dimensional Arrays in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:03:57

744 Views

Suppose, we have the following array of arrays −const arr = [ ["A", "F", "A", "H", "F", "F"],  ["F", "A", "A", "F", "F", "H"] ];We are required to write a JavaScript function that takes in one such array.The function should sort all the subarrays of the given array internally according to these rules −If the elements are not either "A" or "F", they should maintain their positionIf the element is either of "A" or "F", they should be sorted alphabeticallyTherefore, the final output for the above array should look like −const output = [ ["A", "A", "A", "H", "A", "F"], ... Read More

Finding Perfect Numbers in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:02:18

2K+ Views

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.For example −28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number.Exampleconst num = 28; const checkPerfectNumber = (num = 1) => {    if(num === 1) {       return false;    }; ... Read More

Difference Between & and | in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:01:56

418 Views

If we have a data frame defined as df that contains column x, y, and z then extraction of these columns from df can be done by using df$x, df$y, and df$z. On the other hand, if we have an S4 object defined as Data_S4 that contains column x, y, and z then the extraction of these columns can be done by using Data_S4@x, Data_S4@y, and Data_S4@z.Example of a data frame:ExampleLive Demo> x1 x2 df dfOutput x1 x2 1 4 2 2 7 0 3 10 2 4 3 1 5 7 1 6 2 2 7 3 4 8 ... Read More

Remove Border of Bars from Histogram in Base R

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

848 Views

By default, a histogram drawn in base R has black color borders around bars. We might want to remove these black borders to make the histogram visually smooth. This can be done by using lty argument with the hist function. For example, if we have a vector x and we want to create a histogram of x without border of bars then we can use the argument as hist(x,lty="blank").Example> x hist(x)Output:Creating histogram of x without border of bars:Example> hist(x,lty="blank")Output:

Shift Certain Elements to the End of Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:58:26

358 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument.Our function should check for all the instances of second number in the array, if there exists any, the function should push all those instances to the end of the array.If the input array is −const arr = [1, 5, 6, 6, 5, 3, 3];And the second argument is 6Then the array should become −const output = [1, 5, 5, 3, 3, 6, 6];Exampleconst arr = [1, 5, 6, 6, 5, 3, 3]; const ... Read More

Reverse the Order of Bits in a Given Integer Using JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:57:04

290 Views

We are required to write a JavaScript program that reverses the order of the bits in a given integer.For example −56 -> 111000 after reverse 7 -> 111Another example,234 -> 11101010 after reverse 87 -> 1010111Exampleconst num1 = 789; const num = 43 const reverseBits = (num = 1) => {    const str = num.toString(2);    const arr = str.split('').reverse();    const arrStr = arr.join('');    const reversedNum = parseInt(arrStr, 2);    return reversedNum; } console.log(reverseBits(num)); console.log(reverseBits(num1));OutputAnd the output in the console will be −53 675

Create S4 Object in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:57:01

1K+ Views

To create an S4 object, we can use setClass function where we will pass the object name, column names, and the type of the data that will be stored in the columns. For example, if we want to create an S4 with name data and two numerical columns called by x and y then we can use setClass("data", representation(x1="numeric", x2="numeric")).Example1> setClass("data1", representation(x1="numeric", x2="numeric")) > data1 data1OutputAn object of class "data1" Slot "x1": [1] -0.586187627 0.853689097 -0.602612795 -2.194235741 -1.318522292 [6] -0.984882420 0.273584140 0.364691611 1.025472248 1.198547297 [11] -0.709282551 -0.001441127 -0.201348012 1.296811172 1.520093861 [16] 2.071031215 0.472877022 0.616211695 0.642165615 -0.122773000 Slot "x2": [1] ... Read More

Subset Nth Row from an R Data Frame

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:55:47

2K+ Views

We can find subsets using many ways in R and the easiest way is to use single-square brackets. If we want to subset a row or a number of consecutive or non-consecutive rows then it can be directly done with the data frame name and the single-square brackets. For example, if we have a data frame called df and we want to subset 1st row of df then we can use df[1, ] and that’s it.ExampleConsider the below data frame:Live Demo> set.seed(214) > x y z a b c q w df1 df1Outputx y z a b c q w ... Read More

Length of the Longest Possible Palindrome String in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:54:14

416 Views

Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here.For example −If the input string is −const str = "abccccdd";then the output should be 7, because, one longest palindrome that can be built is "dccaccd", whose length is 7.Exampleconst str = "abccccdd"; const longestPalindrome = (str) => {    const set = new Set();    let count = 0;    for (const char of str) {     ... Read More

Different Types of Point in geom_point of ggplot2 Package in R

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

258 Views

We can create a point chart using ggplot2 package but that point not necessarily to be in circular shape, we have twenty-five shape options for those points in ggplot2. While creating a point chart using ggplot2, we can use shape argument inside geom_point to see the difference among these twenty-five shapes.ExampleConsider the below data frame:Live Demo> set.seed(1957) > x y df dfOutput x y 1 0.7028704 1.6664500 2 0.9672393 1.0456639 3 1.3102736 0.2495795 4 0.3389941 0.2141513 5 0.5867095 0.4417377 6 0.4257543 0.6533757 7 0.9106756 0.3611954 8 1.0444729 1.3770588 ... Read More

Advertisements