• 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 of numbers

 
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:
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...
 
Ranch Hand
Posts: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:


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: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

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: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:


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: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:
I was following Junilu's advices step by step and got solution. Have you got yours?
 
sai rama krishna
Ranch Hand
Posts: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

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: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:
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: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:


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: 8880
638
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

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.
 
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

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.
 
Sheriff
Posts: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Sheriff
Posts: 7125
184
  • Number of slices to send:
    Optional 'thank-you' note:

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: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:


i am filling the methods as above

 
sai rama krishna
Ranch Hand
Posts: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

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: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

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.
 
Saloon Keeper
Posts: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:

Where's your loop?

printLine( is, row ) // and let printLine figure out the int.
 
sai rama krishna
Ranch Hand
Posts: 955
2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

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: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:

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: 955
    2
    • Number of slices to send:
      Optional 'thank-you' note:


    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
    Saloon Keeper
    Posts: 10797
    86
    • Number of slices to send:
      Optional 'thank-you' note:
    Not sure why you'd expect anything different. You haven't yet implemented lines 37 and 43.
     
    Carey Brown
    Saloon Keeper
    Posts: 10797
    86
    • Number of slices to send:
      Optional 'thank-you' note:
    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: 17652
    300
    • Number of slices to send:
      Optional 'thank-you' note:

    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: 955
    2
    • Number of slices to send:
      Optional 'thank-you' note:


    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: 8880
    638
    • Number of slices to send:
      Optional 'thank-you' note:
    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: 8880
    638
    • Number of slices to send:
      Optional 'thank-you' note:

    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: 955
    2
    • Number of slices to send:
      Optional 'thank-you' note:
    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
    Saloon Keeper
    Posts: 10797
    86
    • Number of slices to send:
      Optional 'thank-you' note:

    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
    Saloon Keeper
    Posts: 10797
    86
    • Number of slices to send:
      Optional 'thank-you' note:
    To see what's happening with your spaces replace
    " "
    with
    "."
     
    sai rama krishna
    Ranch Hand
    Posts: 955
    2
    • Number of slices to send:
      Optional 'thank-you' note:


    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

     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic
    vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
    http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |