Add other set operations

This commit is contained in:
d98762625 2018-04-09 11:13:23 +01:00
parent 852c95a994
commit adc4f78e99
12 changed files with 513 additions and 2 deletions

View file

@ -0,0 +1,84 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Utils from "../Utils";
import Operation from "../Operation";
/**
* Set cartesian product operation
*/
class CartesianProduct extends Operation {
/**
* Cartesian Product constructor
*/
constructor() {
super();
this.name = "Cartesian Product";
this.module = "Default";
this.description = "Get the cartesian product of two sets";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Sample delimiter",
type: "binaryString",
value: Utils.escapeHtml("\\n\\n")
},
{
name: "Item delimiter",
type: "binaryString",
value: ","
},
];
}
/**
* Validate input length
* @param {Object[]} sets
* @throws {Error} if not two sets
*/
validateSampleNumbers(sets) {
if (!sets || (sets.length !== 2)) {
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
}
}
/**
* Run the product operation
* @param input
* @param args
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
const sets = input.split(this.sampleDelim);
try {
this.validateSampleNumbers(sets);
} catch (e) {
return e;
}
return Utils.escapeHtml(this.runCartesianProduct(...sets.map(s => s.split(this.itemDelimiter))));
}
/**
* Return the cartesian product of the two inputted sets.
*
* @param {Object[]} a
* @param {Object[]} b
* @returns {String[]}
*/
runCartesianProduct(a, b) {
return Array(Math.max(a.length, b.length))
.fill(null)
.map((item, index) => `(${a[index] || undefined},${b[index] || undefined})`)
.join(this.itemDelimiter);
}
}
export default CartesianProduct;

View file

@ -0,0 +1,91 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Utils from "../Utils";
import Operation from "../Operation";
/**
* Power Set operation
*/
class PowerSet extends Operation {
/**
* Power set constructor
*/
constructor() {
super();
this.name = "Power Set";
this.module = "Default";
this.description = "Generate the power set of a set";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Item delimiter",
type: "binaryString",
value: ","
},
];
}
/**
* Generate the power set
* @param input
* @param args
*/
run(input, args) {
[this.itemDelimiter] = args;
// Split and filter empty strings
const inputArray = input.split(this.itemDelimiter).filter(a => a);
if (inputArray.length) {
return Utils.escapeHtml(this.runPowerSet(inputArray));
}
return "";
}
/**
* Return the power set of the inputted set.
*
* @param {Object[]} a
* @returns {Object[]}
*/
runPowerSet(a) {
// empty array items getting picked up
a = a.filter(i => i.length);
if (!a.length) {
return [];
}
/**
* Decimal to binary function
* @param {*} dec
*/
const toBinary = (dec) => (dec >>> 0).toString(2);
const result = new Set();
// Get the decimal number to make a binary as long as the input
const maxBinaryValue = parseInt(Number(a.map(i => "1").reduce((p, c) => p + c)), 2);
// Make an array of each binary number from 0 to maximum
const binaries = [...Array(maxBinaryValue + 1).keys()]
.map(toBinary)
.map(i => i.padStart(toBinary(maxBinaryValue).length, "0"));
// XOR the input with each binary to get each unique permutation
binaries.forEach((binary) => {
const split = binary.split("");
result.add(a.filter((item, index) => split[index] === "1"));
});
// map for formatting & put in length order.
return [...result]
.map(r => r.join(this.itemDelimiter)).sort((a, b) => a.length - b.length)
.map(i => `${i}\n`).join("");
}
}
export default PowerSet;

View file

@ -63,7 +63,7 @@ class SetDifference extends Operation {
return e;
}
return Utils.escapeHtml(this.runSetDifferencez(...sets.map(s => s.split(this.itemDelimiter))));
return Utils.escapeHtml(this.runSetDifference(...sets.map(s => s.split(this.itemDelimiter))));
}
/**

View file

@ -0,0 +1,97 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Utils from "../Utils";
import Operation from "../Operation";
/**
* Set Symmetric Difference operation
*/
class SymmetricDifference extends Operation {
/**
* Symmetric Difference constructor
*/
constructor() {
super();
this.name = "Symmetric Difference";
this.module = "Default";
this.description = "Get the symmetric difference of two sets";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Sample delimiter",
type: "binaryString",
value: Utils.escapeHtml("\\n\\n")
},
{
name: "Item delimiter",
type: "binaryString",
value: ","
},
];
}
/**
* Validate input length
* @param {Object[]} sets
* @throws {Error} if not two sets
*/
validateSampleNumbers(sets) {
if (!sets || (sets.length !== 2)) {
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
}
}
/**
* Run the difference operation
* @param input
* @param args
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
const sets = input.split(this.sampleDelim);
try {
this.validateSampleNumbers(sets);
} catch (e) {
return e;
}
return Utils.escapeHtml(this.runSymmetricDifference(...sets.map(s => s.split(this.itemDelimiter))));
}
/**
* Get elements in set a that are not in set b
*
* @param {Object[]} a
* @param {Object[]} b
* @returns {Object[]}
*/
runSetDifference(a, b) {
return a.filter((item) => {
return b.indexOf(item) === -1;
});
}
/**
* Get elements of each set that aren't in the other set.
*
* @param {Object[]} a
* @param {Object[]} b
* @return {Object[]}
*/
runSymmetricDifference(a, b) {
return this.runSetDifference(a, b)
.concat(this.runSetDifference(b, a))
.join(this.itemDelimiter);
}
}
export default SymmetricDifference;

View file

@ -5,11 +5,13 @@
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import CartesianProduct from "./CartesianProduct";
import FromBase32 from "./FromBase32";
import FromBase64 from "./FromBase64";
import FromHex from "./FromHex";
import Gunzip from "./Gunzip";
import Gzip from "./Gzip";
import PowerSet from "./PowerSet";
import RawDeflate from "./RawDeflate";
import RawInflate from "./RawInflate";
import SetDifference from "./SetDifference";
@ -17,6 +19,7 @@ import SetIntersection from "./SetIntersection";
import SetOps from "./SetOps";
import SetUnion from "./SetUnion";
import ShowBase64Offsets from "./ShowBase64Offsets";
import SymmetricDifference from "./SymmetricDifference";
import ToBase32 from "./ToBase32";
import ToBase64 from "./ToBase64";
import ToHex from "./ToHex";
@ -26,11 +29,13 @@ import ZlibDeflate from "./ZlibDeflate";
import ZlibInflate from "./ZlibInflate";
export {
CartesianProduct,
FromBase32,
FromBase64,
FromHex,
Gunzip,
Gzip,
PowerSet,
RawDeflate,
RawInflate,
SetDifference,
@ -38,6 +43,7 @@ export {
SetOps,
SetUnion,
ShowBase64Offsets,
SymmetricDifference,
ToBase32,
ToBase64,
ToHex,