Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Paul Clapham
• Jeanne Boyarsky
• Knute Snortum
Sheriffs:
• Liutauras Vilda
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
Bartenders:
• Joe Ess
• salvin francis
• fred rosenberger

# What's the pattern?

Ranch Hand
Posts: 58

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!

Marshal
Posts: 24962
61
• 1
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
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
Marshal
Posts: 24962
61
• 1
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?

Ranch Hand
Posts: 231
12
• 1
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
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
This one is better than the first one but still far from the final result
Thanks a lot

Rancher
Posts: 3656
34

still far from the final result

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
• 1
This code prints 10 levels

Marshal
Posts: 6670
178
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
Don't worry I already finished it in another way
Also it's not homework. I'm just doing it for fun.
Thanks though.

Sheriff
Posts: 14768
245

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
What does OP mean?

Knute Snortum
Marshal
Posts: 6670
178
"Original Poster," in this case, you.

Junilu Lacar
Sheriff
Posts: 14768
245

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: 14768
245
• 2
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

 We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad: Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous