Labels

Saturday, December 2, 2017

Short tutorial on frequently used perforce commands

Frequently used perforce commands :

1) p4 opened ... : Lists all opened files for edit.

2) p4 diff ... : Lists all the diffs.

3) p4 diff -se .. : Lists all the files where changes have been made without doing "p4 edit".

4)  p4 diff -sd .. : Lists all the files deleted without doing "p4 delete".

5) p4 diff dir/... : Lists all the diffs in directory dir.

6)  p4 opened dir/... : Lists all opened files for edit in directory dir.

7) p4 delete :  Delete a file.

8) p4 submit ... : Submit opened changes.

9) p4 shelve : Shelve open changes.

10) p4 describe 123 : Describe changes made with change list 123.

11) p4 unshelve -s 123 : Unshelve changes in shelved change list 123.

12) p4 revert src/test.cpp : Revert changes in file "src/test.cpp.

13) p4 revert ... : Revert all the changes.

14) p4 sync ... : Sync to latest change list.

Why STP is better than SIP?

I am writing this article to explain to you why STP which stands for "systematic transfer plan" is better than SIP (systematic investment plan) for equity mutual fund investment when you have a lumpsum amount in your bank account.

Suppose you have 1 lakh rupees in your account and you want to invest it in equity.
We have understand that investment in equity is subject to market risk and it is having high volatility but at the same time history has proven equity as the best form of investment option for human kind.

Understanding everything we know how SIP can save us from market fluctuation and it is the best means for a layman to invest in equity but what should you do if you have lots of funds in you account from a bonus or sale of property? Should you keep that amount in bank and slowly invest through SIP?

The answer is NO. Bank these days are giving only 3.5% return keeping huge amount in savings banks account will not be the wisest investment approach. Put the lumpsum amount in liquid fund.
These are the funds which invests in government securities with very short expires as a result they never go down and gives a approximate return of ~6%. Then start one or multiple STPs from liquid fund to equity fund.  Simple!!

List of liquid funds to invest : http://www.moneycontrol.com/mutual-funds/performance-tracker/returns/liquid.html




Tez app by Google

I recently used Tez app by google for UPI. UPI is Unified Payments Interface (UPI) it is a system that powers multiple bank accounts into a single mobile application. 

There are mayUPI app currently on play store :

1) BHIM : https://play.google.com/store/apps/details?id=in.org.npci.upiapp
2) Paytm : https://play.google.com/store/apps/details?id=net.one97.paytm (Paytm integrated bhim to its app)
3) All the bank upi apps.
4) TEZ : UPI app by google https://g.co/tez/Sk34J

Good thing about TEZ is that they are offering huge cashbacks. So install the app using the above link and get Rs 51. Plus after every tranfer (> 500 Rs) you get a scratch card which can give you any amount between 0 to 100000.

So install the app and enjoy the cashbacks.
 

Power of compounding

Got this message on Whatsapp:

Compounding the 8th wonder😊

10000 invested in EICHER MOTORS in 1992 - today 80 lacs

10000 invested in ASIAN PAINTS in 1986- today 90 lacs

10000 invested in HDFC in 1990 - today 1 cr

10000 invested in BRITANNIA in 1985 - today 1.06 cr

10000 invested in MRF in 1985 - today 1.10cr

10000 invested in BOSCH in 1985 - today 1.20 cr

10000 invested in AMARA RAJA in 1991 - today 1.25 cr

10000 invested in SHREE CEMENT in 1990 - today 2.90 cr

10000 invested in INFY in 1993 - today 3 cr

10000 invested in DR. REDDY in 1986 - today 10 cr

10000 invested in WIPRO in 1981 - today 400 cr

Dividend income Extra

Wednesday, June 7, 2017

Wordament Boggle solver

Here is the code which will work on all unix systems and give all the words that can be formed in wordament game.

Few highlights for the code :

1) Code can be easily modified to give only big words for more points.
2) Also longer words are printed first. This feature can also be easily changed.
3) To compile the code just use g++ compiler. (g++ test.cpp).
4) and run it using ./a.out
5) input the wordament game in row first format.
6) This code was written using code from g4g site.
7) I combined the code given in "g4g boggle solver" and "g4g tries" and combined them to solve wordament.

Code :
 
#include<iostream>
#include<cstring>
#include <vector>
#include <fstream>
#include <algorithm>
#include <pthread.h>
using namespace std;

#define M 4
#define N 4
#define MAX 15
#define MIN 7

struct TrieNode *dictionary;//  = getNode();

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

// Alphabet size (# of symbols)
#define ALPHABET_SIZE (26)

// Converts key current character into index
// use only 'a' through 'z' and lower case
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')

string boggle[M][N];// = {{'G','I','Z'},
// trie node
vector<string> ans;

struct TrieNode
{
struct TrieNode *children[ALPHABET_SIZE];

// isLeaf is true if the node represents
// end of a word
bool isLeaf;
};

// Returns new trie node (initialized to NULLs)
struct TrieNode *getNode(void)
{
struct TrieNode *pNode = NULL;

pNode = (struct TrieNode *)malloc(sizeof(struct TrieNode));

if (pNode)
{
int i;

pNode->isLeaf = false;

for (i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
}

return pNode;
}

// If not present, inserts key into trie
// If the key is prefix of trie node, just marks leaf node
void insert(struct TrieNode *root, string key)
{
int level;
int length = key.size();
int index;

struct TrieNode *pCrawl = root;

for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();

pCrawl = pCrawl->children[index];
}

// mark last node as leaf
pCrawl->isLeaf = true;
}

// Returns true if key presents in trie, else false
bool search(struct TrieNode *root, string key)
{
int level;
int length = key.size();
int index;
struct TrieNode *pCrawl = root;

for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);

if (!pCrawl->children[index])
return false;

pCrawl = pCrawl->children[index];
}

return (pCrawl != NULL && pCrawl->isLeaf);
}




// A given function to check if a given string is present in
// dictionary. The implementation is naive for simplicity. As
// per the question dictionary is givem to us.
bool isWord(string &str)
{
if (search(dictionary, str) != 0)
return true;
return false;
}

// A recursive function to print all words present on boggle
void findWordsUtil(bool visited[M][N], int i,
int j, string &str, int size)
{
// Mark current cell as visited and append current character
// to str
//int size;
visited[i][j] = true;
str = str + boggle[i][j];
size = size+boggle[i][j].size();

// If str is present in dictionary, then print it
if (size<= MAX  && size>=MIN && isWord(str))
ans.push_back(str);//cout << str << endl;

// Traverse 8 adjacent cells of boggle[i][j]
for (int row=i-1; row<=i+1 && row<M; row++)
for (int col=j-1; col<=j+1 && col<N; col++)
if (row>=0 && col>=0 && !visited[row][col])
findWordsUtil(visited, row, col, str, size);

// Erase current character from string and mark visited
// of current cell as false
str.erase(str.length()-boggle[i][j].size());
size = size - boggle[i][j].size();
visited[i][j] = false;
}

// Prints all words present in dictionary.
void findWords() {
bool visited[M][N] = {{false}};
string str = "";
for (int i=0; i<M; i++)
      for (int j=0; j<N; j++)
findWordsUtil(visited, i, j, str, 0);

}

int all_chars(string s) {
int t = s.size();
for (int i  = 0; i<t; i++) {
if (!(s[i] >= 'a' && s[i] <= 'z'))
return 0;
}
return 1;
}

void load_dictionary() {
string line;
int n = 0;
ifstream myfile("/usr/share/dict/words");
dictionary = getNode();
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
//std::transform(line.begin(), line.end(), line.begin(), ::tolower);
//dictionary.push_back(line);
if (all_chars(line) && line.size()<=MAX && line.size()>=MIN) {
insert(dictionary, line);
n++;
}
}
myfile.close();
}
cout <<"words added = "<<n<<endl;
}


void enter_boog(string boggle[M][N]) {
int i,j;
for (i= 0; i<M; i++) {
for (j = 0; j<N; j++) {
cin>>boggle[i][j];
}
}
}

bool compare(string s1, string s2) {
int i = s1.size();
int j = s2.size();
if ( i != j)
return i > j;
else
return (s1[0] - s2[0] > 0);
}
void display_words() {
sort(ans.begin(), ans.end(), compare);
for (int i=0; i<ans.size(); i++) {
cout<<ans[i]<<endl;
}
}
// Driver program to test above function
int main()
{
load_dictionary();
while (1) {
cout<<"enter boggle matrix";
enter_boog(boggle);
for (int i= 0; i<M; i++) {
for (int j = 0; j<N; j++) {
cout<<boggle[i][j]<<" ";
}
cout<<endl;
}

cout << "Following words of dictionary are present\n";

findWords();
display_words();
ans.resize(0);
}
return 0;
}


Sunday, April 16, 2017

Minimum Path Sum in C++ using tabulation (Dynamic Programming)

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int row = grid.size();
        int col = grid[0].size();
        vector<vector<int>> v(row, vector<int>(col,0));
        v[0][0] = grid[0][0];
        for (int i = 1; i< row; i++) 
            v[i][0] = grid[i][0] + v[i-1][0];
        for (int i = 1; i< col; i++) 
            v[0][i] = grid[0][i] + v[0][i-1];
        for (int i = 1; i<row; i++) {
            for (int j = 1; j < col; j++) {
                v[i][j] = grid[i][j] + min (v[i-1][j], v[i][j-1]);
            }
        }
        return v[row-1][col-1];    
    }
};

Coin Change problem in C++ using tabulation method dynamic programming

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)
Example 2:
coins = [2], amount = 3
return -1.
Note:
You may assume that you have an infinite number of each kind of coin.

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int n = coins.size();
        vector<vector<int>> v(n+1, vector<int>(amount+1, 0));
        for (int i = 0; i<=n; i++)
            v[i][0] = 0;
        for (int i = 0; i<= amount; i++)
            v[0][i] = INT_MAX;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= amount; j++) {
                if (j>=coins[i-1] && v[i][j-coins[i-1]] < INT_MAX)
                    v[i][j] = min(v[i-1][j], v[i][j-coins[i-1]]+1);
                else
                    v[i][j] = v[i-1][j];
            }
        }
        return (v[n][amount]<INT_MAX)?v[n][amount]:-1;
    }
};