Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / Stream
Last active March 13, 2017 17:20
Custom Stream
import scala.annotation.tailrec
/**
* Created by i.dey on 3/12/17.
*/
trait Stream[+T] {
def isEmpty : Boolean
def head : T
def tail : Stream[T]
@deyindra
deyindra / LazyPrime
Created March 13, 2017 17:17
Lazy Prime Stream
object PrimeStream {
def apply(end : Int):Stream[Int] = {
def greater:(Int,Int) =>Boolean = (x,y) => x>=y
def transform: (Int) => Int = (x) => x+1
val intStream:Stream[Int] = Stream(2,end)(greater,transform)
def primeStream(stream:Stream[Int]) : Stream[Int] = {
new ConStream[Int](stream.head, primeStream(Stream(stream.tail)(_%stream.head!=0)))
}
primeStream(intStream)
}
@deyindra
deyindra / Associative Operation
Created March 27, 2017 16:22
Associative Operation
import java.util.Arrays;
//Space and time complexity O(n)
public abstract class AssociativeOperation {
protected int intVar;
protected AssociativeOperation(int intVar) {
this.intVar = intVar;
}
@deyindra
deyindra / Planidromic.java
Created April 13, 2017 21:45
Planidromic Number
/**
* Calculate how many palindromic n-digit numbers are present
* for 2 digit the palindromic numbers are 11, 22, 33, 44, 55, 66, 77, 88, 99 (total of 9)
**/
public static int numberOfPalindromicProblem(int n){
if((n & 1) != 0){
n = n/2;
}else{
n = n/2 -1;
}