Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:15:25+00:00 2026-05-27T01:15:25+00:00

Note 1: I already have the brute force, verbose, loopy messy code in place.

  • 0

Note 1: I already have the brute force, verbose, loopy messy code in place.
Note 2: Looking for more elegant ways of establishing this – “lesser” number of lines and clever logic.
Note 3: Not homework, I am a hobbyist trying to improve.

Function to be coded:

printNumericPyramid(int depth, String alignment, String orientation);

depth: +ve integer up to 4
alignment: one of “left”/”right”
orientation: one of “upright”/”inverted”

0 will always be at the apex, whether upright or inverted
0259/9520 will always be the height of the pyramid.


Samples calls and expected outputs:

printNumericPyramid(4, "left", "inverted");

9876
543.
21..
0...

printNumericPyramid(4, "right", "upright");

...0
..12
.345
6789

printNumericPyramid(4, "right", "inverted");

6789
.345
..12
...0


printNumericPyramid(4, "left", "upright");

0...
21..
543.
9876

It would be great if “lesser” number of lines could be the focus of the proposed solution. Not by removing all linebreaks:), but by minimization of code (even if it hurts readibility).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:15:26+00:00Added an answer on May 27, 2026 at 1:15 am

    (First note that your method definition is error-prone: if an argument can only take two values you can use a boolean instead of a String. If you mispell “left” or “right” or “inverted” or “upright”, your method will work yet produce the wrong result)

    If you want to focus on a low number of lines then you should not repeat yourself and you can make use of the ternary operator and of the available Java APIs.

    You could write a very short solution based on very “smart” and totally unreadable code inside the following kind of loop:

       public void printNumericPyramid( final int depth, final String alignment, final String orientation ) {
            for (int i = 0; i < depth; i++) {
                for (int j = 0; j < depth; j++) {
                    System.out.print ( ... ) // insert some smart hackery in here computing if we should print 0-9 or '.'
                }
                System.out.println();
            }
        }
    

    However this may not end up being that easy to read and it’s going to be easy to get the logic wrong.

    So I wrote another solution:

    • at each character you either print the next value (from 0 to 9) or you print a dot ‘.’.

    • when a line is “done”, I either add that line or that line reversed, depending on the value of the alignment parameter.

    • if the inversed representation is asked, you can simply reverse the collection

      public void printNumericPyramid( final int depth, final String alignment, final String orientation ) {
          final List<String> l1 = new ArrayList<String>();
          for (int i = 0, c = 0; i < depth; i++) {
              final StringBuilder sb = new StringBuilder();
              for (int j = 0; j < depth; j++) {
                  sb.append( j >= depth - (i + 1) ? c++ : "." );
              }
              l1.add("left".equals(alignment) ? sb.reverse().toString() : sb.toString());
          }
          if ( "inverted".equals(orientation) ) Collections.reverse(l1);
      
          for ( final String s : l1 ) {
              System.out.println( s );
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i already have a post which is quite similiar, but i am getting more
I already have this coded out (more or less) in Java for Linux OS,
Working in an asp.net 2.0 (VB) environment, I already have code that can generate
Is there a GWT Date Time Component? Note: I am already making use of
[Please note that this is a different question from the already answered How to
Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Currently I am trying to parse an xml string that I already have (no
I already have enabled xDebug profiling in my XAMPP installation and it works fine.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.