Merge branch 'Development' of https://github.com/TheAlgorithms/Java into Development

This commit is contained in:
Abhijay Kumar 2020-08-25 18:08:46 +05:30
parent 60c77e5730
commit 32b02af2bb
18 changed files with 50 additions and 53 deletions

View File

@ -75,8 +75,8 @@ class LFUCache<T> {
public LFUCache(int capacity) { public LFUCache(int capacity) {
this.capacity = capacity; this.capacity = capacity;
size = 0; size = 0;
freq = new TreeMap<>(); freq = new TreeMap<Integer, DLL>();
map = new HashMap<>(); map = new HashMap<Integer, Node>();
System.out.println("LFUCache initialised with capacity: " + capacity); System.out.println("LFUCache initialised with capacity: " + capacity);
} }
@ -145,8 +145,8 @@ class LFUCache<T> {
dll.deleteNode(dll.tail.pre); dll.deleteNode(dll.tail.pre);
if (dll.len == 0 && lowest != 1) if (dll.len == 0 && lowest != 1)
freq.remove(lowest); freq.remove(lowest);
DLL freqOne = freq.computeIfAbsent(1, k -> new DLL()); DLL freq_one = freq.computeIfAbsent(1, k -> new DLL());
freqOne.addToHead(node); freq_one.addToHead(node);
} }
} }

View File

