Skip to content

Instantly share code, notes, and snippets.

@deyindra
deyindra / Generator
Created March 12, 2017 05:24
Custom Random Generator
package org.idey.scala.generator
import scala.util.Random
/**
* Generator Trait for any Random object generator if any types
* @tparam T type of any [[scala.Any]]
*/
trait Generator[+T] {
self =>
@deyindra
deyindra / PeekIterator
Created February 21, 2017 00:45
AssertJ
package org.idey.algo.iterator.peek;
import java.util.Iterator;
/**
* Special Iterator which will provide addiotnal method {@link PeekIterator#peek()}
* @param <T> return the current object
*
*/
public interface PeekIterator<T> extends Iterator<T> {
@deyindra
deyindra / AssertJ
Created February 21, 2017 00:21
Sliding Window Iterator
package org.idey.algo.util;
import java.util.function.Predicate;
/**
* Assertion Utility class which will Assert statement in case statement is false,
* it will throw IllegalArgument Exception.
*/
public class AssertJ {
@deyindra
deyindra / ArrayIndexOf
Created January 19, 2017 21:08
KMP for Sub Array Search
private static <T> int[] computeTemporaryArray(T pattern[]){
int [] lps = new int[pattern.length];
int index =0;
for(int i=1; i < pattern.length;){
if(equals(pattern[i],pattern[index])){
lps[i] = index + 1;
index++;
i++;
}else{
if(index != 0){
@deyindra
deyindra / SampleRateLimiter
Created January 18, 2017 15:30
RateLimiter
public class RateLimiter {
private long minTime;
//holds time of last action (past or future!)
private volatile long lastSchedAction;
private volatile boolean forFirstTime;
public RateLimiter(float maxRate, long duration, TimeUnit unit) {
if(maxRate <= 0.0f) {
throw new IllegalArgumentException("Invalid rate");
}
if(unit.compareTo(TimeUnit.MILLISECONDS)<0){
@deyindra
deyindra / FloodFill
Created January 16, 2017 17:44
Flood Fill Algorithm
private static <T> boolean isSafe(T[][] array, int rowIndex, int colIndex,Predicate<T> predicate){
final int ROW = array.length;
final int COL = array[0].length;
return rowIndex>=0 && rowIndex<ROW && colIndex>=0 && colIndex<COL
&& predicate.test(array[rowIndex][colIndex]);
}
public static <T> void floodFill(T[][] array, int rowIndex, int colIndex, T newObject, Predicate<T> predicate){
int rowNbr[] = new int[] {-1, -1, -1, 0, 0, 1, 1, 1};
@deyindra
deyindra / FindPivote
Created January 15, 2017 17:24
Find Pivote of Sorted Array (Rotated or Non Rotated)
//O(log(N)) complexity
public <T extends Comparable<T>> static in findPivote(T[] array){
int low = 0;
int hight = array.length - 1;
while(array[low].compareTo(array[high])>0){
int mid = low + (high -low)/2;
if(array[mid].compareTo(array[high])>0){
low = mid + 1;
}else{
high = mid;
@deyindra
deyindra / isPalinDrome
Last active January 14, 2017 18:36
Given a String return true if any permutation of the String is palindrome
public static boolean isPalinDrome(String str){
if(str==null || str.length()==0){
throw new IllegalArgumentException("Invalid words");
}else{
str = str.trim();
char[] array = str.toCharArray();
Set<Character> sets = new HashSet<>();
for(char ch:array){
boolean add = sets.add(ch);
if(!add){
@deyindra
deyindra / ClosableResourceIterator
Created October 20, 2016 21:26
FileContentIterartor
import java.io.Closeable;
import java.util.Iterator;
/**
* Iterator which used some resource needs to be closed after execution
* <em>Any concrete {@link ClosableResourceIterator} class either must be initialized with
* try with resource or should call {@link ClosableResourceIterator#close()} after complele
* execution.</em>
* @see Iterator
* @see Closeable
@deyindra
deyindra / NArrayTreeSerDe
Created September 13, 2016 14:40
Serialize and DeSerialize of N-Array Tree
//Class Represent NArrayTreeNode
public class NArrayTreeNode<E> {
private E data;
private List<NArrayTreeNode<E>> childs = new ArrayList<>();
public NArrayTreeNode(E data) {
this.data = data;
}
public NArrayTreeNode<E> addChild(NArrayTreeNode<E> child){