permaculture playing cards*
The moose likes Java in General and the fly likes Pyramid Printing Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Pyramid Printing Problem" Watch "Pyramid Printing Problem" New topic
Author

Pyramid Printing Problem

Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 159

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



"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 159

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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Apr 23, 2009
Posts: 159

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

Joined: Oct 13, 2005
Posts: 39084
    
  23
You can see for yourself how much better it is Well done.
 
GeeCON Prague 2014
 
subject: Pyramid Printing Problem