Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

numbers ascending pyramid

Hi,

I am looking for
given out Input: 43790
i need below pyramid of output
Output:
    0
   33
  444
7777
99999

i wrote as below
import java.util.Scanner;
 
public class TestClass
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
 
      
 
        System.out.println("rows");
 
        int noOfRows = sc.nextInt();
 
        
 
        int rowCount = 1;
 
        System.out.println("output");
 
        
 
        for (int i = noOfRows; i > 0; i--)
        {
            
 
            for (int j = 1; j <= i; j++)
            {
                System.out.print(" ");
            }
 
          
            for (int j = 1; j <= rowCount; j++)
            {
                System.out.print(rowCount+" ");
            }
 
            System.out.println(); 
           
 
            rowCount++;
        }
    }
}

Open in new window

i got below output

rows
10
output
          1
         2 2
        3 3 3
       4 4 4 4
      5 5 5 5 5
     6 6 6 6 6 6
    7 7 7 7 7 7 7
   8 8 8 8 8 8 8 8
  9 9 9 9 9 9 9 9 9
 10 10 10 10 10 10 10 10 10 10


i need for given number not passing rows?
please advise
Avatar of d-glitch
d-glitch
Flag of United States of America image

Where do read the input?  What line of your program?
After you read the input, what do you have to do with it?

How would you describe the relationship between the input and outpit in words?
Avatar of gudii9

ASKER

input 43790

so separate each digit as 0(first digit) 3(second digit of input) 4 7 9

then form ascending pattern of pyramid with space

0(first digit one time)
33(second 2 times)
444
7777
99999
Avatar of gudii9

ASKER

input i can read from scanner class like my example
import java.util.Scanner;
 
public class TestClass
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
 
      
 
        System.out.println("rows");
 
        int noOfRows = sc.nextInt();
 
        
 
        int rowCount = 1;
 
        System.out.println("output");
 
        
 
        for (int i = noOfRows; i > 0; i--)
        {
            
 
            for (int j = 1; j <= i; j++)
            {
                System.out.print(" ");
            }
 
          
            for (int j = 1; j <= rowCount; j++)
            {
                System.out.print(rowCount+" ");
            }
 
            System.out.println(); 
           
 
            rowCount++;
        }
    }
}

Open in new window

givenNumber is==>
5
output
     1
    2 2
   3 3 3
  4 4 4 4
 5 5 5 5 5
Avatar of gudii9

ASKER

instead if i pass 23 as input my output is

 2
33
What is the goal here? I see nothing useful in this. But keep it simple: sort your input from low to high and with 2 for loops write the output. What was your pseudo code anyway?
Avatar of gudii9

ASKER

sure let me try
Avatar of gudii9

ASKER

by putting in array each digit?
int noOfRows = sc.nextInt();

Open in new window

Why 'noOfRows'. It's not the number of rows is it?
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;
 
public class TestClass
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        

        String input = scanner.nextLine();

        int integers[] = new int[input.length()];

        String inputArray[] = input.split("");

        

        for(int i = 0; i < inputArray.length; i++){

                integers[i] = Integer.parseInt(inputArray[i]);

        }

        

    
        Arrays.sort(integers);

        

        for(int i = 0; i < integers.length; i++){

                for(int j = 0; j< i; j++){

                        System.out.print(integers[i]);

                }

                System.out.println();

        }


}
}

Open in new window


234

3
44


not coming first number 2??
Avatar of gudii9

ASKER

also space not adjusting like pyramid
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;
 
public class TestClass
{
    public static void main(String[] args)
    {
    	
    	Scanner scanner = new Scanner(System.in);

    

    String input = scanner.nextLine();

    int integers[] = new int[input.length()];

    String inputArray[] = input.split("");

    

    for(int i = 0; i < inputArray.length; i++){

            integers[i] = Integer.parseInt(inputArray[i]);

    }

    

  

    Arrays.sort(integers);

    for(int i = 1; i < integers.length; i++){

            for(int j = 1; j< i+1; j++){

                    System.out.print(integers[i]);

            }

            System.out.println();

    }

}
}

Open in new window

8678905
5
66
777
8888
88888
999999
One of your for loops starts with 0, the other with 1. That's why you miss the first row in output. If you want a real pyramid, calculate the amount of space you need, based on length and index position. What is the goal of this exercise?
Avatar of gudii9

ASKER

just learning as always
Avatar of gudii9

