HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Calculating a specific entry in a Pascal’s triangle recursively

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
entrypascalrecursivelytrianglecalculatingspecific

Problem

Our task was to calculate the entry of a Pascal’s triangle with a given row and column recursively. The triangle was specified in a way that the tip of the triangle is column = 0 and row = 0. That said, column 0 has always the entry 1.

My concerns are that the way I initialize the triangle as an array and filling in the entries are not so super.

``
class EntryOfPascalsTriangle {
public static void main(String[] args) {
validateInput(args);

int row = 0;
row = parseInteger(args[0]);

int column = 0;
column = parseInteger(args[1]);

// A Pascal’s triangle expands quadratic.
// There are never entries at a position greater column length
if (column > row) {
handleError("There can't be entries in a column that doesn't exist in a Pascal's triangle.");
}

// We know already that the first and last entries in a row are always 1
if (column == 0 || column == row) {
System.out.println(1);
System.exit(0);
}

int pascalsTriangle[][] = initPascalsTriangle(row);

// initPascalsTriangle() takes care of the cases
0 and 1 already …
if (row > 1) {
// … so we can start to fill the rows starting from 2.
pascalsTriangle = fillPascalRows(pascalsTriangle, 2);
}

System.out.println(pascalsTriangle[row][column]);
}

/**
* Initializes a Pascal’s triangle with all cells filled with
0
* except for the first and last cell in a row which are
1`
*/
private static int[][] initPascalsTriangle(int rows) {
int[][] pascalsTriangle = new int[rows+1][];

// Iterate over the columns of the rows of pascalsTriangle
for (int cell = 0; cell 2) {
handleError("Too many arguments.");
}
}

/**
* Small wrapper to parse strings as integers.
*/
private static int parseInteger(String arg) {
int result = 0

Solution

Since this question is marked as beginner, let me compliment the neat structure and consistent style I see throughout. It's well done.

Variable, method, and class names are all meaningful, and useful.

The validation and error handling systems are uncommonly comprehensive, and again, well done.

Validation

The validation should provide more feedback to the user. Saying 'not enough arguments' is fine, but then you should also say how many are expected. Does the user have to engage on a guessing game, bouncing between too many arguments, and not enough arguments before discovering how many they need?

Pre-initializing variables

There is no need to preinitialize variables. The following:

int row = 0;
    row = parseInteger(args[0]);

    int column = 0;
    column = parseInteger(args[1]);


can just be:

int row = parseInteger(args[0]);
    int column = parseInteger(args[1]);


Algorithm

THis is where I see the biggest problem, and it's not because you've made a mess, but because I think you've missed the point. If the problem is: "... to calculate the entry of a Pascal’s triangle with a given row and column recursively" then that is not what you have done.

You have calculated all entries and stored them in an array, then indexed the values you need. Sure, some parts are recursive, but I expect that the point is to not have the arrays at all.

Consider the triangle:

1
1   1
1   2   1
1   3   3   1
1   4   6   4   1
.....


The 'simple' solution is to have a method like:

public int getPascalValue(int row, int column) {
    if (column == 0 || column == row) {
        return 1;
    }
    return getPascalValue(row - 1, column - 1) + getPascalValue(row - 1, column);
}


That is a system for recursively calculating the value at a given row/column (assuming valid input).

It does a fair number or recalculations, but the point is that it solves the problem as stated.

Code Snippets

int row = 0;
    row = parseInteger(args[0]);

    int column = 0;
    column = parseInteger(args[1]);
int row = parseInteger(args[0]);
    int column = parseInteger(args[1]);
1
1   1
1   2   1
1   3   3   1
1   4   6   4   1
.....
public int getPascalValue(int row, int column) {
    if (column == 0 || column == row) {
        return 1;
    }
    return getPascalValue(row - 1, column - 1) + getPascalValue(row - 1, column);
}

Context

StackExchange Code Review Q#74262, answer score: 7

Revisions (0)

No revisions yet.