Skip to content

Commit d175766

Browse files
author
Jakub Jirous
authored
Merge pull request #8 from jakubjirous/feature/diagonal-difference
5) Diagonal Difference
2 parents 1616dc9 + 47127d8 commit d175766

File tree

8 files changed

+198
-8
lines changed

8 files changed

+198
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ Developers compete by writing programs according to provided specifications.
2626
2. [Mini-Max Sum](/src/challenges/02-mini-max-sum/INDEX.md)
2727
3. [Time Conversion](/src/challenges/03-time-conversion/INDEX.md)
2828
4. [Lonely Integer](/src/challenges/04-lonely-integer/INDEX.md)
29+
5. [Diagonal Difference](/src/challenges/05-diagonal-difference/INDEX.md)

src/challenges/01-plus-minus/test/plus-minus.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("Plus Minus", () => {
5252
];
5353

5454
test.each(cases)(
55-
"for given array of integers %p output should be %p",
55+
"for given array of integers %j output should be %s",
5656
(firstArg, expectedResult) => {
5757
const result = plusMinus(firstArg);
5858
expect(result.trim()).toEqual(expectedResult.trim());

src/challenges/02-mini-max-sum/test/mini-max-sum.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("Mini-Max Sum", () => {
4747
];
4848

4949
test.each(cases)(
50-
"for given array of integers %p output should be %p",
50+
"for given array of integers %j output should be %j",
5151
(firstArg, expectedResult) => {
5252
const result = miniMaxSum(firstArg);
5353
expect(result).toEqual(expectedResult);

src/challenges/03-time-conversion/test/time-conversion.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("Time Conversion", () => {
1717
];
1818

1919
test.each(cases)(
20-
"for given time %p output should be %p",
20+
"for given time %s output should be %s",
2121
(firstArg, expectedResult) => {
2222
const result = timeConversion(firstArg);
2323
expect(result).toEqual(expectedResult);

src/challenges/04-lonely-integer/test/lonely-integer.test.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,44 @@ describe("Lonely Integer", () => {
88
[[1, 1, 2], 2],
99
[[0, 0, 1, 2, 1], 2],
1010
[[4, 9, 95, 93, 57, 4, 57, 93, 9], 95],
11-
[[34, 95, 34, 64, 45, 95, 16, 80, 80, 75, 3, 25, 75, 25, 31, 3, 64, 16, 31], 45],
12-
[[15, 60, 74, 1, 94, 93, 28, 48, 70, 98, 45, 94, 42, 45, 48, 1, 72, 9, 24, 93, 41, 70, 60, 28, 11, 20, 72, 35, 11, 98, 31, 74, 31, 41, 9, 42, 20, 35, 24], 15],
13-
[[18, 49, 15, 30, 56, 20, 49, 67, 82, 69, 84, 63, 93, 87, 66, 17, 38, 32, 17, 32, 94, 66, 67, 63, 48, 64, 84, 82, 87, 18, 79, 64, 79, 15, 71, 94, 59, 5, 22, 59, 4, 98, 81, 4, 98, 73, 69, 88, 30, 81, 48, 56, 71, 20, 93, 22, 73, 5, 88], 38],
14-
[[59, 88, 14, 8, 85, 1, 94, 74, 57, 96, 39, 2, 47, 43, 35, 17, 53, 52, 92, 31, 99, 48, 94, 30, 92, 60, 32, 45, 88, 13, 39, 50, 22, 65, 89, 46, 65, 76, 57, 67, 99, 35, 76, 46, 85, 82, 45, 62, 53, 80, 74, 22, 31, 52, 82, 13, 41, 96, 2, 1, 80, 62, 4, 20, 50, 89, 59, 67, 60, 8, 41, 14, 47, 48, 17, 4, 43, 30, 32], 20],
11+
[
12+
[
13+
34, 95, 34, 64, 45, 95, 16, 80, 80, 75, 3, 25, 75, 25, 31, 3, 64, 16,
14+
31,
15+
],
16+
45,
17+
],
18+
[
19+
[
20+
15, 60, 74, 1, 94, 93, 28, 48, 70, 98, 45, 94, 42, 45, 48, 1, 72, 9, 24,
21+
93, 41, 70, 60, 28, 11, 20, 72, 35, 11, 98, 31, 74, 31, 41, 9, 42, 20,
22+
35, 24,
23+
],
24+
15,
25+
],
26+
[
27+
[
28+
18, 49, 15, 30, 56, 20, 49, 67, 82, 69, 84, 63, 93, 87, 66, 17, 38, 32,
29+
17, 32, 94, 66, 67, 63, 48, 64, 84, 82, 87, 18, 79, 64, 79, 15, 71, 94,
30+
59, 5, 22, 59, 4, 98, 81, 4, 98, 73, 69, 88, 30, 81, 48, 56, 71, 20, 93,
31+
22, 73, 5, 88,
32+
],
33+
38,
34+
],
35+
[
36+
[
37+
59, 88, 14, 8, 85, 1, 94, 74, 57, 96, 39, 2, 47, 43, 35, 17, 53, 52, 92,
38+
31, 99, 48, 94, 30, 92, 60, 32, 45, 88, 13, 39, 50, 22, 65, 89, 46, 65,
39+
76, 57, 67, 99, 35, 76, 46, 85, 82, 45, 62, 53, 80, 74, 22, 31, 52, 82,
40+
13, 41, 96, 2, 1, 80, 62, 4, 20, 50, 89, 59, 67, 60, 8, 41, 14, 47, 48,
41+
17, 4, 43, 30, 32,
42+
],
43+
20,
44+
],
1545
];
1646

1747
test.each(cases)(
18-
"for given array of integers %p output should be %p",
48+
"for given array of integers %j output should be %d",
1949
(firstArg, expectedResult) => {
2050
const result = lonelyInteger(firstArg);
2151
expect(result).toEqual(expectedResult);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Diagonal Difference
2+
3+
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
4+
5+
---
6+
7+
### Example:
8+
- For example, the square matrix `matrix` is shown below:
9+
10+
```
11+
1 2 3
12+
4 5 6
13+
9 8 9
14+
```
15+
- The left-to-right diagonal = `1 + 5 + 9 = 15`
16+
- The right to left diagonal = `3 + 5 + 9 = 17`
17+
- Their absolute difference is `|15 - 17| = 2`
18+
19+
### Function Description:
20+
21+
- Complete the `diagonalDifference` function in the editor below.
22+
- `diagonalDifference` diagonalDifference takes the following parameter:
23+
- `int matrix[n][m]`: an array of integers
24+
25+
### Returns:
26+
27+
- `int`: the absolute diagonal difference
28+
29+
### Input Format:
30+
31+
- The first line contains a single integer, `n`, the number of rows and columns in the square matrix `matrix`.
32+
- Each of the next `n` lines describes a row, `matrix[i]`, and consists of `n` space-separated integers `matrix[i][j]`.
33+
34+
35+
### Constraints:
36+
37+
- $-100 \le matrix[i][j] \le 100$
38+
39+
### Output Format:
40+
41+
- Return the absolute difference between the sums of the matrix's two diagonals as a single integer.
42+
43+
---
44+
45+
### Sample Input:
46+
47+
```
48+
11 2 4
49+
4 5 6
50+
10 8 -12
51+
```
52+
53+
### Sample Output:
54+
55+
```
56+
15
57+
```
58+
59+
---
60+
61+
### Solution:
62+
63+
- [Code](/src/challenges/05-diagonal-difference/diagonal-difference.ts)
64+
- [Tests](/src/challenges/05-diagonal-difference/test/diagonal-difference.test.ts)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export type Input = number[][];
2+
export type Output = number;
3+
4+
/**
5+
* Given a square matrix, calculate the absolute difference between the sums of its diagonals.
6+
*
7+
* @param matrix
8+
*/
9+
export const diagonalDifference = (matrix: Input): Output => {
10+
const size = matrix.length;
11+
12+
let primarySum = 0;
13+
let secondarySum = 0;
14+
15+
for (let i = 0; i < size; i++) {
16+
for (let j = 0; j < size; j++) {
17+
if (i === j) {
18+
primarySum += matrix[i][j];
19+
}
20+
21+
if (i + j === size - 1) {
22+
secondarySum += matrix[i][j];
23+
}
24+
}
25+
}
26+
27+
return Math.abs(primarySum - secondarySum);
28+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Input, diagonalDifference, Output } from "../diagonal-difference";
2+
3+
type Cases = [Input, Output][];
4+
5+
describe("Diagonal Difference", () => {
6+
const cases: Cases = [
7+
[
8+
[
9+
[11, 2, 4],
10+
[4, 5, 6],
11+
[10, 8, -12],
12+
],
13+
15,
14+
],
15+
[
16+
[
17+
[1, 5, 6],
18+
[3, 2, 7],
19+
[8, 5, 1],
20+
],
21+
12,
22+
],
23+
[
24+
[
25+
[1, 5, 6, 2],
26+
[3, 2, 7, 9],
27+
[8, 5, 1, 3],
28+
[2, 6, 8, 9],
29+
],
30+
3,
31+
],
32+
[
33+
[
34+
[2, 4, 5, 6],
35+
[8, 3, 1, 3],
36+
[7, 9, 4, 2],
37+
[5, 8, 3, 6],
38+
],
39+
6,
40+
],
41+
[
42+
[
43+
[1, 2, 3],
44+
[4, 5, 6],
45+
[7, 8, 9],
46+
],
47+
0,
48+
],
49+
[
50+
[
51+
[-1, 1, -7, -8],
52+
[-10, -8, -5, -2],
53+
[0, 9, 7, -1],
54+
[4, 4, -2, 1],
55+
],
56+
1,
57+
],
58+
];
59+
60+
test.each(cases)(
61+
"for given matrix %j output should be %d",
62+
(firstArg, expectedResult) => {
63+
const result = diagonalDifference(firstArg);
64+
expect(result).toEqual(expectedResult);
65+
}
66+
);
67+
});

0 commit comments

Comments
 (0)