ASKER

    for(int i = 0; i < inputArray.length; i++){

            integers[i] = Integer.parseInt(inputArray[i]);

    }

    

  

    Arrays.sort(integers);

    for(int i = 1; i < integers.length; i++){

            for(int j = 1; j< i+1; j++){

                    System.out.print(integers[i]);

            }

Open in new window


which loop? i have 3 for loops here?
Which one might cause you to miss or skip the first integer?
Maybe you could try a diagnostic print() statement?
I gave you a very clear hint...
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;
 
public class TestClass
{
    public static void main(String[] args)
    {
    	
    	Scanner scanner = new Scanner(System.in);

    

    String input = scanner.nextLine();

    int integers[] = new int[input.length()];

    String inputArray[] = input.split("");

    

    for(int i = 0; i < inputArray.length; i++){

            integers[i] = Integer.parseInt(inputArray[i]);

    }

    

  

    Arrays.sort(integers);

    for(int i = 0; i < integers.length; i++){

            for(int j = 0; j< i+1; j++){

                    System.out.print(integers[i]);

            }

            System.out.println();

    }

}
}

Open in new window


i got below console output
123
1
22
333


above took care of first digit. how to take care of the space as well so that i will see as below when i pass 123 through console??



  1
 22
333
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;

public class TestClass {
	public static void main(String[] args) {
//getting input
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter number:");
//reading input number as string
		String input = scanner.nextLine();
//creating array of integers with length same as input
		int integerArray[] = new int[input.length()];
//forming string array by splitting above string array with delimitter ""
		String inputStringArray[] = input.split("");//string array {2,3,4}
//
		for (int i = 0; i < inputStringArray.length; i++) {
//converting to integer array{1,2,3}
			integerArray[i] = Integer.parseInt(inputStringArray[i]);

		}
//sort the integerArray
		Arrays.sort(integerArray);
//loop integerArray till its length
		for (int i = 0; i < integerArray.length; i++) {
//not sure what we are doing in below inner loop?? please advise
			for (int j = 0; j < i + 1; j++) {

				System.out.print(integerArray[i]+" ");

			}

			System.out.println();

		}

	}
}

Open in new window

output i got is

Please enter number:
124
1
2 2
4 4 4

i was not clear on inner for loop as below?

//not sure what we are doing in below inner loop?? please advise
			for (int j = 0; j < i + 1; j++) {

				System.out.print(integerArray[i]+" ");

Open in new window




rest of program i understood. i was not sure on how to get spacing so that it forms kind of triangle shape as below


  1
 22
333


not like below

123
1
22
333
I have lost my spoon, but Gerwin Jansen gave you a hint for this as well.
Avatar of gudii9

ASKER

. If you want a real pyramid, calculate the amount of space you need, based on length and index
i see. let me think
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;

public class TestClass {
	public static void main(String[] args) {
		// getting input
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter number:");
		// reading input number as string
		String input = scanner.nextLine();
		// creating array of integers with length same as input
		int integerArray[] = new int[input.length()];
		// forming string array by splitting above string array with delimitter
		// ""
		String inputStringArray[] = input.split("");// string array {2,3,4}
		//
		for (int i = 0; i < inputStringArray.length; i++) {
			// converting to integer array{1,2,3}
			integerArray[i] = Integer.parseInt(inputStringArray[i]);

		}
		// sort the integerArray
		Arrays.sort(integerArray);
		// loop integerArray till its length
		int len = integerArray.length;
		for (int i = 0; i < len; i++) {

			// Printing i spaces at the beginning of each row

			for (int j = 1; j <= i; j++) {
				System.out.print(" ");
			}
			// not sure what we are doing in below inner loop?? please advise
			for (int j = 0; j < i + 1; j++) {

				System.out.print(" " + integerArray[i]);

			}

			System.out.println();

		}

	}
}

Open in new window

Please enter number:
1234
 1
  2 2
   3 3 3
    4 4 4 4

i tried as above but spaces are not properly aligned though referring to below sites
http://www.java-examples.com/java-pyramid-5-example
http://javaconceptoftheday.com/how-to-create-pyramid-of-numbers-in-java/
You need to align the single character in the first line with the halfway point of the last line.
Avatar of gudii9

ASKER

let me think.

challenge seems so simple but lot of thinking to teach java to do it
If the first line is one character and the last line is five characters, how many spaces do you need?
Avatar of gudii9

ASKER

Please enter number:
12345

      1//first line one character then it needs 5 spaces at beginning when last line is of five characters
     2 2
   3 3 3
  4 4 4 4
 5 5 5 5 5
Are there supposed to be spaces between each digit?
Avatar of gudii9

ASKER

actually no. i will remove
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;

public class TestClass {
	public static void main(String[] args) {
		// getting input
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter number:");
		// reading input number as string
		String input = scanner.nextLine();
		// creating array of integers with length same as input
		int integerArray[] = new int[input.length()];
		// forming string array by splitting above string array with delimitter
		// ""
		String inputStringArray[] = input.split("");// string array {2,3,4}
		//
		for (int i = 0; i < inputStringArray.length; i++) {
			// converting to integer array{1,2,3}
			integerArray[i] = Integer.parseInt(inputStringArray[i]);

		}
		// sort the integerArray
		Arrays.sort(integerArray);
		// loop integerArray till its length
		int len = integerArray.length;
		for (int i = 0; i < len; i++) {

			// Printing i spaces at the beginning of each row

			/*for (int j = 1; j <= i; j++) {
				System.out.print(" ");
			}*/
			// not sure what we are doing in below inner loop?? please advise
			for (int j = 0; j < i + 1; j++) {

				System.out.print(integerArray[i]);

			}

			System.out.println();

		}

	}
}

Open in new window


removed the space
Please enter number:
12345
1
22
333
4444
55555
Avatar of gudii9

ASKER

You need to align the single character in the first line with the halfway point of the last line.

is it same with even or odd number of digits of given number??
The integers will do the right thing if you let them.
Avatar of gudii9

ASKER

import java.util.Scanner;
 
public class MainClass
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
 
        //Taking noOfRows value from the user
 
        System.out.println("How Many Rows You Want In Your Pyramid?");
 
        int noOfRows = sc.nextInt();
 
        //Initializing rowCount with 1
 
        int rowCount = 1;
 
        System.out.println("Here Is Your Pyramid");
 
        //Implementing the logic
 
        for (int i = noOfRows; i >= 1; i--)
        {
            //Printing i*2 spaces at the beginning of each row
 
            for (int j = 1; j <= i*2; j++)
            {
                System.out.print(" ");
            }
 
            //Printing j where j value will be from i to noOfRows
 
            for (int j = i; j <= noOfRows; j++)             
            {
                System.out.print(j+" ");
            }
 
            //Printing j where j value will be from noOfRows-1 to i
             
            for (int j = noOfRows-1; j >= i; j--)
            {                 
                System.out.print(j+" ");             
            }
             
            System.out.println();
 
            //Incrementing the rowCount
 
            rowCount++;
        }
    }
}

Open in new window


How Many Rows You Want In Your Pyramid?
9
Here Is Your Pyramid
                  9
                8 9 8
              7 8 9 8 7
            6 7 8 9 8 7 6
          5 6 7 8 9 8 7 6 5
        4 5 6 7 8 9 8 7 6 5 4
      3 4 5 6 7 8 9 8 7 6 5 4 3
    2 3 4 5 6 7 8 9 8 7 6 5 4 3 2
  1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1


above similar to my challenge but it uses just row count where as i am using my numbers sorted ordered ones
Avatar of gudii9

ASKER

not sure why are they incrementing rowcount at the end?


 System.out.println();
 
            //Incrementing the rowCount
 
            rowCount++;
        }

do i need to do similar step?
Avatar of gudii9

ASKER

referring below link which does not talk about spaces though

https://www.youtube.com/watch?v=xpJdjyekKJg

below link has space stuff also

https://www.youtube.com/watch?v=tV2zUoCxFPg

i like the attached picture whichi should folow variables i then j then sp ie space and then output as 4 columns to manually debug on paper with pencil
pyramidSpace1.png
pyramidSpace2.png
Avatar of gudii9

ASKER

public class Pyramid6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i, j, sp = 5;
		// int j;
		for (i = 1; i <= 4; i++) 
		{
			for (j = 0; j <= sp; j++) 
			{
				System.out.print(" ");
			}
			for (j = 1; j <= i; j++) 
			{
				System.out.print(i + " ");
			}
			System.out.println();
			sp--;
		}
	}

}

Open in new window


above printed below

      1
     2 2
    3 3 3
   4 4 4 4


for given number i have modify
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;

public class TestClass {
	public static void main(String[] args) {
		// getting input
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter number:");
		// reading input number as string
		String input = scanner.nextLine();
		// creating array of integers with length same as input
		int integerArray[] = new int[input.length()];
		// forming string array by splitting above string array with delimitter
		// ""
		String inputStringArray[] = input.split("");// string array {2,3,4}
		//
		for (int i = 0; i < inputStringArray.length; i++) {
			// converting to integer array{1,2,3}
			integerArray[i] = Integer.parseInt(inputStringArray[i]);
		}
		// sort the integerArray
		Arrays.sort(integerArray);
		// loop integerArray till its length
		int len = integerArray.length;//same as space
		for (int i = 1; i <= len; i++) {
			// Printing i spaces at the beginning of each row

			for (int j = 0; j <= len; j++) {
				System.out.print(" ");
			}
			// not sure what we are doing in below inner loop?? please advise
			for (int j = 0; j < i + 1; j++) {
				System.out.print(integerArray[i]);
			}
			System.out.println();
			len--;
		}
	}
	/*// TODO Auto-generated method stub
	int i, j, sp = 5;
	// int j;
	for (i = 1; i <= 4; i++) 
	{
		for (j = 0; j <= sp; j++) 
		{
			System.out.print(" ");
		}
		for (j = 1; j <= i; j++) 
		{
			System.out.print(i + " ");
		}
		System.out.println();
		sp--;
	}*/

}

Open in new window


i changed as above still getting wrong output

Please enter number:
12345
      22
     333
    4444

please advise
You seem to be programming at random without thinking.
You were close here   41875419.

If the first line is one character and the last line is five characters, how many spaces do you need?  (to add to the first line)

Answer the question and write a formula.
If the first line is one character and the last line is n characters, how many spaces do you need for each line?
Think in pseudo first ;)
Avatar of gudii9

ASKER

Sure
Avatar of gudii9

ASKER

my code works fine for  below scenario
public class Pyramid6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i, j, sp = 5;
		// int j;
		for (i = 1; i <= 4; i++) 
		{
			for (j = 0; j <= sp; j++) 
			{
				System.out.print(" ");
			}
			for (j = 1; j <= i; j++) 
			{
				System.out.print(i + " ");
			}
			System.out.println();
			sp--;
		}
	}

}

Open in new window


      1
     2 2
    3 3 3
   4 4 4 4


where ones needs to be printed once and 2's twice and 3's thrice 4's four times etc.

i need to extend this logic for my original challenge where each sorted digit of number shoudl come once twice thrice four times etc

You seem to be programming at random without thinking.
You were close here   41875419.

If the first line is one character and the last line is five characters, how many spaces do you need?  (to add to the first line)

Answer the question and write a formula.

i think it is 3rd position as you mentioned earlier

You need to align the single character in the first line with the halfway point of the last line

how to write psuedo code on challenge for which i do not know approach??
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;

public class TestClass {
	public static void main(String[] args) {
		// getting input
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter number:");
		// reading input number as string
		String input = scanner.nextLine();
		// creating array of integers with length same as input
		int integerArray[] = new int[input.length()];
		// forming string array by splitting above string array with delimitter
		// ""
		String inputStringArray[] = input.split("");// string array {2,3,4}
		//
		for (int i = 0; i < inputStringArray.length; i++) {
			// converting to integer array{1,2,3}
			integerArray[i] = Integer.parseInt(inputStringArray[i]);
		}
		// sort the integerArray
		Arrays.sort(integerArray);
		// loop integerArray till its length

		
		int len = integerArray.length;//same as space
		int sp=len;
		for (int i = 0; i <= len; i++) {
			// Printing i spaces at the beginning of each row

			for (int j = 0; j <= (sp/2)+1; j++) {
				System.out.print(" ");
			}
			// not sure what we are doing in below inner loop?? please advise
			for (int j = 0; j < i+1 ; j++) {
				System.out.print(integerArray[i]);
			}
			System.out.println();
			sp--;
		}
	}
	/*// TODO Auto-generated method stub
	int i, j, sp = 5;
	// int j;
	for (i = 1; i <= 4; i++) 
	{
		for (j = 0; j <= sp; j++) 
		{
			System.out.print(" ");
		}
		for (j = 1; j <= i; j++) 
		{
			System.out.print(i + " ");
		}
		System.out.println();
		sp--;
	}*/

}

Open in new window


i wrote as above and got below output

Please enter number:
46139
    Exception in thread "main" 1
    33
   444
   6666
  99999
  java.lang.ArrayIndexOutOfBoundsException: 5
      at TestClass.main(TestClass.java:36)


not sure why i am getting ArrayIndexOutOfBoundsException
spaces not yet aligned completely
please advise
When you scan a line of input, you have to find the smallest integer because that is the one you have to print out first.

You also have to count the input characteristics, because that will tell you have many rows to print out and how many characters there will be in the last row.

If you are going to print n rows, you could print (n-1)/2 spaces before the single character in the first row, (n-2)/2 spaces before the two characters in the second row, . . , and (n-n)/2 spaces before the n characters in the nth row.
Avatar of gudii9

ASKER

in whole google there is not one example similar to my challenge. i wonder why?
Can you explain where you got your challenge from?  It isn't that difficult to be frank.
Avatar of gudii9

ASKER

It isn't that difficult to be frank.
i am struck with spaces.

import java.util.Arrays;
import java.util.Scanner;

public class TestClass {
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("givenNumber is==>");
		String input = scanner.nextLine();
		System.out.println("input-->" + input);
		// building integer array of input length ie 3 for 123
		int integers[] = new int[input.length()];

		String inputArray[] = input.split("");
		// System.out.println("input length-->"+ inputArray.length()));

		for (int i = 0; i < inputArray.length; i++) {

			integers[i] = Integer.parseInt(inputArray[i]);

		}

		Arrays.sort(integers);

		int len = integers.length;
		for (int i = 0; i < len; i++) {

			for (int j = 0; j < i + 1; j++) {

				System.out.print(integers[i]);

			}

			System.out.println();

		}

	}
}

Open in new window



givenNumber is==>
1234
input-->1234
      1
    22
  333
4444

above code produce below output like perpendicular triangle.

i want it like pyramid or equilateral triangle which i cannot get. please advise
SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

any sample video on this kind of pyramid example which forms as equilateral triangle not perpendicular triangle
I don't think so, did you follow up on the advice I gave?
Avatar of gudii9

ASKER

i followed all that i could but no luck yet? can you advise with working code?
Go back to Best Attempt
But print some spaces at the beginning of each line.
How many spaces?   you may ask.

Since there will be a total of six lines:  try (6-n)/2  where n=1 for the first line and so on.
Avatar of gudii9

ASKER

why we need integer array for this solution?
Avatar of gudii9

ASKER

why we need  integerArray here?

      int integerArray[] = new int[input.length()];

import java.util.Arrays;
import java.util.Scanner;

public class TestClass {
	public static void main(String[] args) {
		// getting input
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter number:");
		// reading input number as string
		String input = scanner.nextLine();
		// creating array of integers with length same as input
		int integerArray[] = new int[input.length()];
		// forming string array by splitting above string array with delimitter
		// ""
		String inputStringArray[] = input.split("");// string array {2,3,4}
		//
		for (int i = 0; i < inputStringArray.length; i++) {
			// converting to integer array{1,2,3}
			integerArray[i] = Integer.parseInt(inputStringArray[i]);

		}
		// sort the integerArray
		Arrays.sort(integerArray);
		// loop integerArray till its length
		int len = integerArray.length;
		for (int i = 0; i < len; i++) {

			// Printing i spaces at the beginning of each row

			/*for (int j = 1; j <= i; j++) {
				System.out.print(" ");
			}*/
			// not sure what we are doing in below inner loop?? please advise
			
			/*But print some spaces at the beginning of each line.
			How many spaces?   you may ask.

			Since there will be a total of six lines:  try (6-n)/2  5/2 is 2 then 2nd line 2
			where n=1 for the first line and so on.*/
			
			
			for (int j = 0; j < ((6-j)/2); j++) {

				System.out.print(" ");

			} 
			for (int j = 0; j < i + 1; j++) {

				System.out.print(integerArray[i]);

			}
			
			

			System.out.println();

		}

	}
}

Open in new window


i wrote as above and got below output. please advise


Please enter number:
123456
  1
  22
  333
  4444
  55555
  666666
You have to save and sort the input string.  An array is the obvious choice.  If you have another method, you can try it.
You don't need an array to print out the lines.  A for loop/counter will certainly work.

Why is your test input a sorted string?  You are missing part of the problem.
And you still don't have a pyramid.
Avatar of gudii9

ASKER

Why is your test input a sorted string?  You are missing part of the problem.
And you still don't have a pyramid.

