Skip to content

Commit 5a61103

Browse files
committed
feat(Codewars): Solve onlinePlatform/codewars/4kyu/matrix_determinant.cc
1 parent 0208f28 commit 5a61103

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
project(5kyu)
2+
3+
add_executable(
4+
matrix_determinant_google_test
5+
matrix_determinant_google_test.cc
6+
)
7+
target_link_libraries(
8+
matrix_determinant_google_test
9+
GTest::gtest_main
10+
)
11+
gtest_discover_tests(matrix_determinant_google_test)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// https://www.codewars.com/kata/5263a84ffcadb968b6000513/train/cpp
2+
3+
#include <string>
4+
#include <vector>
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
vector< vector<long long> > getMinorMatrix(vector< vector<long long> > &m, int withoutY, int withoutX) {
11+
vector< vector<long long> > minor;
12+
13+
int HEIGHT = m.size();
14+
if (HEIGHT <= 1) {
15+
// TODO: confirm this
16+
return minor;
17+
}
18+
19+
int WIDTH = m[0].size();
20+
// TODO: validate input, e.g. withoutY and withoutX are within HEIGHT and WIDTH and are non-negative
21+
22+
for (int y = 0; y < HEIGHT; ++y) {
23+
if (y == withoutY) {
24+
continue;
25+
}
26+
vector<long long> row;
27+
for (int x = 0; x < WIDTH; ++x) {
28+
if (x == withoutX) {
29+
continue;
30+
}
31+
row.push_back(m[y][x]);
32+
}
33+
minor.push_back(row);
34+
}
35+
36+
return minor;
37+
}
38+
39+
long long determinant(vector< vector<long long> > m) {
40+
// TODO: Return the determinant of the square matrix passed in
41+
42+
// TODO:
43+
// 1. validate m is a square matrix
44+
// 2. handle when m.size() == 0
45+
int n = m.size();
46+
if (n == 1) {
47+
return m[0][0];
48+
}
49+
if (n == 2) {
50+
return m[0][0] * m[1][1] - m[0][1] * m[1][0];
51+
}
52+
53+
int d = 0;
54+
int sign = 1;
55+
int y = 0;
56+
for (int x = 0; x < n; ++x) {
57+
auto minor = getMinorMatrix(m, y, x);
58+
d += sign * m[y][x] * determinant(minor);
59+
sign *= -1;
60+
}
61+
62+
return d;
63+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <gtest/gtest.h>
2+
#include <string>
3+
#include <vector>
4+
5+
#include "matrix_determinant.cc"
6+
7+
TEST(MatrixDeterminant, should_work_for_a_few_simple_square_matrices) {
8+
EXPECT_EQ(determinant(vector<vector<long long> >{
9+
vector<long long>{1}
10+
}), 1);
11+
12+
EXPECT_EQ(determinant(vector<vector<long long> >{
13+
vector<long long>{1, 3},
14+
vector<long long>{2, 5}
15+
}), -1);
16+
17+
EXPECT_EQ(determinant(vector<vector<long long> >{
18+
vector<long long>{2, 5, 3},
19+
vector<long long>{1, -2, -1},
20+
vector<long long>{1, 3, 4}
21+
}), -20);
22+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
project(codewars)
22

3-
add_subdirectory(5kyu)
3+
add_subdirectory(5kyu)
4+
add_subdirectory(4kyu)

0 commit comments

Comments
 (0)