I think
you should first analyze the logic in the sample code in the book. After you see the output of that code, you can make your own code that is similar in output with what is in the book, but with different logic, what I mean is, the output is similar but the way you used to do that is not a copy-paste from what is in the book, the shorter your code is, the better, as long as it is correct and similar with what is in the output of the book.