Thursday, May 18, 2017

Bridge Design Pattern

The Bridge pattern is designed to separate a class's interface from its implementation so you can vary or replace the implementation without changing the client code (see the Bridge pattern Wikipedia entry).

Lets see what that means in a simpler way.

In following example polymorphism is used inorder to give Person Class different implementations.

---
There are lot of people by same name. Say we want to introduce a new property home town.
If we follow this pattern code would be as follows.

---
That is code is hard to maintain and reusability is low. We can use Bridge design pattern to improve that code by seperating implementation of home town.
---
---

Tuesday, December 20, 2016

Corners of a square matrix remain corners no matter how many times you invert them

This is a hint That can be used in many programming contests which I noticed during solving the Flipping the matrix problem in Hackerrank.com and thought that it is something that I should write down somewhere. :)
Here is the problem description from Hackerrank.
Sean invented a game involving a  matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times, and the goal of the game is to maximize the sum of the elements in the  submatrix located in the upper-left corner of the  matrix (i.e., its upper-left quadrant).
Given the initial configurations for  matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal. For each matrix, print the maximized sum on a new line.
Input Format
The first line contains an integer, , denoting the number of queries. The subsequent lines describe each of the  queries in the following format:
  1. The first line of each query contains an integer, .
  2. Each line  the  subsequent lines contains  space-separated integers describing the respective values of row  in the matrix.
So first I tried varios thing to reverse the rows and columns in various ways. But later realizd that there should be a simple hint that solve this poblem and there was a one. That is
Corners of a square matrix remain corners no matter how many times you invert them
Using that hint to find the max,

In a 4X4 matrix


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16














Sunday, December 11, 2016

3 different ways to loop through every char in a string in c++

When programming sometimes we need to loop through a string and deal with every character in that string. In c++ there are several ways to do that. In this post lets see 3 different ways to loop through a string.
  1. Using a range-based for loop (This feature is available from c++ 11)


  2. Using ierators


  3. Using normal for loop



If you have a null terminated character array this method also can be used.

Those are the 4 different ways that can be used to loop through strings. If you have suggestions or new methods pleas leave a comment below.

Wednesday, December 7, 2016

Find running time of an algorithm in C++

Some times we need to find how long would an algorithm take to complete a task. This is needed very much when we comparing two or more algorithms. So I looked for that in several forums and most of them have suggested to use clock() function which comes with ctime header. That didn't work properly because It gives the time from the start of the program (which I will explain latter part of the post) and not suitable for measuring intervals in nanosecond level.

So here is the solution for that. We can use functions in class high_resolution_clock. Here IsPrime is the function that I want to find the running time of.
As you can see in line 30, we can cast the time difference int any unit we want (ns,ms). That gives us a great advantage of finding very small time intervals. But if we use clock() function instead we wan't be able to achieve that much of accuracy.

Here is a code example of using clock() function (which I don't recommend in this scenario)


clock() function will give the total number of clock ticks until the execution of function from the start of the program (If we want to convert it into seconds, have to divide by CLOCKS_PER_SEC).
But this gives a good result only if we are doing a considerable calculation between the time interval. other wise we will get a zero as result.

Sunday, November 13, 2016

Enabling C++11 in Eclipse Kepler/Luna CDT

When you are coding in C++ using eclipse IDE (installing CDT plugin)and if you use newer features of C++11 sometimes you might get errors when compiling. Reason for that might be you haven't enabled C++11 in Eclipse CDT. Lets see how to enable C++11 in Eclipse CDT. These steps are given assuming you are using GCC compiler. But steps are more similar to the other compilers as well.

Step 1 : Set up your compiler

  • Right click your project and click Properties
  • Under C/C++ Build click Settings
  • Under GCC C++ Compiler, click Miscellaneous
  • In the Other Flags box, append "-std=c++11" to the list of tokens
  • Click Apply and OK

Wednesday, November 9, 2016

Comment a code block in gvim


When we are coding we have to comment out some blocks of code and uncomment those time to time. It is easy with IDE like eclipse, netbeens, Visual Studio. I recently moved to vim and here is how to comment and uncomment in gvim. (lets assume character "#" is used for line commenting)

Method 1 

Command mode of vim used for this.
Use this command to comment lines from 10 to 15 with "#"
:10,15s/^/#
Use this command to uncomment lines commented before.
:10,15s/^#/

Method 2

VISUAL BLOCK mode is used for this.
  1. First, move the cursor to the first char of the first line in block code you want to comment. Then press Ctrl + v to enter VISUAL BLOCK mode
  2. Then move cursor with arrow keys to the end of code block that want to comment.
  3.  Then press Shift + i and cursor will come to the begining of code block
  4. The type the commenting character. In this case "#".
  5. Final press Esc and it will comment out the block you marked
To uncomment,

do the same things but instead of type Shift + i, you just type x to remove all # after highlight them in VISUAL BLOCK mode.

Monday, November 7, 2016

Select and replace strings in gvim


Here are some ways to replace strings in gvim.

For replacing a string across a file

:%s/<search_string>/<replace_string>/g

To replace in current line

:s/<search_string>/>replace_string>/

To select and replace words from selective lines in vim(In this example want to replace in line 6 to 10 and 14 to 18).

:6,10s/<search_string>/<replace_string>/g | 14,18&&
The :&& command repeats the last substitution with the same flags. You can supply the additional range(s) to it (and concatenate as many as you like):

If you have many ranges you can use loops as well.

:for range in split('6,10 14,18')| exe range 's/<search_string>/<replace_string>/g' | endfor
This four options can be used in almost any time that we want select and replace in gvim

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...