How to master the art of programming “Patterns”?

Aneri Sheth
Technology titbits
Published in
5 min readDec 7, 2017

Punch in the word “pattern” in a dictionary and you get the following definition — “ a consistent, recurring characteristic that helps us to identify a phenomenon and predict its future behavior”

Well, if that definition went right over your head, don’t worry! The following discussion will help get it into your brains. Okay, let’s start.

Assume I start reciting aloud the numbers 1, 3, 5, 7 and suddenly pause and ask you to continue. What will you start reciting?

9, 11, 13, 15, 17, 19…. Right? Why?

It is because you noticed a characteristic i.e. alternate numbers are being recited or odd numbers are being recited. Once your brain picked up this behavior you understood what is going on, you realized it is consistent ( the number doesn’t increment by 3 one time and by some random quantity at another), and as a result you were able to predict the future behavior as well.

This is precisely what we mean by a “pattern”. Let us revisit the definition of pattern and it will now make perfect sense to you.

A consistent, recurring characteristic that helps us to identify a phenomenon and predict its future behavior

What if I now recite numbers 100, 2 ,3, 6, 60 and ask you to continue? Confused? Yes. That is because there is no particular trait or characteristic that you can deduce from these numbers that helps you to decide what number comes next. So NO characteristic, NO consistency and so NO Pattern!

So the question is how does this word make sense in the programming world?

The meaning of pattern in the programming world is no different than the one we just learned.

Assume you are writing a program for the problem discussed above — printing odd numbers.

You are told to print odd numbers till 10.

One way to go about it..

int x = 1;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);

Not a wise one though!! Why? You’ll know.

I am a fickle minded person and now want odd numbers till 20. Here you go again…

int x = 1;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);
x = x + 2;
System.out.println(x);

Now let us try to find a wiser way.

If you notice my last two demands you will see that only the endpoint or stopping number has changed but the thing I want is still the same — odd numbers. So the PATTERN HAS NOT CHANGED!! Now you have struck gold — once you know the pattern you can code the characteristic ( or, in programming terms, logic) and since it is always consistent for a pattern, you need not tell the program how to do it each time. Rather just say till when to do it.

Loops to the rescue!!

You just specify the logic once and tell the program till when to keep repeating the logic

int stoppingNum = 20; 
//Start from the first odd number
int i = 1;
/*Since we want odd numbers only till 20 tell the loop to go on repeating the logic till i is less than 20*/while(i<20){
System.out.println(i);
i=i+2;
}

Thus your code becomes concise and easy to write.

In order to code for a pattern, you need to develop a mindset for how to approach any pattern problem. The problem may change but the way you approach it remains more or less the same.

Consider for example the following pattern:

First figure out how many rows are to be printed.

So you need one loop for rows

int rows = 1;
for(rows = 1; rows<=5 ; rows++){
//logic for printing stars in each column for current row
}

Next figure out what has to be done on each row

So one loop is required for specifying number of columns to be printed in each row

int rows = 1;
for(rows = 1; rows<=5 ; rows++){
/*On each row number of stars to be printed is equal to the row
number*/
for(int columns = 1; columns<=rows ; columns++){
System.out.print("* ");
}
/*At the end of the row move to the next line for next row*/
System.out.println("");
}

Check out the following video of a live lecture on patterns to see how Bloombench simplifies learning!!

As we saw in the previous sections, identifying patterns in a problem helps us code it better and in a much more concise manner.

But why is it important to know how to work with patterns? What are its applications?

Consider the following scenario. I have been entrusted with the task of organizing a Republic Day parade for India. I have a certain number of people dressed in either of Orange, White and Green colors. I need to form an arrangement of the people to represent the tricolor flag.

There are two important constraints:

  1. Arrangement should be a perfect rectangle.
  2. All the three colors must be equally represented.

Example:

An arrangement of 42 people would look as follows

Write a program which ,given the number of people available, outputs the largest possible rectangle given the constraints are met.

Sample output:

Enter the number of people available

50

Best possible arrangement would be with 48 people

O O O O O O O O
O O O O O O O O
W W W W W W W W
W W W W W W W W
G G G G G G G G
G G G G G G G G

Get coding!! Post your code in the comments below…

--

--