From 32b02af2bb83e8dc315a85d72f12d8076660f56b Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Tue, 25 Aug 2020 18:08:46 +0530 Subject: [PATCH] Merge branch 'Development' of https://github.com/TheAlgorithms/Java into Development --- src/main/java/com/caching/LFUCache.java | 8 ++-- src/main/java/com/caching/LRUCache.java | 1 + .../java/com/ciphers/CaesarBruteForce.java | 12 +++--- .../com/conversions/HexadecimalToBinary.java | 6 +-- .../java/com/datastructures/BinaryTree.java | 41 +++++++++---------- .../java/com/datastructures/DisjointSet.java | 11 +++-- .../java/com/datastructures/GeneralQueue.java | 2 +- .../java/com/datastructures/IntQueue.java | 2 +- .../com/datastructures/SinglyLinkedList.java | 2 +- src/main/java/com/datastructures/Stack.java | 2 +- .../matchings/stablematching/GaleShapley.java | 2 +- .../com/datastructures/BinaryTreeTest.java | 2 +- .../com/datastructures/DisjointSetTest.java | 2 +- .../com/datastructures/GeneralQueueTest.java | 2 +- .../java/com/datastructures/IntQueueTest.java | 2 +- .../datastructures/SinglyLinkedListTest.java | 2 +- .../java/com/datastructures/StackTest.java | 2 +- .../stablematching/GaleShapleyTest.java | 2 +- 18 files changed, 50 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/caching/LFUCache.java b/src/main/java/com/caching/LFUCache.java index 3eb78de8..c61ba6c7 100644 --- a/src/main/java/com/caching/LFUCache.java +++ b/src/main/java/com/caching/LFUCache.java @@ -75,8 +75,8 @@ class LFUCache { public LFUCache(int capacity) { this.capacity = capacity; size = 0; - freq = new TreeMap<>(); - map = new HashMap<>(); + freq = new TreeMap(); + map = new HashMap(); System.out.println("LFUCache initialised with capacity: " + capacity); } @@ -145,8 +145,8 @@ class LFUCache { dll.deleteNode(dll.tail.pre); if (dll.len == 0 && lowest != 1) freq.remove(lowest); - DLL freqOne = freq.computeIfAbsent(1, k -> new DLL()); - freqOne.addToHead(node); + DLL freq_one = freq.computeIfAbsent(1, k -> new DLL()); + freq_one.addToHead(node); } } diff --git a/src/main/java/com/caching/LRUCache.java b/src/main/java/com/caching/LRUCache.java index 034d7881..6feffea4 100644 --- a/src/main/java/com/caching/LRUCache.java +++ b/src/main/java/com/caching/LRUCache.java @@ -63,6 +63,7 @@ public class LRUCache { System.out.println("Cache set to 0 capacity. No elements will be cached"); } + T currentValue = cache.get(key); if (!cache.containsKey(key)) { cache.put(key, value); System.out.println("Adding new key:" + key + " to cache"); diff --git a/src/main/java/com/ciphers/CaesarBruteForce.java b/src/main/java/com/ciphers/CaesarBruteForce.java index 88f71569..ab60fec0 100644 --- a/src/main/java/com/ciphers/CaesarBruteForce.java +++ b/src/main/java/com/ciphers/CaesarBruteForce.java @@ -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 * @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). */ - public String decrypt(String message, int key) { + public String decrypt(String message, int Key) { final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - if (key > 26){ System.out.println(); return null; } + if (Key > 26){ System.out.println(); return null; } StringBuilder plainText = new StringBuilder(); for (char character : message.toUpperCase().toCharArray()) { int index = LETTERS.indexOf(character); if (index != -1) { - index -= key; + index -= Key; //Wrap around index value range[1-26] if (index < 0) { index += LETTERS.length(); } plainText.append(LETTERS.toCharArray()[index]); } else { plainText.append(character); } } - System.out.println(String.format("Current Decryption Key %d : %s", key, plainText)); - return plainText.append(decrypt(message, key+1)).toString(); + System.out.println(String.format("Current Decryption Key %d : %s", Key, plainText)); + return plainText.append(decrypt(message, Key+1)).toString(); } } diff --git a/src/main/java/com/conversions/HexadecimalToBinary.java b/src/main/java/com/conversions/HexadecimalToBinary.java index da3a01ac..6758606e 100644 --- a/src/main/java/com/conversions/HexadecimalToBinary.java +++ b/src/main/java/com/conversions/HexadecimalToBinary.java @@ -12,7 +12,7 @@ public class HexadecimalToBinary { public String hexToBin (String hexStr) { String binaryString = "", hexaNumbers = "0123456789ABCDEF", - decimalStr ="", binaryStringBefore ="" , binaryStringAfter = ""; + DecimalStr ="", binaryStringBefore ="" , binaryStringAfter = ""; int indexOfHex, decimalNumber = 0, k = 1, n =1, z=1, decimalNumberBefore = 0 , decimalNumberAfter = 0; char letter; @@ -48,12 +48,12 @@ public class HexadecimalToBinary { 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 */ diff --git a/src/main/java/com/datastructures/BinaryTree.java b/src/main/java/com/datastructures/BinaryTree.java index 5bf1702e..4aae62fe 100644 --- a/src/main/java/com/datastructures/BinaryTree.java +++ b/src/main/java/com/datastructures/BinaryTree.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; /** * Binary tree for general value type, without redundancy @@ -8,7 +8,7 @@ package com.datastructures; public class BinaryTree { private final T data; - private BinaryTree right, // the upper binary tree + private BinaryTree right, // the upper binary tree left; // the lower binary tree public BinaryTree(T data) { @@ -21,38 +21,35 @@ public class BinaryTree { } /** - * 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 */ 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 */ - public void insert(BinaryTree newData) { + public void insert(BinaryTree newData) { int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value - if (cpr < 0) { - if (this.left == null) { + if (cpr < 0) + if (this.left == null) this.setLeft(newData); - } else { + else this.left.insert(newData); - } - } else if (cpr > 0) { - if (this.right == null) { + else if (cpr > 0) + if (this.right == null) this.setRight(newData); - } else { + else this.right.insert(newData); - } - } else { + else System.out.println("Redundant value, not added"); - } } /** @@ -61,8 +58,8 @@ public class BinaryTree { * @param data Searched value * @return Binary tree which contains the value, null if it doesn't exist */ - public BinaryTree search(T data) { - int cpr = data.compareTo(this.data); //new value comparison respect to actual value + public BinaryTree search(T data) { + int cpr = data.compareTo(this.data); //new value comparission respect to actual value if (cpr < 0) { if (this.left == null) @@ -116,19 +113,19 @@ public class BinaryTree { return data; } - public BinaryTree getRight() { + public BinaryTree getRight() { return right; } - public void setRight(BinaryTree right) { + public void setRight(BinaryTree right) { this.right = right; } - public BinaryTree getLeft() { + public BinaryTree getLeft() { return left; } - public void setLeft(BinaryTree left) { + public void setLeft(BinaryTree left) { this.left = left; } } diff --git a/src/main/java/com/datastructures/DisjointSet.java b/src/main/java/com/datastructures/DisjointSet.java index 281bbf16..3cb0d698 100644 --- a/src/main/java/com/datastructures/DisjointSet.java +++ b/src/main/java/com/datastructures/DisjointSet.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import java.io.Serializable; import java.util.*; @@ -18,15 +18,14 @@ import java.util.*; */ public class DisjointSet implements Serializable { private static final long serialVersionUID = 3134700471905625636L; - private static final String elementKey = "element"; - private final Map> nodeMap = new HashMap<>(); + private Map> nodeMap = new HashMap<>(); /** * Add an element to the disjoint-set forests as a set. */ public void makeSet(T element) { - checkNotNull(element, elementKey); + checkNotNull(element, "element"); nodeMap.putIfAbsent(element, new Node<>()); } @@ -37,8 +36,8 @@ public class DisjointSet implements Serializable { * Rank is an upper bound on the height of node. */ public void union(T left, T right) { - checkNotNull(left, elementKey); - checkNotNull(right, elementKey); + checkNotNull(left, "element"); + checkNotNull(right, "element"); Node leftNode = nodeMap.get(left), rightNode = nodeMap.get(right); diff --git a/src/main/java/com/datastructures/GeneralQueue.java b/src/main/java/com/datastructures/GeneralQueue.java index 1749bb10..3d092082 100644 --- a/src/main/java/com/datastructures/GeneralQueue.java +++ b/src/main/java/com/datastructures/GeneralQueue.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import com.types.Queue; diff --git a/src/main/java/com/datastructures/IntQueue.java b/src/main/java/com/datastructures/IntQueue.java index 901609c6..d300f383 100644 --- a/src/main/java/com/datastructures/IntQueue.java +++ b/src/main/java/com/datastructures/IntQueue.java @@ -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 diff --git a/src/main/java/com/datastructures/SinglyLinkedList.java b/src/main/java/com/datastructures/SinglyLinkedList.java index 6d1e5e98..4525e3a7 100644 --- a/src/main/java/com/datastructures/SinglyLinkedList.java +++ b/src/main/java/com/datastructures/SinglyLinkedList.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; diff --git a/src/main/java/com/datastructures/Stack.java b/src/main/java/com/datastructures/Stack.java index e71d3cd1..7c562f5b 100644 --- a/src/main/java/com/datastructures/Stack.java +++ b/src/main/java/com/datastructures/Stack.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import java.io.Serializable; import java.util.EmptyStackException; diff --git a/src/main/java/com/matchings/stablematching/GaleShapley.java b/src/main/java/com/matchings/stablematching/GaleShapley.java index f8d94b62..1e1e1ab9 100644 --- a/src/main/java/com/matchings/stablematching/GaleShapley.java +++ b/src/main/java/com/matchings/stablematching/GaleShapley.java @@ -1,4 +1,4 @@ -package com.matchings.stablematching; +package com.matchings.stableMatching; public class GaleShapley { diff --git a/src/test/java/com/datastructures/BinaryTreeTest.java b/src/test/java/com/datastructures/BinaryTreeTest.java index a574692e..1fcfeb7d 100644 --- a/src/test/java/com/datastructures/BinaryTreeTest.java +++ b/src/test/java/com/datastructures/BinaryTreeTest.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/datastructures/DisjointSetTest.java b/src/test/java/com/datastructures/DisjointSetTest.java index 79e65df1..d85871a7 100644 --- a/src/test/java/com/datastructures/DisjointSetTest.java +++ b/src/test/java/com/datastructures/DisjointSetTest.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/datastructures/GeneralQueueTest.java b/src/test/java/com/datastructures/GeneralQueueTest.java index d4794d02..3c4c3a7a 100644 --- a/src/test/java/com/datastructures/GeneralQueueTest.java +++ b/src/test/java/com/datastructures/GeneralQueueTest.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import com.types.Queue; diff --git a/src/test/java/com/datastructures/IntQueueTest.java b/src/test/java/com/datastructures/IntQueueTest.java index f9f87199..4e107c88 100644 --- a/src/test/java/com/datastructures/IntQueueTest.java +++ b/src/test/java/com/datastructures/IntQueueTest.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import static org.junit.Assert.*; diff --git a/src/test/java/com/datastructures/SinglyLinkedListTest.java b/src/test/java/com/datastructures/SinglyLinkedListTest.java index 0396bf3d..a5b87b63 100644 --- a/src/test/java/com/datastructures/SinglyLinkedListTest.java +++ b/src/test/java/com/datastructures/SinglyLinkedListTest.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import org.junit.Test; import org.junit.jupiter.api.Assertions; diff --git a/src/test/java/com/datastructures/StackTest.java b/src/test/java/com/datastructures/StackTest.java index 4759d17b..c6b80f15 100644 --- a/src/test/java/com/datastructures/StackTest.java +++ b/src/test/java/com/datastructures/StackTest.java @@ -1,4 +1,4 @@ -package com.datastructures; +package com.dataStructures; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/matchings/stablematching/GaleShapleyTest.java b/src/test/java/com/matchings/stablematching/GaleShapleyTest.java index 6506bd07..d23db538 100644 --- a/src/test/java/com/matchings/stablematching/GaleShapleyTest.java +++ b/src/test/java/com/matchings/stablematching/GaleShapleyTest.java @@ -1,4 +1,4 @@ -package com.matchings.stablematching; +package com.matchings.stableMatching; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test;