-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more unique problem generation
- Loading branch information
Showing
5 changed files
with
132 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import roundRobinRange from './roundRobinRange'; | ||
|
||
type Range = { from: number, to: number }; | ||
type PairOfRange = [aRange: Range, bRange: Range]; | ||
type NumberPair = [a: number, b: number]; | ||
interface TestData { | ||
args: PairOfRange; | ||
pairs: NumberPair[] | ||
} | ||
|
||
describe('roundRobinRange()', () => { | ||
const testTable = new Map<string, TestData>([ | ||
['only zeroes', { | ||
args: [{ from: 0, to: 0 }, { from: 0, to: 0 }], | ||
pairs: [[0, 0]], | ||
}], | ||
['1 vs 1', { | ||
args: [{ from: 1, to: 1 }, { from: 2, to: 2 }], | ||
pairs: [ | ||
[1, 2], | ||
[2, 1], | ||
], | ||
}], | ||
['2 vs 1', { | ||
args: [{ from: 0, to: 1 }, { from: 2, to: 2 }], | ||
pairs: [ | ||
[0, 2], | ||
[1, 2], | ||
[2, 0], | ||
[2, 1], | ||
], | ||
}], | ||
['2 vs 2', { | ||
args: [{ from: 0, to: 1 }, { from: 2, to: 3 }], | ||
pairs: [ | ||
[0, 2], | ||
[0, 3], | ||
[1, 2], | ||
[1, 3], | ||
[2, 0], | ||
[2, 1], | ||
[3, 0], | ||
[3, 1], | ||
], | ||
}], | ||
['2 vs 2 with overlap', { | ||
args: [{ from: 0, to: 1 }, { from: 1, to: 2 }], | ||
pairs: [ | ||
[0, 1], | ||
[0, 2], | ||
[1, 1], | ||
[1, 2], | ||
[1, 0], | ||
[2, 0], | ||
[2, 1], | ||
], | ||
}], | ||
['3 vs 3 with overlap', { | ||
args: [{ from: 0, to: 2 }, { from: 1, to: 3 }], | ||
pairs: [ | ||
[0, 1], | ||
[0, 2], | ||
[0, 3], | ||
[1, 1], | ||
[1, 2], | ||
[1, 3], | ||
[2, 1], | ||
[2, 2], | ||
[2, 3], | ||
[1, 0], | ||
[2, 0], | ||
[3, 0], | ||
[3, 1], | ||
[3, 2], | ||
], | ||
}], | ||
]); | ||
|
||
testTable.forEach((testData, note) => { | ||
describe(`When ${note}`, () => { | ||
let pairings: NumberPair[]; | ||
|
||
beforeEach(() => { | ||
pairings = roundRobinRange(...testData.args); | ||
}); | ||
|
||
it('contains only possible range', () => { | ||
expect(new Set(pairings)).toEqual(new Set(testData.pairs)); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
type Pair = [a: number, b: number]; | ||
type Range = { from: number, to: number }; | ||
export default function roundRobinRange(aRange: Range, bRange: Range): Pair[] { | ||
const pairs: Pair[] = []; | ||
const found = new Set<string>(); | ||
for (let i = aRange.from; i <= aRange.to; i++) { | ||
for (let j = bRange.from; j <= bRange.to; j++) { | ||
const keyA = `${i}:${j}`; | ||
if (!found.has(keyA)) { | ||
pairs.push([i, j]); | ||
found.add(keyA); | ||
} | ||
const keyB = `${j}:${i}`; | ||
if (!found.has(keyB)) { | ||
pairs.push([j, i]); | ||
found.add(keyB); | ||
} | ||
} | ||
} | ||
return pairs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters