+ 4
What is the best way to understand and display symbol patterns in programming languages?
I'm not very new to programming, nor am I a very advanced programmer, having covered the basics of C and C++. Recently, I began learning Java and stumbled upon a weird star pattern problem. This reminded me that I'm not very good at solving these. So I'm asking you fellas, should I simply look over the solutions for each unique pattern and memorise them? Or perhaps, I believe, I'm missing something and that these problems aren't as hard as I think they are. Maybe there is a method to solving these? Any help is appreciated.
10 ответов
+ 8
I don't understand the obsession with symbol patterns, I have not seen them outside this forum and I had no clue they were so common.
Anyhow you just have to look at how many stars get added or removed each line.
A triangle like
*
**
***
Is one more star per line, therefor if we have 9 lines
for(i = 1; i <= 9; i++){
print "*" * i;
print "\n";
}
If your programming language can't multiply strings, that means another for loop:
for(i = 1; i <= 9; i++){
for(j = 0; j < i; j++) print "*";
print "\n";
}
And if you want your triangle to tilt to the right like
*
**
***
You have to fill the remaining space with spaces first, like
for(i = 1; i <= 9; i++){
print " " * (9-i);
print "*" * i;
print "\n";
}
Or, with loops
for(i = 1; i <= 9; i++){
for(j = 0; j < (9-i); j++) print " ";
for(j = 0; j < i; j++) print "*";
print "\n";
}
etc. Hope that helps.
Maybe if you post the pattern that gave you trouble we can look over it.
+ 7
Adi I really like your approach in using this pattern as a learning exercise. Thanks so much for sharing such insight in your feedback. 😉👌
I have a feeling you're one to watch grow in your coder's journey.
+ 5
Schindlabua LOL... I thought it was just me who thought that. 🤣😂
+ 4
Schindlabua Thank you for your reply. I was actually hoping for a more concrete way that showed how the pattern's x and y-axis can be observed to create appropriate loop statements.
But the string multiplication concept seems to provide a fresh perspective on the subject. Thank you very much.
And I didn't include the pattern in the post because I like to understand things rather than simply copying someone else's code.
+ 4
Adi You're welcome! Good call on doing things yourself.
I think the point of these puzzles is that you have to figure them out on a case-by-case basis though.
I guess the only thing they all have in common is that you are drawing them line-by-line, so think in terms of the y-axis, not x.
+ 3
So much inspiration.. Haha. I love it.
You guys rock. :D
+ 3
you should practice and practice...
if you stuck in a pattern change the loop and try again...
i am sure you will slowly understand the way and also your logical thinking will become strong...
hope my suggestion helped you.. ^_^
+ 2
Regular expressions. This is a well studied problem. The solutions are:
* recursion
* Thompson's construction https://swtch.com/~rsc/regexp/regexp1.html
+ 2
Adi
There are many and many patterns you can first you should try those simple * patterns then you can go for the triangle like shape patterns first with stars then with numbers then you can go for the reverse diamond shape patterns and then pascal's triangle all of this just need some concept of creating patterns and some logic and for the last one you will need some help of mathematics specially permutation and combinations
+ 1
If you memorize anything try to memorize the method while understanding why it's done that way.