Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

pyramid of numbers  RSS feed

 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me look a bit more to a thread, so I could just basically repeat what you have been told already (might will rephrase my way). Few minutes...
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


above gave reverse sorted but space issues also resolved
4517
integerArray is [0, 0, 0, 0]
Splitted the String up into an array that has 1 element per character in the String [4, 5, 1, 7]
integerArray before sorting[4, 5, 1, 7]
integerArray after sorting[1, 4, 5, 7]
    7
   5 5
  4 4 4
1 1 1 1
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i want to get output like below instead?


    1
   4 4
  5 5 5
7 7 7 7


how to get it? please advise

I know my approach is bit hapazard. I need to write the below things
1. high level approach
2. next granular level approach till i can code
3. put proper method names and variable names
4. write the psuedo code
5. write actual code in editor or IDE
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:I need to write the below things
Cool, it seems you understood, we are waiting your re-written solution, so we could take it from there.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of reason why i took about one months for this challenge which according to experts like you takes 15 minutes which is what i am also aiming at
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when i take in last for loop j instead of i-1

i got below output
359
integerArray is [0, 0, 0]
Splitted the String up into an array that has 1 element per character in the String [3, 5, 9]
integerArray before sorting[3, 5, 9]
integerArray after sorting[3, 5, 9]
   3
  3 5
3 5 9


how to optimize my code to eliminate 4 for loops etc
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was following Junilu's advices step by step and got solution. Have you got yours?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. it was helpful to see all above great comments which pointed me and lead in right direction



one question pending is

But i want to get output like below instead?


    1
   4 4
  5 5 5
8  8 8 8
9 ...........etc



as of now i am getting like

      9
    8 8
   5 5 5
  4 4 4 4
1 1 1 1 1

do i need to change my approach from string array to some other colection object which sorts in ascending order rather than descending? please advise
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:do i need to change my approach

Yes, you said that already

sai rama krishna half an hour ago wrote:I know my approach is bit hapazard. I need to write the below things
1. high level approach
2. next granular level approach till i can code
3. put proper method names and variable names
4. write the psuedo code
5. write actual code in editor or IDE
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the solution supposed to be around 8 your defined methods (names can differ):

1. main
2. printAsTriangle
3. getUserNumbers
4. sortNumbers
5. printNumbers
6. printLine
7 printSpaces
8 printElements

 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


got below output

213
integerArray is [0, 0, 0]
integerArray before sorting...[2, 1, 3]
      3 2 1


need to modify somethings
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:need to modify somethings

Don't modify anything at the moment. What you need to do, is to THINK, and do it for a while. No one sits down and writes code right away. Once you start your first attempt, at least you should have a solid confidence on what you are going to write, as a result you should be accurate with your very first attempt, or very close to, which is not a case in your case, as you may noticed already. Do you see a problem? You're almost guessing. Yesterday I was solving this exercise too, and what do you think, I just started writing code? Nope, I took a piece of paper (I have a lot in my drawer under the table) and solved it on it first, so it looked ok, then I wrote the code in my IDE to ensure it is indeed ok - it wasn't, so i went back to paper to find where I did mistake, next attempt did work as expected. If you are guessing there is a small chances you'll get it right any time soon.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . . No one sits down and writes code right away. . . .
Oh, yes, they do. (You can tell what time of year it is, can't you ‍).
You can recognise people who write code straight away by their hair (all torn out), their fingernails (bitten off) and their coffee (all spilt). Then they come here and try guessing how to change their code.
Liutauras is correct. You start with pencil and paper, like the student I was helping face to face last night (but I told him to sign up on Coderanch). You also need an eraser, because you will have to destroy the evidence change the algorithm slightly when you find out what works.
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started reading what's shaping up to be a really good book. It's called Think Like a Programmer: An Introduction to Creative Problem Solving by V. Anton Spraul.

Here's his first problem solving strategy:
V. Anton Spraul wrote:This is perhaps the most important rule. You must always have a plan, rather than engaging in directionless activity.


I can't believe I haven't heard about this book before. It's exactly the kind of thing many students and their teachers need to read.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So at this point, you just need to replace "*" with a number.
Please ignore, I missed a whole page of responses.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i am filling the methods as above

 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what helped me in solving pyramid of stars and eventually this challenge is dry erase board and dry marker with eraser on top of it. But some times carrying that outside home may not look professional rather carry a book pencil and eraser in the laptop bag.
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:i am filling the methods as above

Alright, so now you know which parts doesn't work for you, and which methods are responsible for that, right?

All where you need to work in (if need) are two methods: printSpaces() and printElements() as these doesn't produce expected output.

Relax about this one below, you just need to add println statement to it
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure. I will try to tweak those two methods to get desired output.



what surprised me is above method call(printAsTriangle) which takes other method call(getUserNumbers()) as parameter.

Usually we do not see that scenario right where one method call take other method call as parameter?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am looking for good sites, books, resources to make me good at hands on java coding.

I just found below link with assignments, exercises.
https://programmingbydoing.com/

please advise any other good similar resources to improve my hands on coding skills so that i can eventually do these kind of exercises in 15 minutes like you experts with all clean healthy variable, method names etc

 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:Usually we do not see that scenario right where one method call take other method call as parameter?
Yes and No. Probably for the beginning, for the sake of clarity, you can and maybe even you should write:

It is more readable actually and more flexible, as in case you'd need to use userNumbers somewhere else, you wouldn't need to call that method once again.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Where's your loop?

printLine( is, row ) // and let printLine figure out the int.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my mind i was thinking i was able solve this challenge in 25 lines of code within in main method itself rather than introducing new 5 to 10 new methods but apparently writing all in main with 25 simple lines is not good idea in terms of readability to others or even to me later stages?
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:In my mind i was thinking i was able solve this challenge in 25 lines of code within in main method itself rather than introducing new 5 to 10 new methods but apparently writing all in main with 25 simple lines is not good idea in terms of readability to others or even to me later stages?

Never judge the code by its length, nor think shorter - better. Judge by readability, how quickly you can find the things in the code which are responsible for particular tasks. Your code will need to be read by others, and when you write your code in a main method, no one can understand it - except you, not for long too as it gets forgotten when time gets passed.

Always concentrate on clarity - and when there is just a beginning for you, I'd say - more methods better. Most important thing is - that singular method would do that one thing what it says only.

printAsTriangle it should print numbers as triangle, no more no less.
goToNextLine needs to do what it says, just simply go to next line.
printLine too, it is a bit abstract, but then again, you digging down and you see specifics, sub-parts of this task, not the other different task, but sub-task, it is important.

And always try follow such approach and you'll notice how much easier is to cope with complexity, because at a given time you need to concentrate on a tiny piece of work.
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Yes and No. Probably for the beginning, for the sake of clarity, you can and maybe even you should write:
It is more readable actually and more flexible, as in case you'd need to use userNumbers somewhere else, you wouldn't need to call that method once again.

Well, yes and no.

As Andy Hunt wrote, "Rule #2: Always consider context."

Here's the context:

There is nothing that's too surprising about this.  The method printAsTriangle() is NOT taking another method call, it's still taking an int[] because that is what the method getUserNumbers() returns. You are simply getting rid of an unnecessary temporary variable like int[] userNumbers.

In this context, the temporary variable userNumbers adds no value. It is not used by anything else and its name does not tell you anything more than what the method name getUserNumbers() already does.

Refer to these two symmetrically opposite refactorings and understand the contexts in which they are applied:
  • Inline Temp - http://refactoring.com/catalog/inlineTemp.html
  • Extract Variable - http://refactoring.com/catalog/extractVariable.html

  •  
    sai rama krishna
    Ranch Hand
    Posts: 530
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    as above i modified and came close but all printing in same line

    312
    integerArray is [0, 0, 0]
    integerArray before sorting...[3, 1, 2]
    noOfRows value in printSpaces--->3
          noOfRows value is printElements--->3
    3 2 2 1 1 1
     
    Carey Brown
    Bartender
    Posts: 2996
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not sure why you'd expect anything different. You haven't yet implemented lines 37 and 43.
     
    Carey Brown
    Bartender
    Posts: 2996
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In your
    method you are trying to do too much. Perhaps if you renamed it to
    you'd see that you veered off into the weeds.
     
    Junilu Lacar
    Sheriff
    Posts: 11146
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sai rama krishna wrote:In my mind i was thinking i was able solve this challenge in 25 lines of code within in main method itself rather than introducing new 5 to 10 new methods but apparently writing all in main with 25 simple lines is not good idea in terms of readability to others or even to me later stages?

    Look where it got you. This thread should have no more than 10 posts long. Instead it's well past 50 posts.

    If you had instead attacked this problem in a systematic way, breaking the problem down into increasingly smaller chunks like we showed you, you could have been done with this many, many hours ago. Instead, here we still are. 

    Your gauge should be how long your methods are, not how long your entire program is. 

    Kent Beck, the co-creator of JUnit, author, speaker, and pioneer of Extreme Programming, is reported to write most methods in his programs with only 1 to 3 lines of code. This means he writes many, many methods to accomplish any significantly large task. That's the level of brevity and clarity that you should aspire to. SHORT, SIMPLE, and CORRECT is the goal, not SHORT but SUPER-CONVOLUTED and WRONG.
     
    sai rama krishna
    Ranch Hand
    Posts: 530
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    above kind of messed up spaces


    521
    integerArray is [0, 0, 0]
    integerArray before sorting...[5, 2, 1]
    noOfRows value in printSpaces--->3
      
     

    noOfRows value is printElements--->3
    5
    2 2
    1 1 1



    issue here is pyramid of stars has 2 for loops within one for loop and at the end of two for loops we are doing sysout(with line feed) and the incrementing rowEndPosition as below to set stage for next row to start character after printing the spaces

      System.out.println();
                rowEndPosition++;

    here since we separated to two separate methods those above 2 lines of common code is in the air. not sure where it belongs may be one or two or none of the two methods?
     
    Liutauras Vilda
    Marshal
    Posts: 4641
    318
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm not looking at the moment to your code, but what I could conclude by looking to this your example:
    noOfRows value is printElements--->3
    So you see, that you got amount of elements per row correct. 1st line - 1 element, 2nd line -2 elements...
    What else you can see? That all lines are printed without any leading spaces. So, looking visually to this figure, for the first line you need to add 2 spaces, right? For the second line 1 space, and for the third - none. Can you have a look to loop and see if you could somehow workout needed amount of spaces per iteration out of loop counter?

    By the way, stop printing those before sorting, after sorting.. these are irrelevant here, as values of numbers aren't taken into account.
     
    Liutauras Vilda
    Marshal
    Posts: 4641
    318
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:So, looking visually to this figure, for the first line you need to add 2 spaces, right? For the second line 1 space, and for the third - none. Can you have a look to loop and see if you could somehow workout needed amount of spaces per iteration out of loop counter?

    Good that you took only 3 numbers array, that way is easier to see the key here.

    So, during the first iteration you need to print 2 spaces, second iteration 1 space, third iteration none. Is it somehow related with array's length?
     
    sai rama krishna
    Ranch Hand
    Posts: 530
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    not able to align digit next to space as both are from different method calls??



    313
    integerArray is [0, 0, 0]
    integerArray before sorting...[3, 1, 3]
    noOfRows value in printSpaces--->3
          noOfRows value is printElements--->3
    3
    3 3
    1 1 1

    space is not coming correct before digit
     
    Carey Brown
    Bartender
    Posts: 2996
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sai rama krishna wrote:space is not coming correct before digit

    Follow each line of the code as if you are the computer, it should be pretty obvious why you are not getting any leading spaces.

    (Hmmmm, guess what, you haven't followed any of my previous suggestions. I'm done here.)

    If you are struggling with this, print out the code and mark/highlight each line as you "execute" it in your mind.

    Learning to code also involves learning to debug.
     
    Carey Brown
    Bartender
    Posts: 2996
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To see what's happening with your spaces replace
    " "
    with
    "."
     
    sai rama krishna
    Ranch Hand
    Posts: 530
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Post Yesterday 4:15:26 PM     Subject: pyramid of numbers
    To see what's happening with your spaces replace
    " "
    with
    "."



    spaces are prepending at wrong location as below

    312
    integerArray is [0, 0, 0]
    integerArray before sorting...[3, 1, 2]
    noOfRows value in printSpaces--->3
    ......noOfRows value is printElements--->3
    3
    2 2
    1 1 1

     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!