Partitioning in JavaScript

Partitioning in JavaScript

If using Underscore.js, you can implement this with groupBy() and values()

function partition(items, size) {
var result = _.groupBy(items, function(item, i) {
return Math.floor(i/size);
});
return _.values(result);
}

(This is less ugly in CoffeeScript.)

jsFiddle: http://jsfiddle.net/MW3BS/

How to create a partition function in javascript. using the following guidelines

Here is a version which uses reduce:

function partition(arr, filter) {
return arr.reduce(
(r, e, i, a) => {
r[filter(e, i, a) ? 0 : 1].push(e);
return r;
}, [[], []]);
}

Here's an alternative version which uses Array#filter to find the matches, and builds an array of non-matches as it goes along:

function partition(arr, filter) {
var fail = [];

var pass = arr.filter((e, i, a) => {
if (filter(e, i, a)) return true;
fail.push(e);
});

return [pass, fail];
}

Understanding the event loop and partitioning in Node.js

then the poll should be empty but the setImmediate command, and therefore it should keep running the recursive, so how would it give opportunity to other code to be executed meanwhile?

Because setImmediate schedules callbacks to happen just after the poll phase completes. Those callbacks are run during the check phase. Adding more callbacks during check schedules them, but doesn't cause them to run; that happens the next time the loop gets to the poll phase, sees there are pending callbacks, and enters the check phase.

Why for really big n there isn't RangeError: Maximum call stack size exceeded error

Because the recursion isn't immediate, so it doesn't consume stack space. setImmediate just schedules a callback, it doesn't call the callback. Your code ends, releasing its stack resources, and the event loop continues. Later, the callback you scheduled via setImmediate gets run, briefly using a small bit of stack space.

In contrast, when you call help immediately, that's allocating a new stack frame, and if you use a big n, you allocate too many stack frames and run out of stack.

numpy.partition in JavaScript

Package to do quickselect.

Github

NPM

(I've never used this package, but from the README, it seems to be what OP is looking for)

Partitioning weighted elements with a restriction on total partition weight

This is a bin-packing problem and is known to be NP-hard.

For a quick approximation I would suggest sorting from largest to smallest, and then putting each element in the bin that it fits in which is closest to full.

Partition data from array and store in variable (Javascript / JSON)

We should be able to assign the first element of the info array to the requiredPayload variable, like so:

 
const payload = {
"body": {
"info": [
{
"campaign_id": "",
"recipient": {
"first_name": "",
"last_name": "",
"company": "",
"email": "",
"address_1": "",
"city": "",
"state": "",
"postal_code": "",
"identifier": ""
}
}
],
"otherArray": [],
"otherString": "",
"otherString2": ""
}
};

var requiredPayload = payload.body.info[0];
console.log("requiredPayload:", requiredPayload);

Partition Javascript array of primitive types into multiple parts

You may use lodash function chaining like

var array = [404,101,102,103,104,201,203,204,303,301,302,405,406,408];
var newArray = _(array)
.groupBy(function (x) { return x / 100 | 0; })
.values()
.value();

Is this right way to solve the array partition problem?

The trick is to first sort the array, and then take the sum of every number with an even index (i.e. the smallest number in each pair).

var arrayPairSum = function(nums) {
nums.sort((a, b) => a - b);
let sum = 0;

for(let i = 0; i < nums.length; i += 2) {
sum += nums[i];
}

return sum;
};

Tested on LeetCode:

Sample Image

Array partition based on 'contain'

From first approach, I think it is because _.contains match exact value between array and the targeted value.

console.log(_.contains(selected, "123-foo")); // false

My solution will use some method from Array.

var partitioned = _.partition(array, value => selected.some(select => value.indexOf(select) > -1));
console.log(partitioned);

I saw underscore.js also has _.some method.



Related Topics



Leave a reply



Submit