Saturday, 31 October 2015

Occurence of a number in Sorted Array/Vector using Recursion

 #include<iostream>  
 #include<vector>  
 using namespace std;  
 int Frequency(std::vector<int> &v, int k, int start, int end)  
 {  
   if (start > end)  
     return 0;  
   int mid = (start + end) / 2;  
   if (v[mid] < k)  
     return Frequency(v, k, mid + 1, end);  
   if (v[mid] > k)  
     return Frequency(v, k, start, mid - 1);  
   return Frequency(v, k, start, mid - 1) + 1 + Frequency(v, k, mid + 1, end);  
 }  
 int main()  
 {  
   int arr[] ={2,2,3,3,5,5,5};//Sorted Array  
   std::vector<int> v(arr, arr+7);  
   int k;  
   std::cin>>k;  
   std::cout<<Frequency(v, k, 0, v.size()-1);  
   getchar();  
 }  

Sunday, 18 October 2015

grep - Linux Command

Most of the general linux commands are available online but here i will be covering bit advance feature of some linux commands

grep

We use grep command to search contents in a file or directory.What will be command for searching only in specific extension files in a directory.Lets find out below
  • Recursively Search a pattern in all files of a directory use
        grep -R "Pattern to Search" /DirectoryPath
  • To Search a pattern only in files with specific extensions in a directory use
        grep -R --include=*.xml "Pattern to Search" /DirectoryPath
  • To Search a pattern in files with multiple extensions in a directory use
        grep -R --include=*.{xml,sh,html} "Pattern to Search" /DirectoryPath

  • Case insensitive Search of a pattern
        grep -iR  "Pattern to Search" /DirectoryPath