• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pyramid Printing Problem

 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can see for yourself how much better it is Well done.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic