Merge branch 'Development' into Development

This commit is contained in:
Abhijay Kumar 2019-05-21 14:26:32 +05:30 committed by GitHub
commit 132060f078
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 644 additions and 524 deletions

View File

@ -3,6 +3,7 @@ package src.main.java.com.conversions;
public class AnyBaseToDecimal {
/**
* This method produces a decimal value of any given input number of any base
*
* @param inpNum String of which we need the decimal value and base in integer format
* @return string format of the decimal value
*/
@ -12,11 +13,11 @@ public class AnyBaseToDecimal {
int num = 0;
int pow = 1;
for (int i=len-1; i>=0; i--) {
for (int i = len - 1; i >= 0; i--) {
if (valOfChar(inpNum.charAt(i)) >= base) {
return "Invalid Number";
}
num += valOfChar(inpNum.charAt(i))*pow;
num += valOfChar(inpNum.charAt(i)) * pow;
pow *= base;
}
return String.valueOf(num);
@ -24,16 +25,16 @@ public class AnyBaseToDecimal {
/**
* This method produces integer value of the input character and returns it
*
* @param c Char of which we need the integer value of
* @return integer value of input char
*/
private static int valOfChar(char c) {
if (c >= '0' && c <= '9') {
return (int)c - '0';
}
else {
return (int)c - 'A' + 10;
return (int) c - '0';
} else {
return (int) c - 'A' + 10;
}
}
}

View File

@ -0,0 +1,26 @@
package src.main.java.com.conversions;
/**
* Convert the binary number into gray code
*/
public class BinaryToGray {
/**
* convert the binary number into gray code
*
* @param binaryCode binary number
* @return grayCode return as string
*/
public String binaryToGray(String binaryCode) {
StringBuilder grayCode = new StringBuilder(Character.toString(binaryCode.charAt(0)));
for (int i = 0; i < binaryCode.length() - 1; i++) {
if (binaryCode.charAt(i) == binaryCode.charAt(i + 1))
grayCode.append("0");
else
grayCode.append("1");
}
return grayCode.toString();
}
}

View File

@ -3,23 +3,24 @@ package src.main.java.com.conversions;
import java.util.ArrayList;
public class DecimalToAnyBase {
/**
* This method produces a String value of any given input decimal in any base
*
* @param inp Decimal of which we need the value in base in String format
* @param base base in which we want the decimal value to be converted into
* @return string format of the converted value in the given base
*/
public String convertToAnyBase(int inp, int base) {
ArrayList<Character> charArr = new ArrayList<>();
while (inp > 0) {
charArr.add(reVal(inp%base));
charArr.add(reVal(inp % base));
inp /= base;
}
StringBuilder str = new StringBuilder(charArr.size());
for(Character ch: charArr) {
for (Character ch : charArr) {
str.append(ch);
}
@ -28,14 +29,15 @@ public class DecimalToAnyBase {
/**
* This method produces character value of the input integer and returns it
*
* @param num integer of which we need the character value of
* @return character value of input integer
*/
private char reVal(int num) {
if (num >= 0 && num <= 9)
return (char)(num + '0');
return (char) (num + '0');
else
return (char)(num - 10 + 'A');
return (char) (num - 10 + 'A');
}
}

View File

@ -3,15 +3,16 @@ package src.main.java.com.conversions;
import java.math.BigInteger;
public class DecimalToHexadecimal {
private static final char hexChars[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
private static final char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static final BigInteger valueHex = new BigInteger("16");
/**
* This method converts and decimal number to a Hexadecimal number
*
* @param decimalStr
* @return hexadecimal number
*/
public String decimalToHex(String decimalStr){
public String decimalToHex(String decimalStr) {
BigInteger decimal = new BigInteger(decimalStr);
int rem;

View File

@ -3,20 +3,21 @@ package src.main.java.com.conversions;
import java.math.BigInteger;
public class DecimalToOctal {
private static final char octalChars[] = {'0','1','2','3','4','5','6','7'};
private static final char[] octalChars = {'0', '1', '2', '3', '4', '5', '6', '7'};
private static final BigInteger valueOctal = new BigInteger("8");
/**
* This method converts and decimal number to a octal number
*
* @param decimalStr
* @return octal number
*/
public String decimalToOctal(String decimalStr){
public String decimalToOctal(String decimalStr) {
BigInteger decimal = new BigInteger(decimalStr);
int rem;
String octal = "";
while(decimal.compareTo(BigInteger.ZERO) > 0) {
while (decimal.compareTo(BigInteger.ZERO) > 0) {
rem = decimal.mod(valueOctal).intValueExact();
octal = octalChars[rem] + octal;
decimal = decimal.divide(valueOctal);

View File

@ -2,9 +2,10 @@ package src.main.java.com.dataStructures;
/**
* Binary tree for general value type, without redundancy
* @author RICARDO
*
* @param <T> root data
*/
public class BinaryTree<T extends Comparable> {
private final T data;
private BinaryTree right, // the upper binary tree
@ -15,23 +16,25 @@ public class BinaryTree<T extends Comparable> {
}
@Override
public String toString(){
public String toString() {
return this.data.toString();
}
/**
* inserts a new value in it's correspondant place
* @param newDataValue value of the new banary tree to add on this tree
*
* @param newDataValue value of the new binary tree to add on this tree
*/
public void insert(T newDataValue){
public void insert(T newDataValue) {
this.insert(new BinaryTree(newDataValue));
}
/**
* inserts a new binary tree in it's correspondant place
*
* @param newData new value to add on this tree
*/
public void insert(BinaryTree newData){
public void insert(BinaryTree newData) {
int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value
@ -51,10 +54,11 @@ public class BinaryTree<T extends Comparable> {
/**
* search and specific value on the tree
*
* @param data Searched value
* @return Binary tree wich contains the value, null if it doesn't exist
* @return Binary tree which contains the value, null if it doesn't exist
*/
public BinaryTree search(T data){
public BinaryTree search(T data) {
int cpr = data.compareTo(this.data); //new value comparission respect to actual value
if (cpr < 0) {
@ -72,18 +76,20 @@ public class BinaryTree<T extends Comparable> {
/**
* Checks if the data value exist in the tree
*
* @param data data to be searched
* @return true if this tree contains the data value, false if not.
*/
public boolean contains(T data){
public boolean contains(T data) {
return this.search(data) != null;
}
/**
* uses recursive black magic to print this tree in console
*
* @param tabCounter prev tabs
*/
private void print(int tabCounter){
private void print(int tabCounter) {
for (int i = 0; i < tabCounter; i++)
System.out.print("\t");
@ -98,7 +104,7 @@ public class BinaryTree<T extends Comparable> {
/**
* uses black magic to print this tree in console
*/
public void print(){
public void print() {
this.print(0);
}

View File

@ -15,7 +15,6 @@ import java.util.*;
* <p>
* 2. quickly query two elements whether contained in the same set, requiring about O(1) time.
*
* @author yangxf
*/
public class DisjointSet<T> implements Serializable {
private static final long serialVersionUID = 3134700471905625636L;

View File

@ -25,14 +25,14 @@ public class SimplexNoise {
this.persistance = persistence;
this.seed = seed;
int octaveCount = (int)Math.ceil(Math.log10(largestFeature) / Math.log10(2.0D));
int octaveCount = (int) Math.ceil(Math.log10(largestFeature) / Math.log10(2.0D));
this.octaves = new SimplexNoiseOctave[octaveCount];
this.frequencys = new double[octaveCount];
this.amplitudes = new double[octaveCount];
Random random = new Random(seed);
for(int index = 0; index < octaveCount; index++) {
for (int index = 0; index < octaveCount; index++) {
this.octaves[index] = new SimplexNoiseOctave(random.nextInt());
this.frequencys[index] = Math.pow(2, index);
@ -42,6 +42,7 @@ public class SimplexNoise {
/**
* Generates a height map.
*
* @param x X coordinate
* @param y Y coordinate
* @param width width
@ -55,13 +56,13 @@ public class SimplexNoise {
float[][] result = new float[width][height];
for(int i = 0; i < width; i++) {
for (int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
for (int j = 0; j < height; j++) {
int posX = x + i * ((xEnd - x) / width);
int posY = y + j * ((yEnd - y) / height);
result[i][j] = Math.min(1.0F, Math.max(0.0F, (float)(0.5D * (1 + this.getNoise(posX, posY)))));
result[i][j] = Math.min(1.0F, Math.max(0.0F, (float) (0.5D * (1 + this.getNoise(posX, posY)))));
}
}
@ -70,6 +71,7 @@ public class SimplexNoise {
/**
* Generates a two dimensional noise.
*
* @param x X coordinate
* @param y Y coordinate
* @return the generated noise
@ -78,7 +80,7 @@ public class SimplexNoise {
double result = 0;
for(int index = 0; index < this.octaves.length; index++) {
for (int index = 0; index < this.octaves.length; index++) {
result += this.octaves[index].noise(x / this.frequencys[index], y / this.frequencys[index]) * this.amplitudes[index];
}
@ -88,6 +90,7 @@ public class SimplexNoise {
/**
* Generates a three dimensional noise.
*
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
@ -97,7 +100,7 @@ public class SimplexNoise {
double result = 0;
for(int index = 0; index < this.octaves.length; index++) {
for (int index = 0; index < this.octaves.length; index++) {
double frequency = Math.pow(2, index);
double amplitude = Math.pow(this.persistance, this.octaves.length - index);

View File

@ -14,7 +14,7 @@ public class SimplexNoiseOctave {
new Gradient(1, 0, -1), new Gradient(-1, 0, -1), new Gradient(0, 1, 1),
new Gradient(0, -1, 1), new Gradient(0, 1, -1), new Gradient(0, -1, -1)
};
private static final short P_SUPPLY[] = {
private static final short[] P_SUPPLY = {
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,

View File

@ -0,0 +1,36 @@
package src.main.java.com.others;
import java.math.BigInteger;
/**
* We may calculate power with loops, but what if the index is too large ?
* FastPower aims to calculate quickly in this circumstances with time complexity O(log k),
* where k is the index.
*
*/
public class FastPower {
public static BigInteger calculate(BigInteger n, BigInteger k, BigInteger mod) {
BigInteger ans = BigInteger.ONE;
while (!k.equals(BigInteger.ZERO)) {
int odd = k.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO);
if (odd > 0) {
ans = ans.multiply(n).mod(mod);
}
k = k.divide(BigInteger.valueOf(2));
n = n.multiply(n).mod(mod);
}
return ans.mod(mod);
}
public static long calculate(long n, long k, long mod) {
long ans = 1;
while (k != 0) {
if (k % 2 == 1) {
ans = (ans * n) % mod;
}
k >>= 1;
n = (n * n) % mod;
}
return ans % mod;
}
}

View File

@ -16,7 +16,6 @@ import java.util.function.Function;
* <p>
* The accuracy rate depends on capacity and hash functions.
*
* @author yangxf
*/
public class BloomFilter implements Serializable {
private static final long serialVersionUID = -4466610350741278658L;

View File

@ -20,9 +20,11 @@ public class ExponentialSearch {
* @return The index position of the key in the array, returns -1 for empty array
*/
public <T extends Comparable<T>> int findIndex(T[] array, T key) {
int size = array.length;
if(size == 0)
if (array == null || array.length == 0) {
return -1;
}
int size = array.length;
// If the element is present at first position
if (array[0] == key)
return 0;
@ -30,10 +32,10 @@ public class ExponentialSearch {
// Find the range for binary search by repeated doubling
int i = 1;
while (i < size && array[i].compareTo(key) <= 0) {
i = i * 2;
i <<= 1;
}
// Call binary search for the range found
return Arrays.binarySearch(array, i / 2, Math.min(i, size), key);
return Arrays.binarySearch(array, i >> 1, Math.min(i, size), key);
}
}

View File

@ -15,7 +15,7 @@ public final class LinearSearch {
* @param <T> is any comparable type
* @return index of the element
*/
public static <T extends Comparable<T>> int findIndex(T array[], T key) {
public static <T extends Comparable<T>> int findIndex(T[] array, T key) {
return search(array, key);
}
@ -24,7 +24,7 @@ public final class LinearSearch {
* @param key The element you are looking for
* @return the location of the key or -1 if the element is not found
**/
private static <T extends Comparable<T>> int search(T array[], T key){
private static <T extends Comparable<T>> int search(T[] array, T key){
for(int i = 0; i < array.length;i++) {
if (array[i].compareTo(key) == 0){
return i;

View File

@ -9,7 +9,6 @@ import static src.main.java.com.sorts.SortUtils.swap;
public class HeapSort {
private static class Heap<T extends Comparable<T>> {
/**
* Array to store heap

View File

@ -5,7 +5,6 @@ import static src.main.java.com.sorts.SortUtils.swap;
public class QuickSort {
/**
* This method implements the Generic Quick Sort
*

View File

@ -8,7 +8,7 @@ public class StoogeSort {
/**
* This method implements recursion StoogeSort
*
* @param int[] array to store number elements
* @param arr array to store number elements
* @param f first element in the array
* @param l last element in the array
*/

View File

@ -3,5 +3,5 @@ package src.main.java.com.types;
@FunctionalInterface
public interface Sort<T> {
public <T extends Comparable<T>> T[] sort(T[] array);
<T extends Comparable<T>> T[] sort(T[] array);
}

View File

@ -0,0 +1,17 @@
package src.test.java.com.conversions;
import src.main.java.com.conversions.BinaryToGray;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class BinaryToGrayTest {
@Test
public void testBinaryToGray() {
BinaryToGray binaryToGray = new BinaryToGray();
assertEquals("1101", binaryToGray.binaryToGray("1001"));
assertEquals("11010011101", binaryToGray.binaryToGray("10011101001"));
}
}

View File

@ -1,12 +1,10 @@
package src.main.java.com.dataStructures;
package src.test.java.com.dataStructures;
import org.junit.Test;
import src.main.java.com.dataStructures.BinaryTree;
import static org.junit.Assert.*;
/**
*
* @author RICARDO
*/
public class BinaryTreeTest {
public BinaryTreeTest() {
@ -16,15 +14,16 @@ public class BinaryTreeTest {
* Test of insert method, of class BinaryTree.
*/
@Test
public void testInsert_BinaryTree() {
public void testInsertBinaryTree() {
System.out.println("insert");
BinaryTree<String> lowerdata = new BinaryTree<>("1");
BinaryTree<String> upperdata = new BinaryTree<>("3");
BinaryTree<String> lowerData = new BinaryTree<>("1");
BinaryTree<String> upperData = new BinaryTree<>("3");
BinaryTree<String> instance = new BinaryTree<>("2");
instance.insert(lowerdata);
instance.insert(upperdata);
String proof = instance.getLeft().toString()+instance.toString()+instance.getRight().toString();
System.out.println(proof);
instance.insert(lowerData);
instance.insert(upperData);
String proof = instance.getLeft().toString()
+ instance.toString()
+ instance.getRight().toString();
assertEquals("123", proof);
}
@ -36,10 +35,10 @@ public class BinaryTreeTest {
System.out.println("search");
BinaryTree<Integer> instance = new BinaryTree<>(5);
for (int i = 1; i < 10; i++) {
instance.insert(new Integer(i));
instance.insert(i);
}
BinaryTree result = instance.search(new Integer(1));
assertEquals(new Integer(1), result.getData());
BinaryTree result = instance.search(1);
assertEquals(1, result.getData());
}
/**
@ -53,8 +52,7 @@ public class BinaryTreeTest {
instance.insert(i);
}
boolean result = instance.contains(2)&&instance.contains(11);
assertEquals(false, result);
boolean result = instance.contains(2) && instance.contains(11);
assertFalse(result);
}
}

View File

@ -4,12 +4,9 @@ import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.junit.Assert;
import org.junit.Test;
import src.main.java.com.generation.SimplexNoise;
public class SimplexNoiseTest {
@ -34,7 +31,6 @@ public class SimplexNoiseTest {
Assert.assertEquals(HEIGHT, image.getHeight());
} catch (IOException | IllegalArgumentException exception) {
Assert.fail(exception.toString());
}

View File

@ -0,0 +1,35 @@
package src.test.java.com.others;
import org.junit.Test;
import src.main.java.com.others.FastPower;
import java.math.BigInteger;
import static org.junit.Assert.*;
public class FastPowerTest {
@Test
void testLong(long n, long k, long m) {
long result = FastPower.calculate(n, k, m);
assertEquals(result, BigInteger.valueOf(n).modPow(BigInteger.valueOf(k), BigInteger.valueOf(m)).longValue());
}
@Test
void testBigInteger(BigInteger n, BigInteger k, BigInteger m) {
BigInteger result = FastPower.calculate(n, k, m);
assertEquals(result, n.modPow(k, m));
}
@Test
public void test() {
testLong(2, 2, 10);
testLong(100, 1000, 20);
testLong(123456, 123456789, 234);
testBigInteger(BigInteger.TEN, BigInteger.TEN, BigInteger.valueOf(4));
testBigInteger(new BigInteger("123456"), new BigInteger("123456789"), new BigInteger("234"));
testBigInteger(new BigInteger("123456789101112"), new BigInteger("12345678910111213"), new BigInteger("567890"));
}
}