Skip to content

[BUGFIX] sonarcloud: Fix this access on a collection that may trigger… #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,39 @@ private Problem0015() {}
* Problem 0015.
*/
public static Long problem0015(Integer gridSide) {
int limit;

Long[][] grid = new Long[gridSide + 1][gridSide + 1];
if (gridSide <= 0) {
limit = 0;
} else {
limit = gridSide + 1;
}

Long[][] grid = new Long[limit][limit];

// initialization
for (int i = 0; i <= gridSide; i++) {
for (int j = 0; j <= gridSide; j++) {
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
grid[i][j] = 1L;
}
}

// vertex computing
for (int i = 1; i <= gridSide; i++) {
for (int j = 1; j <= gridSide; j++) {
for (int i = 1; i < limit; i++) {
for (int j = 1; j < limit; j++) {
long upperParent = grid[i - 1][j];
long leftParent = grid[i][j - 1];

grid[i][j] = upperParent + leftParent;
}
}

Long result = grid[gridSide][gridSide];
Long result;
if (grid.length > 0) {
result = grid[grid.length - 1][grid.length - 1];
} else {
result = 0L;
}

String log;
log = String.format("Problem 00015 solved: %d", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,22 @@ class Problem0015Test {
String.format("Problem 0015 answer must be: %d", answer)
);
}

@Test void problem0015edgeCase() {

Integer input = 0;
Long answer = 0L;
Long solutionFound = Problem0015.problem0015(input);

assertEquals(answer, solutionFound,
String.format("Problem 0015 answer must be: %d", answer)
);

input = -1;
answer = 0L;
solutionFound = Problem0015.problem0015(input);
assertEquals(answer, solutionFound,
String.format("Problem 0015 answer must be: %d", answer)
);
}
}
Loading