i know but not sure on how?
Is this the kind of thing you mean?
http://technojeeves.com/jws/pyramid/pyramid.jnlp
Avatar of gudii9

ASKER

something blocking to run above

i hupgraded java also now
You need to explain what goes wrong, experts are not mind readers.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

for given number 5371 above gave result as attached.
     1
    7 7
  3 3 3
5 5 5 5
i want instead as below(i.e ascending sorted order of numbers as we move from first row to second and then to third then to fourth row etc)

     1
    3 3
  5 5 5
7 7 7 7
pyramdid.png
Avatar of gudii9

ASKER

i wonder why i cannot unzip to see jar contents. please advise
>> i cannot unzip
What did you try exactly and what error did you get?
Avatar of gudii9

ASKER

What did you try exactly and what error did you get?

i right clicked on the jar file and selected 7-zip then
Extract Here

says
cannot open file as archive?
try with jar instead:

jar xf <jar file>
i want instead as below(i.e ascending sorted order of numbers as we move from first row to second and then to third then to fourth row etc)
I could probably make that app do that but it would have to be a small (cheap) Gig
Avatar of gudii9

ASKER

try with jar instead:

jar xf <jar file>

to unzip to see source?
Avatar of gudii9

ASKER

This expert suggested creating a Gigs project.

i cannot spend 50$ for learning this challenge concept
>> to unzip to see source
I hope you are not thinking that a .jar file is containing java source code. It contains classes (compiled java source code).
>> i cannot spend 50$ for learning this challenge concept
That is up to you to decide of course.

This question has been going on for quite some time, there have been a lot of hist to help you solve this challenge. I'm thinking there's really enough information here for you to work out the issue and complete this challenge.
Could you summarize the progress you have made in the past 54 days?
Avatar of gudii9

ASKER

can some one post the solution please. few lines of code equal to 1000 comments.
i cannot spend 50$ for learning this challenge concept
I'm prepared to do it with you on Live
Avatar of gudii9

ASKER

how to do live?
Click on https://www.experts-exchange.com/live/ 
I've only used Live from the other end so can't say exactly what to do. Should be pretty simple
Avatar of gudii9

ASKER

i will connect with you soon live. i like this feature
Avatar of gudii9

ASKER

looks like live is paid service which i cannot aford
Avatar of gudii9

ASKER

can some one post the solution as i ran out of all ideas for this challenge?
Avatar of gudii9

ASKER

package com.pyramid;
public class Pyramid
{
    public static void main( String[] args )
    {
        int[] a = { 1, 2, 3, 4, 5 };
        for( int i=0 ; i < a.length ; i++ )
        {
            for( int j=a.length-i-1 ; j > 0 ; j-- )
                System.out.print( " " );
            for( int j=0 ; j <= i ; j++ )
                System.out.print( a[i] + " " );
            System.out.println();
        }
    }
}

Open in new window


above gives below output

    1
   2 2
  3 3 3
 4 4 4 4
5 5 5 5 5


i need to see how to give from scanner and then sort then generate above traingle shape
What output do you expect for  
     a = { 3, 2, 1, 3, 4 }   and   a = { 3, 3, 3, 3, 3 }  ?

Are those valid input strings?

Open in new window

Pseudo code for a simple sort:
for j = 0 to a.length
   find the index k of the minimum value in a
   b[ j ]  =  a[ k ]
   a[ k ]  =  12

Open in new window

Avatar of gudii9

ASKER

What output do you expect for  
     a = { 3, 2, 1, 3, 4 }   and   a = { 3, 3, 3, 3, 3 }  ?

Are those valid input strings?

for this challenge those inputs are invalid.

All should be different numbers without any repetitiomn
Did the simple sorting hints help?

If you sort the input before passing it to the print routine, then there is
no problem with inputs like  { 3, 2, 1, 3, 4 }  and  { 3, 3, 3, 3, 3 }.
Avatar of gudii9

ASKER

can you please post complete solution. I am about to give up on this
Avatar of gudii9

ASKER

did not get complete solution i was looking
Hello gudii9, I'm thinking this question has been answered, you are asking for this:

    0
   33
  444
7777
99999

and the output you get through comment https://www.experts-exchange.com/questions/28981110/numbers-ascending-pyramid.html?anchorAnswerId=41943135#a41943135 is this:

    1
   2 2
  3 3 3
 4 4 4 4
5 5 5 5 5

That is the answer you are looking for, right? The question you posted is a challenge for you, this implies that experts will help you to get there, by not posting the full code, otherwise there would be no challenge in it, correct?

What is it exactly why you think the above isn't the answer to your challenge?
Avatar of gudii9

ASKER

What is it exactly why you think the above isn't the answer to your challenge?

i do not know how to approach this solution which was my original question

     0
   33
  444
 7777
99999
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

i am sick and tired of one more and one more try.

if you can give complete solution please give. otherwise i will close this question as i have not got solution i need
Failure is always an option.
Avatar of gudii9

ASKER

sort the input string
find the length of the sorted string

what collection should i use to sort
Avatar of gudii9

ASKER

There was a hint for that:  Sorting Hint


above link not working for me
Avatar of gudii9

ASKER

public class PyramidNoSorting
{
    public static void main( String[] args )
    {
        int[] a = { 6,1, 2, 3, 4, 5 };
        for( int i=0 ; i < a.length ; i++ )
        {
            for( int j=a.length-i-1 ; j > 0 ; j-- )
                System.out.print( " " );
            for( int j=0 ; j <= i ; j++ )
                System.out.print( a[i] + " " );
            System.out.println();
        }
    }
}

Open in new window


import java.util.Arrays;
import java.util.Scanner;
 
public class PyramidSortingNotWorkingExpected
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        

        String input = scanner.nextLine();

        int integers[] = new int[input.length()];

        String inputArray[] = input.split("");

        

        for(int i = 0; i < inputArray.length; i++){

                integers[i] = Integer.parseInt(inputArray[i]);

        }

        

    
        Arrays.sort(integers);

        

        for(int i = 0; i < integers.length; i++){

                for(int j = 0; j< i; j++){

                        System.out.print(integers[i]);

                }

                System.out.println();

        }


}
}

Open in new window



read the input string
sort the input string
find the length of the sorted string

for i = 1 to the len
    print (len-n)/2  spaces
    print n copies of the nth digit 

Open in new window


i need rope to tie above 3 things together
Avatar of gudii9

ASKER

and i cannot find rope in my mind now
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;
 
public class PyramidSortingNotWorkingExpected
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        

        String input = scanner.nextLine();

        int integers[] = new int[input.length()];

        String inputArray[] = input.split("");

        

        for(int i = 0; i < inputArray.length; i++){

                integers[i] = Integer.parseInt(inputArray[i]);
                System.out.println("values--->"+integers[i]);

        }

        

    
        Arrays.sort(integers);

        

        /*for(int i = 0; i < integers.length; i++){

                for(int j = 0; j< i; j++){

                        System.out.print(integers[i]);

                }

                System.out.println();*/

       // }


}
}

Open in new window


taking baby steps
above gave below
9103
values--->9
values--->1
values--->0
values--->3


now let me stort
Here is the expanded psuedo code for a simple sort.  

read input_str
for j = 0 to length( input_str)             On each pass through the loop find the minimum value in input_str
   min = 11
   for k = 0 to length( input_str)
      if input_str[ j] < min
      min = input_str[ j]
      index = j
   output_str[ j ]  =  a[ index]             Then it copies it into the proper location in output_str
   input_str[ index]  =  12                  And it marks it as done in the input_str

Open in new window

Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;
 
public class PyramidSorting
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        

        String input = scanner.nextLine();

        int integers[] = new int[input.length()];

        String inputArray[] = input.split("");

        

        for(int i = 0; i < inputArray.length; i++){

                integers[i] = Integer.parseInt(inputArray[i]);
                System.out.println("before sort"+integers[i]);

        }

        

    
        Arrays.sort(integers);
        
        
        for(int i = 0; i < integers.length; i++){

            //integers[i] = Integer.parseInt(inputArray[i]);
            System.out.println("after sort"+integers[i]);

    }

        

        //int[] a = { 6,1, 2, 3, 4, 5 };
       /* for( int i=0 ; i < a.length ; i++ )
        {
            for( int j=a.length-i-1 ; j > 0 ; j-- )
                System.out.print( " " );
            for( int j=0 ; j <= i ; j++ )
                System.out.print( a[i] + " " );
            System.out.println();
        }*/


}
}




/*read the input string
sort the input string
find the length of the sorted string

for i = 1 to the len
    print (len-n)/2  spaces
    print n copies of the nth digit */

Open in new window


271
before sort2
before sort7
before sort1
after sort1
after sort2
after sort7


nw i need to arrange in pyramid with 2 for loops
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;
 
public class PyramidSorting
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        

        String input = scanner.nextLine();

        int integers[] = new int[input.length()];

        String inputArray[] = input.split("");

        

        for(int i = 0; i < inputArray.length; i++){

                integers[i] = Integer.parseInt(inputArray[i]);
                System.out.println("before sort"+integers[i]);

        }

        

    
        Arrays.sort(integers);
        
        
        for(int i = 0; i < integers.length; i++){

            //integers[i] = Integer.parseInt(inputArray[i]);
            System.out.println("after sort"+integers[i]);

    }

        

        //int[] a = { 6,1, 2, 3, 4, 5 };
        for( int i=0 ; i < integers.length ; i++ )
        {
            for( int j=integers.length-i-1 ; j > 0 ; j-- )
                System.out.print( " " );
            for( int j=0 ; j <= i ; j++ )
                System.out.print( integers[i] + " " );
            System.out.println();
        }


}
}




/*read the input string
sort the input string
find the length of the sorted string

for i = 1 to the len
    print (len-n)/2  spaces
    print n copies of the nth digit */

Open in new window



waaaaaaaaaaaaaaaaaav finally i see desired output

34180
before sort3
before sort4
before sort1
before sort8
before sort0
after sort0
after sort1
after sort3
after sort4
after sort8
    0
   1 1
  3 3 3
 4 4 4 4
8 8 8 8 8
Avatar of gudii9

ASKER

tell me how to be as patience and persistent as you guys are.

i think i gave up early and do not think deep and hard?
Avatar of gudii9

ASKER

where to find more of these kind of challenges to practice?
Avatar of gudii9

ASKER

any improvements/modifications to my code?
Avatar of gudii9

ASKER

import java.util.Arrays;
import java.util.Scanner;

public class PyramidPrinter {

	public static void main(String[] args) {
		PyramidPrinter printer = new PyramidPrinter();
		printer.print(getUserInput());
	}

	// private static final Scanner input = new Scanner();

	/*
	 * private static String getUserInput() {
	 * System.out.print("Enter a sequence of digits, no spaces: "); return
	 * input.nextLine(); }
	 */

	private static String getUserInput() {
		System.out.println("Enter a sequence of digits, no spaces: ");
		Scanner scanner = new Scanner(System.in);
		String input = scanner.nextLine();
		return input; // input.nextLine();

	}

	/*
	 * private void print(String sequence) { }
	 */

	/*
	 * private void print(String sequence) { char[] digits =
	 * sequence.toCharArray(); }
	 */

	/*
	 * private void print(String sequence) { char[] digits =
	 * sequence.toCharArray(); displayAsPyramid(digits); }
	 */
	/*
	 * private void print(String sequence) { char[] digits =
	 * sequence.toCharArray(); Arrays.sort(digits); displayAsPyramid(digits); }
	 * 
	 * private void displayAsPyramid(char[] digits) { for (char i : digits) {
	 * System.out.println(i); } }
	 */
	/*
	 * private void print(String sequence) { char[] digits =
	 * sequence.toCharArray(); Arrays.sort(digits); displayAsPyramid(digits); }
	 * 
	 * private void displayAsPyramid(char[] digits) { for (int i = 0; i <
	 * digits.length; i++) { printSpaces(digits.length - 1 - i);
	 * System.out.println(digits[i]); } }
	 * 
	 * private void printSpaces(int n) { for (int i = 0; i < n; i++) {
	 * System.out.print('.'); } }
	 */

	private void print(String sequence) {
		char[] digits = sequence.toCharArray();
	     Arrays.sort(digits);
		displayAsPyramid(digits);
	}

	private void displayAsPyramid(char[] digits) {
		for (int i = 0; i < digits.length; i++) {
			printSpaces(digits.length - 1 - i);
			printRow(digits[i], i + 1);
		}
	}

	private void printSpaces(int n) {
		for (int i = 0; i < n; i++) {
			System.out.print(' ');
		}
	}

	private void printRow(char c, int n) {
		for (int i = 0; i < n; i++) {
			System.out.printf("%c ", c);
		}
		System.out.println();
	}

}

Open in new window


i wrote more OOP and OOD way

got below correct output
Enter a sequence of digits, no spaces:
7209
   0
  2 2
 7 7 7
9 9 9 9