-
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.
🏋️♂️ added challenge 20: distribute the weight
- Loading branch information
Showing
3 changed files
with
162 additions
and
0 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,21 @@ | ||
export function distributeGifts (weights: Array<Array<number | null>>): number[][] { | ||
const n = weights.length; const m = weights[0].length | ||
const gifts: number[][] = Array(n).fill(null).map(() => Array(m).fill(0)) | ||
for (let i = 0; i < n; ++i) { | ||
for (let j = 0; j < m; ++j) { | ||
const up = weights[i - 1]?.[j] | ||
const lf = weights[i][j - 1] | ||
const rg = weights[i][j + 1] | ||
const dw = weights[i + 1]?.[j] | ||
const ct = weights[i][j] | ||
let n = 0; let sum = 0 | ||
for (const x of [up, lf, rg, dw, ct]) { | ||
const z = +(x != null) | ||
sum += +([0, x][z] as number) | ||
n += z | ||
} | ||
gifts[i][j] = Math.round(sum / n) | ||
} | ||
} | ||
return gifts | ||
} |
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,140 @@ | ||
import { test, expectTypeOf, expect, describe } from 'vitest' | ||
import { distributeGifts } from '../challenges/20' | ||
|
||
describe('Challenge #20', () => { | ||
test('Test #01', () => { | ||
expectTypeOf(distributeGifts).returns.toEqualTypeOf([]) | ||
}) | ||
|
||
test('Test #02', () => { | ||
const received = distributeGifts([ | ||
[4, 5, 1], | ||
[6, null, 3], | ||
[8, null, 4] | ||
]) | ||
const expected = [ | ||
[ | ||
5, | ||
3, | ||
3 | ||
], | ||
[ | ||
6, | ||
5, | ||
3 | ||
], | ||
[ | ||
7, | ||
6, | ||
4 | ||
] | ||
] | ||
expect(received).toEqual(expected) | ||
}) | ||
|
||
test('Test #03', () => { | ||
const received = distributeGifts([ | ||
[2, null], | ||
[null, 3] | ||
]) | ||
const expected = [ | ||
[ | ||
2, | ||
3 | ||
], | ||
[ | ||
3, | ||
3 | ||
] | ||
] | ||
expect(received).toEqual(expected) | ||
}) | ||
|
||
test('Test #04', () => { | ||
const received = distributeGifts([ | ||
[2, 1, 1], | ||
[3, 4, null] | ||
]) | ||
const expected = [ | ||
[ | ||
2, | ||
2, | ||
1 | ||
], | ||
[ | ||
3, | ||
3, | ||
3 | ||
] | ||
] | ||
expect(received).toEqual(expected) | ||
}) | ||
|
||
test('Test #05', () => { | ||
const received = distributeGifts([ | ||
[null, 5], | ||
[3, null] | ||
]) | ||
const expected = [ | ||
[ | ||
4, | ||
5 | ||
], | ||
[ | ||
3, | ||
4 | ||
] | ||
] | ||
expect(received).toEqual(expected) | ||
}) | ||
|
||
test('Test #06', () => { | ||
const received = distributeGifts([ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9] | ||
]) | ||
const expected = [ | ||
[ | ||
2, | ||
3, | ||
4 | ||
], | ||
[ | ||
4, | ||
5, | ||
6 | ||
], | ||
[ | ||
6, | ||
7, | ||
8 | ||
] | ||
] | ||
expect(received).toEqual(expected) | ||
}) | ||
|
||
test('Test #07', () => { | ||
const received = distributeGifts([ | ||
[null, 1, null, 1, null], | ||
[1, null, 1, null, 1] | ||
]) | ||
const expected = [ | ||
[ | ||
1, | ||
1, | ||
1, | ||
1, | ||
1 | ||
], | ||
[ | ||
1, | ||
1, | ||
1, | ||
1, | ||
1 | ||
] | ||
] | ||
expect(received).toEqual(expected) | ||
}) | ||
}) |