Tuesday, June 3, 2014

What I keep in my .bashrc or .profile

As of now:


alias c="cd"
alias d="cd ../"
alias g="grep"
alias l="ls"
alias p="pwd"
alias v="gvim"
alias vv="vim"
alias br="vim ~/.bashrc"
alias cs="cscope -d"
alias ll="ls -l"
alias sc="source ~/.bashrc"
alias sv="source ~/.vimrc"
alias vr="vim ~/.vimrc"


export PS1='\[\033[32m\]\h:\[\033[35m\]\w\[\033[32m\]$\[\033[35m\]'
#COMMENT: color format command [\033[32m user \]\h:\[\033[35m\]\w(workspace directory)\[\033[32m\]$\[\033[35m what we type in\]

.bashrc on MAC OS X (Snow Leopard, Mountain Lion, Mavericks, Yosemite)

Mac OS X does not have .bashrc file, instead there is .profile
To create this(if it already does not exist), create a profile in ~/.profile and treat it like a normal bashrc 
Note that ~ means /Users/MyUserName

Friday, March 28, 2014

Algo question: write "bool isSplitable(int[] arr)" to return true if the arr can be divided into two parts having the equal sums


First lets prove that an array can be split at only one point:

E.g
Array = [……….17……….19]
Lets say its split able at 19. To be also split able at 17, the sum of numbers b/w 19 and 17 would have to be -ve(-2 in this case). Which means sum total going from 17 to 19 would be < 17 (=15 in this case). So array would not be split able at 19.

Same can be proven if there is a num > 19 say 23





Now, for quickest run time, we’ll use an extra array to hold the sums.(S)
Algo would be:
Going forward, add and store the sum till that idx i into S[i].
Once the end is reached, walk backward, adding the sum of terms from back
and compare with the sum from front (S[i]
If match found, return true, else return false

Run time would be O(n)



Code(C++):

#include <iostream>
#include <map>
#include <tr1/unordered_map>
#include <tr1/unordered_set>

using namespace std;
using namespace tr1;


bool isSplittable(int *A, int size){

if(size < 2){return false;}

//array to hold sums
int *S = new int[size];
S[0] = A[0];
int i = 1;
int bkwd_sum = A[size-1]; //to hold sum while walking backwards
int fwd_sum = 0;
//put forward sum into S from 0 to len - 1
//cout << "S[0]" << S[0] << endl;
while(i<size-1){
//cout << "i: " << i << endl;
//cout << "A[i] " << A[i] << endl;

S[i] = S[i-1] + A[i];
//cout << "S[i] " << S[i] << endl;

i++;
}
i--;
//cout << "==============================" << endl;

//Start walking backwards till a match in sum is found
while(i>0){
fwd_sum = S[i];
//cout << "i: "  << i << endl;
//cout << "bkwd_sum: " << bkwd_sum << endl;
//cout << "fwd_sum "<< fwd_sum << endl;

if(fwd_sum == bkwd_sum){
cout << "array can be split at idx:" << i << endl;
return true;
}
bkwd_sum = bkwd_sum + A[i];
i--;
}

return false;
}

int main (int argc, char * const argv[]) {
int a[7] = {5, 7, 10, 10, -30, 17, 19};
int size = sizeof(a)/sizeof(int);

isSplittable(a, size);



return 0;
}

Wednesday, March 26, 2014

Unix/Linux: Device or resource busy, cannot delete

If you ever get a message like ..../dc3/stats_asdf/inc/.nfs00000000022b4f4d0009aa': Device or resource busy
when you try to delete a folder using rm -fr ..


This most probably means that there is a process running on your machine that is currently using this directory.


Solution: Kill the process. Run ps -ef | grep "your username etc." Identify the culprit process and kill it using kill (-9) PID

Using -Werror option

Werror takes warnings as errors and therefore prevents compilation even if there is a warning.

Wednesday, March 19, 2014

Find all permutations of string

Given string s, find all permutations of it. Don't worry about repeating.
E.g.: "hat" should give o/p:
“tha”, “aht”, “tah”, “ath”, “hta”, and “hat"

E.g.  "aaa" should give o/p aaa 6 times

-Note that repetition can be simply avoided by using a hashmap to quickly check if that permutation has been counted or not

C++ prog:

#include <iostream>
#include <map>
#include <vector>
#include <tr1/unordered_map>
#include <tr1/unordered_set>

using namespace std;
using namespace tr1;

void printVec(vector<string> v){
int i = 0;
cout << "start printing vec: "<< endl;
while(i < v.size()){
//cout << "i: " << i << endl;
cout  << v[i] << endl;
i++;
}
cout << "stop printing vec, i: " << i << endl;
return;
}


vector<string> perms(string s){
//cout << "input string: " << s << endl;
string temp;
vector<string> v (0);
if(s.length() <= 0){
//cout << "input string blank: " << s << endl;
temp = "";
v.push_back(temp);
//cout << "return vector: " << endl;
//printVec(v);
return v;
}
char c;
int i = 0; int j = 0;
vector<string> ret_vec (0);
string temp1;

c = s[0];
temp = s.substr(1, (s.length()-1));
ret_vec = perms(temp);

while(i < ret_vec.size()){
temp = ret_vec[i];
//cout << "======================================="<< endl;
//cout << "i: " << i << "  temp: " << temp << endl;
j = 0;

while(j <= temp.size()){
temp1 = temp;
temp1.insert(j, 1, c);
//cout << "j: " << i << "  temp1: " << temp1 << endl;
v.push_back(temp1);
j++;
}
i++;
}
//cout << "final return vector: " << endl;
//printVec(v);
return v;
}


int main (int argc, char * const argv[]) {
string s = "aaa";
vector<string> v (0);
v = perms(s);
printVec(v);
    return 0;
}

Tuesday, March 18, 2014

Algorithm question: Write a function to reverse the order of the words in a string

E.g.:  empty string here    o/p: here string, empty

C++ Prog:

#include <iostream>
#include <map>
#include <tr1/unordered_map>
#include <tr1/unordered_set>

using namespace std;
using namespace tr1;


string reverseWordBtwIdx(string s, int i, int j){
if (i >= j) {return s;}
if (s.length() <= 1){return s;}
char c;

while(i < j){
c = s[i];
s[i] = s[j];
s[j] = c;
i++; j--;
}
return s;
}


void reverseWords(string s){
if (s.size() == 0){
cout << "empty string" << endl;
return;
}
int i = 0; //start idx
int j = s.length() - 1; //end idx
int itr = 0;

cout << "original string: " << s << endl;
s = reverseWordBtwIdx(s, 0, s.length()-1);

i = 0;
while(itr < s.length()){
i = 0; j = 0;

if(s[itr] != ' ')
{
i = itr; j = itr+1;
while(j < s.length()){
if(s[j] == ' '){
break;
}
j++;
}
if (j >= s.length()){
j = s.length();
}
s = reverseWordBtwIdx(s, i, j-1);
itr = j+1;
}
else{itr++;}
}
cout << "final string: " << s << endl;
return;
}


int main (int argc, char * const argv[]) {
string s = "empty string, here";

reverseWords(s);


    return 0;
}