Intersection
Posted by Dustin Boston in Algorithms.
The calculateIntersectionWeight
function computes the intersection weight
between two arrays of strings by determining the ratio of the size of their
intersection to the length of the smaller array. It relies on the
getIntersection
function to find the common elements between the two arrays,
ensuring an efficient and straightforward implementation. The getIntersection
function uses sets to identify and return a list of unique shared elements,
removing duplicates and simplifying comparison. Together, these functions handle
edge cases, such as empty arrays, by returning 0 when appropriate to avoid
division by zero, making them robust and easy to use for set-based comparisons.
Source Code Listing
code.ts
// @example Usage
// const a = ['apple', 'banana', 'cherry'];
// const b = ['banana', 'cherry', 'date'];
//
// const intersection = getIntersection(a, b);
// console.log(intersection); // ['banana', 'cherry']
//
// const weight = calculateIntersectionWeight(a, b);
// console.log(weight); // 2 / 3 = 0.666...
export function calculateIntersectionWeight(
aTerms: string[],
bTerms: string[],
): number {
if (aTerms.length === 0 || bTerms.length === 0) {
return 0;
}
const intersection = getIntersection(aTerms, bTerms);
// Calculate weight based on the size of the intersection
const weight = intersection.length / Math.min(aTerms.length, bTerms.length);
return weight;
}
export function getIntersection(a: string[], b: string[]): string[] {
const aSet = new Set(a);
const bSet = new Set(b);
return [...aSet].filter((x) => bSet.has(x));
}
test.ts
import {describe, it, expect} from "bun:test";
import {calculateIntersectionWeight, getIntersection} from "./code.ts"; // Replace with the actual path
describe("Intersection", () => {
it("Identical Arrays (1)", () => {
const aTerms = ["apple", "banana", "cherry"];
const bTerms = ["apple", "banana", "cherry"];
const result = calculateIntersectionWeight(aTerms, bTerms);
expect(result).toBe(1);
});
it("Disjoint Arrays (0)", () => {
const aTerms = ["apple", "banana", "cherry"];
const bTerms = ["date", "fig", "grape"];
const result = calculateIntersectionWeight(aTerms, bTerms);
expect(result).toBe(0);
});
it("Partial Intersection", () => {
const aTerms = ["apple", "banana", "cherry"];
const bTerms = ["banana", "cherry", "date"];
const result = calculateIntersectionWeight(aTerms, bTerms);
expect(result).toBe(2 / 3);
});
it("Duplicate Items", () => {
const aTerms = ["apple", "banana", "banana", "cherry"];
const bTerms = ["banana", "cherry", "cherry", "date"];
const result = calculateIntersectionWeight(aTerms, bTerms);
expect(result).toBe(2 / 4); // Intersection size is 2, min length is 4
});
it("One Empty Array (0)", () => {
const aTerms: string[] = [];
const bTerms = ["apple", "banana", "cherry"];
const result = calculateIntersectionWeight(aTerms, bTerms);
expect(result).toBe(0);
});
it("Two Empty Arrays (0)", () => {
const aTerms: string[] = [];
const bTerms: string[] = [];
const result = calculateIntersectionWeight(aTerms, bTerms);
expect(result).toBe(0);
});
it("Case Sensitive", () => {
const aTerms = ["apple", "Banana", "cherry"];
const bTerms = ["Apple", "banana", "Cherry"];
const result = calculateIntersectionWeight(aTerms, bTerms);
expect(result).toBe(0); // Case-sensitive comparison
});
it("Subsets", () => {
const aTerms = ["apple", "banana"];
const bTerms = ["apple", "banana", "cherry", "date"];
const result = calculateIntersectionWeight(aTerms, bTerms);
expect(result).toBe(2 / 2); // Intersection size is 2, min length is 2
});
it("Two Arrays", () => {
const aTerms = ["apple", "banana", "cherry"];
const bTerms = ["banana", "cherry", "date"];
const result = getIntersection(aTerms, bTerms);
expect(result).toEqual(["banana", "cherry"]);
});
it("Disjoint Result", () => {
const aTerms = ["apple", "banana", "cherry"];
const bTerms = ["date", "fig", "grape"];
const result = getIntersection(aTerms, bTerms);
expect(result).toEqual([]);
});
it("Removes Duplicates", () => {
const aTerms = ["apple", "banana", "banana", "cherry"];
const bTerms = ["banana", "cherry", "cherry", "date"];
const result = getIntersection(aTerms, bTerms);
expect(result).toEqual(["banana", "cherry"]);
});
});