• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the pattern?  RSS feed

 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,
How do you make a triangle like this?

              1
            2 3 2
          3 4 5 4 3
        4 5 6 7 6 5 4
      5 6 7 8 9 8 7 6 5
    6 7 8 9 0 1 0 9 8 7 6
  7 8 9 0 1 2 3 2 1 0 9 8 7
8 9 0 1 2 3 4 5 4 3 2 1 0 9 8
I can't seem to find a pattern I can use to write the code...
Thanks!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first line is 1.

The second line is 2 and goes up to 3 and back down to 2.

The third line is 3 and goes up to 5 and back down to 3.

The fourth line is 4 and goes up to 7 and back down to 4.

So the pattern so far is, line N starts with N and goes up to 2N-1 and then back down to N.

It looks like the sixth line breaks the pattern, and it does, but the more complicated pattern says "Only display the last digit of each number". That then seems to work for the rest of the triangle, at least the part we can see.

Does that help?
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Paul! I understood everything until here:

"... but the more complicated pattern says "Only display the last digit of each number". That then seems to work for the rest of the triangle..."

what do you mean by that?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line six isn't "6 7 8 9 10 11 10 9 8 7 6", which is what the original pattern looked like. But it's pretty similar to that, right? So what's the difference?
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The counting follows the sequence 1,2,3,4,5,6,7,8,9,0. After 0, it goes back to 1.

Each line contains 2 more numbers than the previous line.

At the start of each line, the counting increments by 1 but when the counting gets to the middle of the line, it decrements by 1.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help Daniel and Paul.
This is a messed up code I have right now. I don't know what to do.




 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one is better than the first one but still far from the final result
please help
Thanks a lot

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still far from the final result
please help 

Is this a for a graded homework assignment? If so, how can you turn it in as your work if you are having someone else do the work?
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code prints 10 levels


 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel: I know that the basic rule for posting complete solutions is, "If the user has posted a complete solution, you are free to post yours," but this may be a homework assignment, in which case this could encourage the OP to use your solution rather than her own.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry I already finished it in another way
Also it's not homework. I'm just doing it for fun.
Thanks though.


 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Daniel: I know that the basic rule for posting complete solutions is, "If the user has posted a complete solution, you are free to post yours," but this may be a homework assignment, in which case this could encourage the OP to use your solution rather than her own.


Since this has already been out for a while, it's probably too late now to hide the complete solution that was posted.

I would say to the OP, however, Caveat Emptor: If you use the solution that has been offered, it's pretty easy to find and figure out that it has been plagiarized so it may be more trouble than it's worth. Also, frankly, the solution is not a very good one even though it works.  (No offense intended to the poster, just giving an honest opinion)
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does OP mean?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Original Poster," in this case, you.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindsey Brooks wrote:Don't worry I already finished it in another way
Also it's not homework. I'm just doing it for fun.
Thanks though.


Good to hear.

Your code is actually pretty decent for a beginner. I would say you're starting to get a hang of it and more importantly, you're starting to understand the logic and seeing the pattern. I added the same comments I added in your other code. Good job.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you said this is not homework and you're doing it for fun and trying to learn, let me show you my solution.

A few things to keep in mind when you're writing code:
1. Code should be expressive. you should be able to read it and immediately get a sense for what it's doing.
2. You can separate tasks into different levels of abstraction, starting from a high level and drilling down to the minute details.
3. Make your variable names and method names express their intent and make them do what they say they do, nothing more.

Using the outline I gave you in your other problem, you can structure the code better this way:


Except for the elided parts, this is the exact code that I wrote for the problem, no more, no less.  Now, if you want to step up to the challenge and learn a little bit more, figure out what goes inside those three elided for-loops. The comments are optional but I put them in there for your benefit.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
                                                                                                                                                                           
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!