Skip to content

Commit

Permalink
Merge pull request #4 from prezly/fix/exclude-empty-rows-from-the-cal…
Browse files Browse the repository at this point in the history
…culated-partition

Exclude empty groups from the calculated partitioning
  • Loading branch information
e1himself authored Jun 3, 2022
2 parents ead7004 + bc16af2 commit 582da71
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/linearPartition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ describe('linearPartition', () => {
[74, 77, 133, 64],
],
},
{
input: {
elements: [150, 66.65, 66.65, 150, 100, 150, 66.65],
maxRanges: 11,
},
output: [[150], [66.65, 66.65], [150], [100], [150], [66.65]],
},
];

tests.forEach(({ input: { elements, maxRanges }, output }) => {
Expand Down
3 changes: 2 additions & 1 deletion src/linearPartition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const reconstructPartitioning = (S: number[], D: number[][], n: number, k: numbe
}

// Fix order as we reconstructed the partitioning from end to start
return partition.reverse();
// Ignoring possibly empty rows, appearing when there are no delimiters for the given `j` index.
return partition.reverse().filter((row) => row.length > 0);
};

export function linearPartition(elements: number[], maxRanges: number): number[][] {
Expand Down

0 comments on commit 582da71

Please sign in to comment.