Linux Command Line - Process Management

Definition

  • Instance of a program An instance of a program is a copy of an executable version of the program that has been written to the computer’s memory.

  • Process When we run a program, those instructions are copied into memory and space is allocated for variables and other stuff required to manage its execution. This running instance of a program is called a process and it’s processes which we manage.

Top

Click to read more ...

Linux Command Line - Regular Expressions

Egrep

egrep is a program which will search a given set of data and print every line which contains a given pattern. It is an extension of a program called grep. It’s name is odd but based upon a command which did a similar function, in a text editor called ed. It has many command line options which modify it’s behaviour so it’s worth checking out it’s man page. ie the -v option tells grep to instead print every line which does not match the pattern.

hadley@hadley-MacBookPro:~/Developments/parentDirectory$ egrep 'mellon' data.txt 
Mark watermellons 12
Oliver rockmellons 2

-v to tells egrep to instread print every line which does not match the pattern

Click to read more ...

Linux Command Line - Piping and Redirection

Redirecting to a file

save current command output to a file instead of printed to the screen.

hadley@hadley-MacBookPro:~/Developments/parentDirectory$ ls -l > list
hadley@hadley-MacBookPro:~/Developments/parentDirectory$ cat list
total 16
-rw-rw-r-- 1 hadley hadley  230 Okt 21 22:08 data_test.txt
-rw-rw-r-- 1 hadley hadley  230 Okt 21 21:01 data.txt
-rwxrwxrwx 1 hadley hadley   92 Okt 20 21:28 file.txt
-rw-rw-r-- 1 hadley hadley    0 Okt 21 22:08 list
drwxrwxr-x 2 hadley hadley 4096 Okt 21 21:09 test1
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:42 test.cpp
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:01 ttt
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:42 x
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:43 x1
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:42 y
Click to read more ...

Linux Command Line II

Permissions

view permissions

hadley@hadley-MacBookPro:~/Developments/parentDirectory$ ls -l
total 8
-rw-rw-r-- 1 hadley hadley   92 Okt 20 21:28 file.txt
drwxrwxr-x 2 hadley hadley 4096 Okt 20 21:33 test1
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:42 test.cpp
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:01 ttt
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:42 x
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:43 x1
-rw-rw-r-- 1 hadley hadley    0 Okt 20 21:42 y
  • The first character identifies the file type. If it is a dash ( - ) then it is a normal file. If it is a d then it is a directory.
  • The following 3 characters represent the permissions for the owner. A letter represents the presence of a permission and a dash ( - ) represents the absence of a permission. In this example the owner has all permissions (read, write and execute).
  • The following 3 characters represent the permissions for the group. In this example the group has the ability to read but not write or execute. Note that the order of permissions is always read, then write then execute.
  • Finally the last 3 characters represent the permissions for others (or everyone else). In this example they have the execute permission and nothing else.
Click to read more ...

Two Pointers

Two Pointers Classical Problem

  • 360 sort transformed array(Q:A)

Sum

Description

Given a sorted array A, having N integers, find if there exists any pair of elements such that their sum is equal to target.

bool isPairSum(int array[], int N, int target){
    //two pointers
    int i=0,j=N-1;

    while(i<j){
        if(array[i]+array[j]==target){
            return true;
        }else if(array[i]+array[j]<target){
            i++;
        }else{
            j--;
        }
    }
    return false;
}
Click to read more ...

Backtracking and DFS

String Definition

Standard Backtracking

  • 294 Flip Game II(Q:A)
  • 320 Generalized Abbreviation(Q:A)

Permutations

Permutations for dictinct data variables

void permutation(vector<int> &num, int l, vector<vector<int> > &res) {
    if (l == num.size()-1) {
        res.push_back(num);
        return;
    }
        
    for (int i = l; i < num.size(); i++) {
        swap(num[i], num[l]);
        permutation(num, l+1, res);
        swap(num[i], num[l]);
    }
}
Click to read more ...