@ -63,6 +63,7 @@ public class LRUCache<T> {
System.out.println("Cache set to 0 capacity. No elements will be cached"); System.out.println("Cache set to 0 capacity. No elements will be cached");
} }
T currentValue = cache.get(key);
if (!cache.containsKey(key)) { if (!cache.containsKey(key)) {
cache.put(key, value); cache.put(key, value);
System.out.println("Adding new key:" + key + " to cache"); System.out.println("Adding new key:" + key + " to cache");

View File

@ -5,25 +5,25 @@ public class CaesarBruteForce {
/** /**
* Recursively Brute forces a parsed encrypted text, trying out all shifting keys from 1-26, printing out all decryption attempts * Recursively Brute forces a parsed encrypted text, trying out all shifting keys from 1-26, printing out all decryption attempts
* @param message (String) The encrypted text. * @param message (String) The encrypted text.
* @param key (int) The key used to decrypt the encrypted text and is increment upon a recursive call. * @param Key (int) The key used to decrypt the encrypted text and is increment upon a recursive call.
* @return (String) Concatenated string of all decryption attempts (For unit testing purposes). * @return (String) Concatenated string of all decryption attempts (For unit testing purposes).
*/ */
public String decrypt(String message, int key) { public String decrypt(String message, int Key) {
final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (key > 26){ System.out.println(); return null; } if (Key > 26){ System.out.println(); return null; }
StringBuilder plainText = new StringBuilder(); StringBuilder plainText = new StringBuilder();
for (char character : message.toUpperCase().toCharArray()) { for (char character : message.toUpperCase().toCharArray()) {
int index = LETTERS.indexOf(character); int index = LETTERS.indexOf(character);
if (index != -1) { if (index != -1) {
index -= key; index -= Key;
//Wrap around index value range[1-26] //Wrap around index value range[1-26]
if (index < 0) { index += LETTERS.length(); } if (index < 0) { index += LETTERS.length(); }
plainText.append(LETTERS.toCharArray()[index]); plainText.append(LETTERS.toCharArray()[index]);
} else { plainText.append(character); } } else { plainText.append(character); }
} }
System.out.println(String.format("Current Decryption Key %d : %s", key, plainText)); System.out.println(String.format("Current Decryption Key %d : %s", Key, plainText));
return plainText.append(decrypt(message, key+1)).toString(); return plainText.append(decrypt(message, Key+1)).toString();
} }
} }

View File

@ -12,7 +12,7 @@ public class HexadecimalToBinary {
public String hexToBin (String hexStr) { public String hexToBin (String hexStr) {
String binaryString = "", hexaNumbers = "0123456789ABCDEF", String binaryString = "", hexaNumbers = "0123456789ABCDEF",
decimalStr ="", binaryStringBefore ="" , binaryStringAfter = ""; DecimalStr ="", binaryStringBefore ="" , binaryStringAfter = "";
int indexOfHex, decimalNumber = 0, k = 1, n =1, z=1, decimalNumberBefore = 0 int indexOfHex, decimalNumber = 0, k = 1, n =1, z=1, decimalNumberBefore = 0
, decimalNumberAfter = 0; , decimalNumberAfter = 0;
char letter; char letter;
@ -48,12 +48,12 @@ public class HexadecimalToBinary {
String decimalNumberAfterStr = String.valueOf(decimalNumberAfter); String decimalNumberAfterStr = String.valueOf(decimalNumberAfter);
decimalStr = decimalNumberBeforeStr + '.' + decimalNumberAfterStr; DecimalStr = decimalNumberBeforeStr + '.' + decimalNumberAfterStr;
} }
int pointPositionDec = decimalStr.indexOf("."); int pointPositionDec = DecimalStr.indexOf(".");
/** /**
* Check whether the result contains a floating point or not * Check whether the result contains a floating point or not
*/ */

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
/** /**
* Binary tree for general value type, without redundancy * Binary tree for general value type, without redundancy
@ -8,7 +8,7 @@ package com.datastructures;
public class BinaryTree<T extends Comparable> { public class BinaryTree<T extends Comparable> {
private final T data; private final T data;
private BinaryTree<T> right, // the upper binary tree private BinaryTree right, // the upper binary tree
left; // the lower binary tree left; // the lower binary tree
public BinaryTree(T data) { public BinaryTree(T data) {
@ -21,39 +21,36 @@ public class BinaryTree<T extends Comparable> {
} }
/** /**
* inserts a new value in it's correspondent place * inserts a new value in it's correspondant place
* *
* @param newDataValue value of the new binary 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)); this.insert(new BinaryTree(newDataValue));
} }
/** /**
* inserts a new binary tree in it's correspondent place * inserts a new binary tree in it's correspondant place
* *
* @param newData new value to add on this tree * @param newData new value to add on this tree
*/ */
public void insert(BinaryTree<T> newData) { public void insert(BinaryTree newData) {
int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value
if (cpr < 0) { if (cpr < 0)
if (this.left == null) { if (this.left == null)
this.setLeft(newData); this.setLeft(newData);
} else { else
this.left.insert(newData); this.left.insert(newData);
} else if (cpr > 0)
} else if (cpr > 0) { if (this.right == null)
if (this.right == null) {
this.setRight(newData); this.setRight(newData);
} else { else
this.right.insert(newData); this.right.insert(newData);
} else
} else {
System.out.println("Redundant value, not added"); System.out.println("Redundant value, not added");
} }
}
/** /**
* search and specific value on the tree * search and specific value on the tree
@ -61,8 +58,8 @@ public class BinaryTree<T extends Comparable> {
* @param data Searched value * @param data Searched value
* @return Binary tree which contains the value, null if it doesn't exist * @return Binary tree which contains the value, null if it doesn't exist
*/ */
public BinaryTree<T> search(T data) { public BinaryTree search(T data) {
int cpr = data.compareTo(this.data); //new value comparison respect to actual value int cpr = data.compareTo(this.data); //new value comparission respect to actual value
if (cpr < 0) { if (cpr < 0) {
if (this.left == null) if (this.left == null)
@ -116,19 +113,19 @@ public class BinaryTree<T extends Comparable> {
return data; return data;
} }
public BinaryTree<T> getRight() { public BinaryTree getRight() {
return right; return right;
} }
public void setRight(BinaryTree<T> right) { public void setRight(BinaryTree right) {
this.right = right; this.right = right;
} }
public BinaryTree<T> getLeft() { public BinaryTree getLeft() {
return left; return left;
} }
public void setLeft(BinaryTree<T> left) { public void setLeft(BinaryTree left) {
this.left = left; this.left = left;
} }
} }

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.*;
@ -18,15 +18,14 @@ import java.util.*;
*/ */
public class DisjointSet<T> implements Serializable { public class DisjointSet<T> implements Serializable {
private static final long serialVersionUID = 3134700471905625636L; private static final long serialVersionUID = 3134700471905625636L;
private static final String elementKey = "element";
private final Map<T, Node<T>> nodeMap = new HashMap<>(); private Map<T, Node<T>> nodeMap = new HashMap<>();
/** /**
* Add an element to the disjoint-set forests as a set. * Add an element to the disjoint-set forests as a set.
*/ */
public void makeSet(T element) { public void makeSet(T element) {
checkNotNull(element, elementKey); checkNotNull(element, "element");
nodeMap.putIfAbsent(element, new Node<>()); nodeMap.putIfAbsent(element, new Node<>());
} }
@ -37,8 +36,8 @@ public class DisjointSet<T> implements Serializable {
* Rank is an upper bound on the height of node. * Rank is an upper bound on the height of node.
*/ */
public void union(T left, T right) { public void union(T left, T right) {
checkNotNull(left, elementKey); checkNotNull(left, "element");
checkNotNull(right, elementKey); checkNotNull(right, "element");
Node<T> leftNode = nodeMap.get(left), Node<T> leftNode = nodeMap.get(left),
rightNode = nodeMap.get(right); rightNode = nodeMap.get(right);

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import com.types.Queue; import com.types.Queue;

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
/** /**
* This file contains an implementation of an integer only queue which is extremely quick and * This file contains an implementation of an integer only queue which is extremely quick and

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import java.io.Serializable; import java.io.Serializable;
import java.util.EmptyStackException; import java.util.EmptyStackException;

View File

@ -1,4 +1,4 @@
package com.matchings.stablematching; package com.matchings.stableMatching;
public class GaleShapley { public class GaleShapley {

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import com.types.Queue; import com.types.Queue;

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import static org.junit.Assert.*; import static org.junit.Assert.*;

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;

View File

@ -1,4 +1,4 @@
package com.datastructures; package com.dataStructures;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

View File

@ -1,4 +1,4 @@
package com.matchings.stablematching; package com.matchings.stableMatching;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;