Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Pyramid Printing Problem

Riaan Nel
Ranch Hand
Posts: 160
Hi Guys

I came across a fairly standard 'pyramid' problem in a Java test yesterday. The objective was to take a number and use it to generate a pyramid starting with 1 at the top and working downwards, printing each number x times per row. I have to admit that it had me puzzled for a while. This is the solution that I eventually came up with. What do you think? Is it efficient, clean and simple, or would you do it differently?

Cheers,
Riaan

Campbell Ritchie
Sheriff
Posts: 48954
60
Lots of extraneous code; you can get rid of "new int[]" for example, and you never need use println to print an empty String.
Your gridwidth and max/min indices are also necessary. I would suggest you can print spaces up to the count. That way you only need two nested loop levels, rather than three. Your thing about odd numbers and even numbers looks complicated, and is probably incorrect. The easiest way to test whether a number is odd isThat works for positive and negative numbers, and the right operand of the & can be one of a restricted set of numbers. I'll let you work it out for yourself which numbers.

Clean, efficient and simple? Afraid not.

Rob Spoor
Sheriff
Posts: 20529
54
Campbell Ritchie wrote:That way you only need two nested loop levels, rather than three.

There are only two nested loops for a single pyramid. The outer loop will cause three pyramids to be printed.

Riaan Nel
Ranch Hand
Posts: 160
Campbell Ritchie wrote:Lots of extraneous code; you can get rid of "new int[]" for example, and you never need use println to print an empty String.
Your gridwidth and max/min indices are also necessary. I would suggest you can print spaces up to the count. That way you only need two nested loop levels, rather than three. Your thing about odd numbers and even numbers looks complicated, and is probably incorrect. The easiest way to test whether a number is odd isThat works for positive and negative numbers, and the right operand of the & can be one of a restricted set of numbers. I'll let you work it out for yourself which numbers.

Clean, efficient and simple? Afraid not.

Thanks for your response Campbell. I've cleaned up the cosmetic issues, but I'm stumped on how I can use another way to determine when to print numbers. Would you mind giving me some pointers?

Edit: This is what I've got after making updates. How does it look?

Campbell Ritchie
Sheriff
Posts: 48954
60
Only a slight improvement.
Get rid of the following variables: gridWidth, minXIndex, maxXIndex. You don't need any of them. Why have you got the check for odd and even numbers at all? You don't need that either.

What you want to do is print three spaces, then 1. Then print two spaces, then 2 (+ space) (twice). Then print one space and 3 (+ space) thrice. Then no spaces and 4 ...
No need for width measurement. No need to check for odd and even numbers. It is much simpler than you think.

Riaan Nel
Ranch Hand
Posts: 160
Campbell Ritchie wrote:Only a slight improvement.
Get rid of the following variables: gridWidth, minXIndex, maxXIndex. You don't need any of them. Why have you got the check for odd and even numbers at all? You don't need that either.

What you want to do is print three spaces, then 1. Then print two spaces, then 2 (+ space) (twice). Then print one space and 3 (+ space) thrice. Then no spaces and 4 ...
No need for width measurement. No need to check for odd and even numbers. It is much simpler than you think.

Thanks for your help. That last paragraph made it all come together. How's about this? When I did the problem the first time round, my assumption was that it should print a rectangular grid (including spaces after the numbers).

Campbell Ritchie
Sheriff
Posts: 48954
60
• 1
You can see for yourself how much better it is Well done.