This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
//Space and time complexity O(n) | |
public abstract class AssociativeOperation { | |
protected int intVar; | |
protected AssociativeOperation(int intVar) { | |
this.intVar = intVar; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |