+ 2
How can I print any kind of pattern. What are approaches to solve .
10 Respuestas
+ 3
Printing is the easy part.
The challenge is understanding the pattern.
When you say "pattern", it will certainly mean something regular, repeating, symmetrical or otherwise following some sort of RULE. Definitely not something random.
So the big task is to recognize the rules, based on what the pattern looks like, and try to translate it to code.
Typical pattern tasks involve a kind of 2D graphic with symbols or numbers, where the numbers are in some mathematical relationship with their position (of row or column). To solve a pattern, you have to describe this relationship with code!
+ 4
Hi, Avinash Kumar !
I think a good idea is to study other peoples code. People loves to make pattern, because it is fun, so there are a lot of them here on SoloLearn (just search for them). Here are a few different examples:
https://code.sololearn.com/c153Yk1i85qB/?ref=app
https://code.sololearn.com/c7QaoO1djTQu/?ref=app
https://code.sololearn.com/coTo5DOCGdPQ/?ref=app
https://code.sololearn.com/c6lW5LasRkai/?ref=app
+ 2
Tibor Santa Thanks a lot for your valuable tips.
+ 2
# Hi, again Avinash Kumar !
# An amazing patterns is an excellent goal. For now the can serve as an inspiration. But start with something simpler.
# Start with this code an change it to you understand the concept. Then make it to your own:
a = ' '
b = '*'
n = 10
for i in range(n):
s = b + i*a + b
print(s)
else:
t = (n + 2) * b
print(t)
+ 2
Avinash Kumar
Have you tried to print them out, or use == between the expressions? Note: in above the variable a is a space!
The concatenation operator + is not commutative for strings, as it is for numbers; the order on the terms are importent for the outcome. For two strings a and b where a !=b, and a and b is not empty (an empy string):
a + (b + a) != a + (a + b)
and thus:
a + b + a != 2*a + b
+ 2
Per Bratthammar wow. You are such a amazing genius 😍
+ 1
Avinash Kumar
If a, b as above and i is an integer:
b + i*a + b == 2*b +i*a
ONLY IF: i == 0.
In my code above
s = b + i*a + b, gives:
**
* *
* *
* *
* *
* *
* *
* *
* *
* *
************
s = 2*b + i*a, gives:
**
**
**
**
**
**
**
**
**
**
************
and s = i*a + 2*b, gives:
**
**
**
**
**
**
**
**
**
**
************
Try it! 🙂
0
Per Bratthammar your all codes are amazing.I don't have idea how to create patterns like yours.
0
Per Bratthammar why did you use s=b+I*a+b?
You can simply use s=2*b
0
Per Bratthammar I tried your code and output was same . But your logic is also correct