Example of how to implement the JS Cartesian Product algorithm and multiple arrays Cartesian Product, js flute

Source: Internet
Author: User

Example of how to implement the JS Cartesian Product algorithm and multiple arrays Cartesian Product, js flute

This article describes how to implement the Javascript Cartesian Product algorithm and multiple arrays Cartesian product. We will share this with you for your reference. The details are as follows:

The implementation code of the js Cartesian Product algorithm, which generates Cartesian Product Based on objects or arrays, and introduces an example of multiple javascript arrays Cartesian product, as well as the java Algorithm and instance code for implementing Cartesian product.

I. javascript Cartesian Product algorithm code

In this example, Cartesian product is generated based on objects or arrays.

// Function descartes (list) {// upper-level index of parent; count pointer count var point ={}; var result = []; var pIndex = null; var tempCount = 0; var temp = []; // generates the pointer object for (var index in list) {if (typeof list [index] = 'object') based on the parameter Column ') {point [index] = {'parent': pIndex, 'Count': 0} pIndex = index;} // if (pIndex = null) is returned for a Single-dimension data structure) {return list;} // dynamically generate the Cartesian product while (true) {for (var index in list) {tempCount = point [index] ['Count']; temp. push (list [index] [tempCount]);} // press the result array result. push (temp); temp = []; // check the maximum pointer value while (true) {if (point [index] ['Count'] + 1> = list [index]. length) {point [index] ['Count'] = 0; pIndex = point [index] ['parent']; if (pIndex = null) {return result ;} // assign a value to parent and check again index = pIndex;} else {point [index] ['Count'] ++; break ;}}}}

Call method:

Var result = descartes ({'A': ['A', 'B', 'C', 'D'], 'bb': ['$ ', '%', '^', '&']}); alert (result); // result is a Cartesian product.

Ii. Implement Multi-array Cartesian Product in js

Example:

<Script> (function () {dwn = function (a) {document. writeln (a + "<br/>")}; // Cartesian Product var Cartesian = function (a, B) {var ret = []; for (var I = 0; I <. length; I ++) {for (var j = 0; j <B. length; j ++) {ret. push (ft (a [I], B [j]) ;}return ret ;}var ft = function (a, B) {if (! (A instanceof Array) a = [a]; var ret = Array. call (null, a); ret. push (B); return ret;} // multiple cartesian products are used together for multiCartesian = function (data) {var len = data. length; if (len = 0) return []; else if (len = 1) return data [0]; else {var r = data [0]; for (var I = 1; I <len; I ++) {r = Cartesian (r, data [I]) ;}return r ;}}})(); var data = [['A', 'B', 'C'], [1, 2, 3, 4], ['A', 'B'], ['#', '@', '+'], ['Mary ', 'terry', 'duration']; var r = multiCartesian (data ); for (var I = 0; I <r. length; I ++) {dwn ("(" + r [I] + ")") ;}</script>

To better understand cartesian products, we recommend a java Cartesian Product Method Tutorial: http://www.bkjia.com/article/129585.htm.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.