To Find and Replace in Terminal use this
for first occurrence:
sed -i 's/FIND/REPLACE/' <file_name>
for global occurrence:
sed -i 's/FIND/REPLACE/g' <file_name>
Wednesday, July 8, 2020
Monday, January 14, 2019
In Java Create Static Instance of a class
public class MyClass {
private static MyClass instance = null;
public synchronized static MyClass getInstance() {
if (instance == null) {
instance = new MyClass();
}
return instance;
}
public MyClass() {
super();
}
.
.
.
}
from external classes, this class can be accessed through
MyClass.getInstance()
private static MyClass instance = null;
public synchronized static MyClass getInstance() {
if (instance == null) {
instance = new MyClass();
}
return instance;
}
public MyClass() {
super();
}
.
.
.
}
from external classes, this class can be accessed through
MyClass.getInstance()
Thursday, May 10, 2018
Find and Replace in vi Editor
How do we find and replace a string in vi Editor?
Open the file in vi and use this:
In such case, you have to add that string pattern inside \( and \) in the find pattern and use \1 in replace string in the position where you want to replace it.
For Example:
Let's say you wan't to take all abc_NAME.test (NAME can be any name) and put it differently as lmn_NAME.test.
Open the file in vi and use this:
:%s/FIND_PATTERN/REPLACE_STRING/This will replace only the first occurrence of that string in each line. If you want to replace all occurrence, use the below:
:%s/FIND_PATTERN/REPLACE_STRING/gHow to take one string pattern from find pattern and use it in replace string?
In such case, you have to add that string pattern inside \( and \) in the find pattern and use \1 in replace string in the position where you want to replace it.
For Example:
Let's say you wan't to take all abc_NAME.test (NAME can be any name) and put it differently as lmn_NAME.test.
:%s/abc_\(.*.test\).*/lmn_\1/You can also do in multiple times. Only the order in which you open \( matters. It can even be nested. In replace you can use \1 \2 ]3 ... etc
Replace String in Multiple Files Linux Bash
We can copy all file paths into a file. Let's call the file name as `list`.
There are multiple ways of creating `list`. Here are some.
Below is the bash script which you can put in an executable file and run.
There are multiple ways of creating `list`. Here are some.
ls > listIn file_path you can mention something like *.java for example to search all java files alone.
grep -l "FIND_STRING" file_path > list
Below is the bash script which you can put in an executable file and run.
#!/usr/bin/ksh
for var in `cat list`
do
sed -i "s/FIND_STRING/REPLACE_STRING/g" $var
done
Friday, April 22, 2016
Level Order Traversal in a Binary Tree from Bottom to Top
public void LevelOrderReverse() {
System.out.println(LevelOrderReverse(this));
}
private String LevelOrderReverse(BinarySearchTree T) {
StringBuffer sb = new StringBuffer();
if(T!=null) {
Queue<BinarySearchTree> Q = new LinkedList<BinarySearchTree>();
Stack<BinarySearchTree> S = new Stack<BinarySearchTree>();
Q.add(T);
while(Q.size() > 0) {
BinarySearchTree C = Q.remove();
if (C.right != null)
Q.add(C.right);
if (C.left != null)
Q.add(C.left);
S.push(C);
}
while(S.size() > 0)
sb.append(S.pop().data).append(", ");
}
return sb.toString();
}
System.out.println(LevelOrderReverse(this));
}
private String LevelOrderReverse(BinarySearchTree T) {
StringBuffer sb = new StringBuffer();
if(T!=null) {
Queue<BinarySearchTree> Q = new LinkedList<BinarySearchTree>();
Stack<BinarySearchTree> S = new Stack<BinarySearchTree>();
Q.add(T);
while(Q.size() > 0) {
BinarySearchTree C = Q.remove();
if (C.right != null)
Q.add(C.right);
if (C.left != null)
Q.add(C.left);
S.push(C);
}
while(S.size() > 0)
sb.append(S.pop().data).append(", ");
}
return sb.toString();
}
Wednesday, April 20, 2016
Amazon Question: +1 or -1 Array Searching
Given an array, next element is either +1 or -1 of previous element then find any number k ?
public int getIndex(int element, int[] input) { //element = k
int size = input.length;
for(int i=0; i < size;) {
if(input[i]==element)
return i;
i=i+getPositive(element - input[i]);
}
return -9999; // element not found
}
private int getPositive(int i) {
if (i>0) return i;
return (i * -1);
}
Example 1:
Input: 4 5 4 5 6 7 8 9 8 9 10 11
k = 8
Output = 6
Example 2:
Input: 11 10 9 8 7 6 5 4 5 6 7 8 9 10 11
k = 4
Output = 7
public int getIndex(int element, int[] input) { //element = k
int size = input.length;
for(int i=0; i < size;)
return i;
i=i+getPositive(element - input[i]);
}
return -9999; // element not found
}
private int getPositive(int i) {
if (i>0) return i;
return (i * -1);
}
Example 1:
Input: 4 5 4 5 6 7 8 9 8 9 10 11
k = 8
Output = 6
Example 2:
Input: 11 10 9 8 7 6 5 4 5 6 7 8 9 10 11
k = 4
Output = 7
Bucket Sort
Amazon Question: Given an array with 3 distinct elements, sort the elements in O(n) complexity
Input: 1,3,1,2,3,1,2,2,3
Output: 1, 1, 1, 2, 2, 2, 3, 3, 3
import java.util.ArrayList;
public class BucketSort {
ArrayList> bucket;
private int count;
public BucketSort(int unique_int_count) {
this.count = unique_int_count;
this.bucket = new ArrayList>();
for(int i=0; i < this.count; i++) {
bucket.add(new ArrayList());
}
public ArrayList Sort(ArrayList input) {
for(int c:input) {
if(c==1) {
bucket.get(0).add(c);
} else if(c==2) {
bucket.get(1).add(c);
} else {
bucket.get(2).add(c);
}
}
ArrayList sorted_output = new ArrayList<>();
for(int i=0; i < count; i++) {
sorted_output.addAll(bucket.get(i));
return sorted_output;
}
@Override
protected void finalize() throws Throwable {
bucket=null;
super.finalize();
}
}
//----------------------Main Method------------------------
public static void main(String[] args) {
BucketSort b = new BucketSort(3);
ArrayList input = new ArrayList();
input.add(1);
input.add(3);
input.add(1);
input.add(2);
input.add(3);
input.add(1);
input.add(2);
input.add(2);
input.add(3);
System.out.println(b.Sort(input));
}
Input: 1,3,1,2,3,1,2,2,3
Output: 1, 1, 1, 2, 2, 2, 3, 3, 3
import java.util.ArrayList;
public class BucketSort {
ArrayList
private int count;
public BucketSort(int unique_int_count) {
this.count = unique_int_count;
this.bucket = new ArrayList
for(int i=0; i
}
public ArrayList
for(int c:input) {
if(c==1) {
bucket.get(0).add(c);
} else if(c==2) {
bucket.get(1).add(c);
} else {
bucket.get(2).add(c);
}
}
ArrayList
for(int i=0; i
return sorted_output;
}
@Override
protected void finalize() throws Throwable {
bucket=null;
super.finalize();
}
}
//----------------------Main Method------------------------
public static void main(String[] args) {
BucketSort b = new BucketSort(3);
ArrayList
input.add(1);
input.add(3);
input.add(1);
input.add(2);
input.add(3);
input.add(1);
input.add(2);
input.add(2);
input.add(3);
System.out.println(b.Sort(input));
}
Labels:
Algorithm,
Amazon,
Bucket Sort,
Java,
Jeevan,
Jeevan Rex,
Jeevanus,
Sorting
Amazon Question: Print Last N nodes of Linked List in reverse
Given a singly linked list's head and an integer N, print last N nodes of the list in reverse order.
Example:
List = 1->2->3->4->5->6->7->8
N = 3
Output = 8, 7, 6,
public void printRev(int count) { // count = N
temp = count;
// this here is the head node
System.out.println(printRev(this, new StringBuffer()));
}
private int temp;
private String printRev(LinkedList L, StringBuffer sb) {
if(L.next!=null)
printRev(L.next, sb);
if(temp>0) {
sb.append(L.data).append(", ");
temp--;
}
return sb.toString();
}
Example:
List = 1->2->3->4->5->6->7->8
N = 3
Output = 8, 7, 6,
public void printRev(int count) { // count = N
temp = count;
// this here is the head node
System.out.println(printRev(this, new StringBuffer()));
}
private int temp;
private String printRev(LinkedList L, StringBuffer sb) {
if(L.next!=null)
printRev(L.next, sb);
if(temp>0) {
sb.append(L.data).append(", ");
temp--;
}
return sb.toString();
}
Monday, April 11, 2016
Tamil Kumari FM Talk about Birds
This is a talk about Birds which I gave 3 years ago in Kumari FM about Origin of Birds, Living Birds, Conservation of Birds.
Labels:
Birds,
FM,
India,
Jeevan,
Jeevan Rex,
Jeevanus,
Kanyakumari,
Kumari,
Tamil
Sunday, March 6, 2016
Find if a Tree is a Binary Search Tree
public boolean isBinarySearchTree() {
return isBinarySearchTree(this, -99999, 99999);
}
public boolean isBinarySearchTree(BinarySearchTree T, int min, int max) {
if(T==null)return true;
if(T.data>min && T.data<max
&& isBinarySearchTree(T.left, min, T.data) && isBinarySearchTree(T.right, T.data, max))
return true;
return false;
}
return isBinarySearchTree(this, -99999, 99999);
}
public boolean isBinarySearchTree(BinarySearchTree T, int min, int max) {
if(T==null)return true;
if(T.data>min && T.data<max
&& isBinarySearchTree(T.left, min, T.data) && isBinarySearchTree(T.right, T.data, max))
return true;
return false;
}
Subscribe to:
Posts (Atom)