Deriving a common pattern to solve any pattern problem.
It is been a long time since I did some patterns. I remember developing an animated excavator using html and css over codepen.io. My son had an obsession with singing the excavator song from Blippi's youtube channel. I was practicing html and css in my spare time and doing regular web designs wasn't that motivating at that point in time. And then I did a few more cartoons that my son might like. Let me share the link here. Patterns are fun too. To refresh your way of thinking over problems. Starting small is always helpful in learning some complex things. Let us pick some patterns in this blog and see how we can create them and derive a simple logic that you can apply to any pattern problem.
Pattern 1: zero
As we see this has two dimensions. Let us consider the rows to be held in variable 'i' and columns to be held in variable 'j'. This way we can break the problem that for every row, we check if we want to print a star for every column in that row. We run the rows through a loop till n(say 10). So for every row until 10 rows, we test and run a nested loop for columns to print star. Check the below code that gives a better idea of rows and columns and conditions for printing the above pattern.
public class Patterns {
public static void main(String args[]) {
int n=10;
for (int i=0; i<=n; i++){ //this is the loop for rows
if(i==0 || i==n){ //this is the condition for which we want to print stars in columns
for(int j=0; j<=n; j++){ //this is the loop for columns
System.out.print('*');
}
} else {
for(int j=0; j<=n; j++){
if(j==0 || j==n){
System.out.print('*');
} else {
System.out.print(' ');
}
}
}
System.out.println(' ');
}
}
}
Pattern 2: letter 'T'
For printing a T pattern, let us use similar logic to the above but by using the 'j' loop only for i=0 and i= n//2 conditions.
public class Patterns {
public static void main(String args[]) {
int n=10;
for (int i=0; i<=n; i++){
if(i==0){
for(int j=0; j<=n; j++){
System.out.print('*');
}
} else {
for(int j=0; j<=n; j++){
if(j==n/2){
System.out.print('*');
} else {
System.out.print(' ');
}
}
}
System.out.println(' ');
}
}
}
Pattern 3: Star- A triangle whose tip is on another triangle.
Now that we understood the 'i' and 'j' loops for rows and columns of a pattern, we use the same thinking process to solve any pattern problem. Let us now develop a pattern of Star. Printing stars to derive star pattern :)
public class Patterns {
public static void main(String args[]) {
int n=10;
for (int i= 0; i<= rows-1 ; i++) {
for (int j=0; j <i; j++) {
System.out.print(" ");
}
for (int k=i; k<=rows-1; k++) {
System.out.print("*" + " ");
}
System.out.println("");
}
for (int i= rows-1; i>= 0; i--) {
for (int j=0; j< i ;j++) {
System.out.print(" ");
}
for (int k=i; k<=rows-1; k++) {
System.out.print("*" + " ");
}
System.out.println("");
}
}
}
As you observed from the above patterns, we have to first focus on generalizing the number of rows and figuring out the conditions for which the row and column have to print a star or space.
I hope this was helpful. Thank you. Happy Learning!