From 86671367aae573f3f6fd31ea2ce08a8d8561bd22 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Thu, 14 Mar 2024 20:13:15 -0700 Subject: [PATCH] De-duplicate permutations output Otherwise, a 4-word grid will have duplicate outputs --- main.ts | 2 +- main_test.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index 2f77e63..824d803 100644 --- a/main.ts +++ b/main.ts @@ -45,7 +45,7 @@ export function getPermutations( horizontalTB[horizontalIndex] = word; } - return [verticalLR.join(" "), horizontalTB.join(" ")]; + return Array.from(new Set([verticalLR.join(" "), horizontalTB.join(" ")])); } /** diff --git a/main_test.ts b/main_test.ts index fc6d33c..370808f 100644 --- a/main_test.ts +++ b/main_test.ts @@ -67,6 +67,13 @@ User enters: getPermutations should return: [ "1 2 3 4 5 6 7 8 9 10 11 12", "1 10 8 6 4 2 11 9 7 5 3 12" ] */ +const test4WordGrids = [ + { + testName: "2-column vertical-lr incorrectly recorded as horizontal-tb", + words: "1 3 2 4".split(" "), + columns: 2, + }, +]; const test12WordGrids = [ { testName: "2-column vertical-lr incorrectly recorded as horizontal-tb", @@ -139,6 +146,13 @@ const test24WordGrids = [ columns: 12, }, ]; +Deno.test("getPermutations returns correct permutations of 4-word test grids", () => { + test4WordGrids.forEach((grid) => { + const permutations = getPermutations(grid.words, grid.columns); + assertArrayIncludes(permutations, ["1 2 3 4"]); + assertEquals(permutations.length, 1); // Should de-duplicate + }); +}); Deno.test("getPermutations returns correct permutations of 12-word test grids", () => { test12WordGrids.forEach((grid) => { const permutations = getPermutations(grid.words, grid.columns); -- 2.47.3