BruceEckel/AtomicKotlinExamples

1.14 Summary 1, Exercise 4: pad width

diedummydie opened this issue · 3 comments

From the Task Description:

For proper alignment use "%3d".format(number) to place additional spaces before the number.

With that phrasing I would expect to use the provided expression nearly verbatim. However, exercise code doesn't pass the 2x2 unit test or match the expected 3x3 output in the description without calculating width with (rows * columns).toString().length + 1 and then using "%${width}d".format(number), rather than just hard-coding it to 3.

This is apparent after a 'Peek Solution...', but I think a learner would have a better shot at writing passing code on their own if at least one of the following were provided:

  • A previous exercise illustrating how to combine string templates and format strings
  • A more detailed response if the 2x2 unit test fails
  • Phrasing changed to:

For proper alignment use a format string such as "%3d".format(number) to place additional spaces before the number.

  • A call to printSnake(3, 3) added to main() to give that use case better visibility, and expected output added to the commented output alongside the expected 4x5 output:
fun main(args: Array<String>) {
    printSnake(4, 5)
    println()
    printSnake(3, 3)
}
/* Output:
  0  1  2  3  4
  9  8  7  6  5
 10 11 12 13 14
 19 18 17 16 15

 0 1 2
 5 4 3
 6 7 8
*/

If people who are completely new to programming are among the target audience, this might also be a nice place to explicitly mention the pitfalls of hard-coding values. After all, it isn't immediately apparent that output of a small table with width hard-coded to 3 is 'wrong', especially if you look at a 2x5 next to the 4x5:

/* Output:
  0  1  2  3  4
  9  8  7  6  5
 10 11 12 13 14
 19 18 17 16 15

  0  1  2  3  4
  9  8  7  6  5
*/

An alignment mismatch between cells wouldn't manifest until creating a table with over 1000 cells, and that could be considered outside intended use since it would also overflow a standard 80x24 terminal.

Note also that (row, column) means parameters are in (y, x) order, inverse to the more conventional (x, y).

I'm not trying to be annoying, I promise. I'm trying to be helpful since the book is still in beta. Feel free to disregard any or all of the issues I've opened, because they're all minor and subjective.

svtk commented

Thanks for such detailed feedback! Updated the task description svtk/AtomicKotlinCourse@09aa533

Issue fixed by Svetlana