5 Added test case for tall matrices
source | link

(Note that this pseudocode works only for slopes less than 1; for tall grids, a similar treatment should be done, but with a loop over y. See this section for the two cases.)

Agatha imagines a matrix as a rectangle, draws a diagonal line in it, and Bresenham’s algorithm determines which elements of a matrix belong to the diagonal. Then she takes their sum, and this is what she wants to implement in as few bytes as possible because she is a poor student and cannot afford large-capacity HDDs to store her code.

  1. [[-0.3,0.5]] → output: 0.2.

  2. [[3.1],[2.9]] → output: 6.

  3. [[-5]] → output: -5.

    [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]1+5+8+12 → output: 26.

Test case 5

  1. [[-0.3,0.5]] → output: 0.2.

  2. [[3.1],[2.9]] → output: 6.

  3. [[-5]] → output: -5.

Agatha imagines a matrix as a rectangle, draws a diagonal line in it, and Bresenham’s algorithm determines which elements of a matrix belong to the diagonal. Then she takes their sum, and this is what she wants to implement in as few bytes as possible because she is a poor student and cannot afford large-capacity HDDs to store her code.

  1. [[-0.3,0.5]] → output: 0.2.

  2. [[3.1],[2.9]] → output: 6.

  3. [[-5]] → output: -5.

(Note that this pseudocode works only for slopes less than 1; for tall grids, a similar treatment should be done, but with a loop over y. See this section for the two cases.)

Agatha imagines a matrix as a rectangle, draws a diagonal line in it, and Bresenham’s algorithm determines which elements of a matrix belong to the diagonal. Then she takes their sum, and this is what she wants to implement in as few bytes as possible because she is a poor student and cannot afford large-capacity HDDs to store her code.

  1. [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]1+5+8+12 → output: 26.

Test case 5

  1. [[-0.3,0.5]] → output: 0.2.

  2. [[3.1],[2.9]] → output: 6.

  3. [[-5]] → output: -5.

4 Explained an unclear case
source | link

Note that some sources (e. g. the Wikipedia’s pseudocode above) use the condition check error≥0.5, while other sources use error>0.5. You should use the originally posted one (error≥0.5), but if the alternative error>0.5 is shorter in your code, then you are allowed to implement it (since this is code golf), but mention it explicitly. See test case 4.

  1. [[-0.3,0.5]] → output: 0.2.

  2. [[3.1],[2.9]] → output: 6.

  3. [[-5]] → output: -5.

    [[1,2,3,4,5],[6,7,8,9,10]]1+2+8+9+10 (using the error condition) → output: 30.

Test case 4

However, if it would be shorter to use the strict inequality > in your code, then the allowed output is 1+2+3+9+10=25, but you should mention it separately.

Test case 5

  1. [[-0.3,0.5]] → output: 0.2.

  2. [[3.1],[2.9]] → output: 6.

  3. [[-5]] → output: -5.

  1. [[-0.3,0.5]] → output: 0.2.

  2. [[3.1],[2.9]] → output: 6.

  3. [[-5]] → output: -5.

Note that some sources (e. g. the Wikipedia’s pseudocode above) use the condition check error≥0.5, while other sources use error>0.5. You should use the originally posted one (error≥0.5), but if the alternative error>0.5 is shorter in your code, then you are allowed to implement it (since this is code golf), but mention it explicitly. See test case 4.

  1. [[1,2,3,4,5],[6,7,8,9,10]]1+2+8+9+10 (using the error condition) → output: 30.

Test case 4

However, if it would be shorter to use the strict inequality > in your code, then the allowed output is 1+2+3+9+10=25, but you should mention it separately.

Test case 5

  1. [[-0.3,0.5]] → output: 0.2.

  2. [[3.1],[2.9]] → output: 6.

  3. [[-5]] → output: -5.

3 error in test case
source | link
  1. [[1,2,3],[4,5,6],[7,8,9]]1+5+9 → output: 1415.
  1. [[1,2,3],[4,5,6],[7,8,9]]1+5+9 → output: 14.
  1. [[1,2,3],[4,5,6],[7,8,9]]1+5+9 → output: 15.
    Tweeted twitter.com/StackCodeGolf/status/977100712172769281
2 extra dot
source | link
1
source | link