Code cleanup (#4246)

This commit is contained in:
HManiac74 2023-07-22 17:23:00 +02:00 committed by GitHub
parent 3facb0d862
commit 2488a2ad51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
52 changed files with 50 additions and 167 deletions

View File

@ -1,5 +1,4 @@
package com.thealgorithms.conversions; package com.thealgorithms.conversions;
import java.util.Scanner;
/** /**
* Converts any Octal Number to a Binary Number * Converts any Octal Number to a Binary Number

View File

@ -1,7 +1,6 @@
package com.thealgorithms.datastructures.graphs; package com.thealgorithms.datastructures.graphs;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Stack; import java.util.Stack;

View File

@ -28,18 +28,12 @@ public class DoublyLinkedList {
*/ */
private LinkOperations linkOperations; private LinkOperations linkOperations;
/**
* Size refers to the number of elements present in the list
*/
private int size;
/** /**
* Default Constructor * Default Constructor
*/ */
public DoublyLinkedList() { public DoublyLinkedList() {
head = null; head = null;
tail = null; tail = null;
size = 0;
} }
/** /**
@ -55,7 +49,6 @@ public class DoublyLinkedList {
for (int i : array) { for (int i : array) {
linkOperations.insertTail(i, this); linkOperations.insertTail(i, this);
} }
size = array.length;
} }
/** /**

View File

@ -47,10 +47,5 @@ public class Merge_K_SortedLinkedlist {
private int data; private int data;
private Node next; private Node next;
public Node(int d) {
this.data = d;
next = null;
}
} }
} }

View File

@ -148,9 +148,7 @@ public class SinglyLinkedList extends Node {
public void clear() { public void clear() {
Node cur = head; Node cur = head;
while (cur != null) { while (cur != null) {
Node prev = cur;
cur = cur.next; cur = cur.next;
prev = null; // clear to let GC do its work
} }
head = null; head = null;
size = 0; size = 0;
@ -346,9 +344,7 @@ public class SinglyLinkedList extends Node {
public void deleteNth(int position) { public void deleteNth(int position) {
checkBounds(position, 0, size - 1); checkBounds(position, 0, size - 1);
if (position == 0) { if (position == 0) {
Node destroy = head;
head = head.next; head = head.next;
destroy = null;
/* clear to let GC do its work */ /* clear to let GC do its work */
size--; size--;
return; return;
@ -358,10 +354,7 @@ public class SinglyLinkedList extends Node {
cur = cur.next; cur = cur.next;
} }
Node destroy = cur.next;
cur.next = cur.next.next; cur.next = cur.next.next;
destroy = null; // clear to let GC do its work
size--; size--;
} }

View File

@ -43,7 +43,6 @@ public class NodeStack<Item> {
private Item data; private Item data;
private static NodeStack<?> head; private static NodeStack<?> head;
private NodeStack<?> next;
private NodeStack<?> previous; private NodeStack<?> previous;
private static int size = 0; private static int size = 0;
@ -72,7 +71,7 @@ public class NodeStack<Item> {
} else { } else {
newNs.setPrevious(NodeStack.head); newNs.setPrevious(NodeStack.head);
NodeStack.head.setNext(newNs); NodeStack.head.setNext(newNs);
NodeStack.head.setHead(newNs); NodeStack.setHead(newNs);
} }
NodeStack.setSize(NodeStack.getSize() + 1); NodeStack.setSize(NodeStack.getSize() + 1);
@ -86,7 +85,7 @@ public class NodeStack<Item> {
public Item pop() { public Item pop() {
Item item = (Item) NodeStack.head.getData(); Item item = (Item) NodeStack.head.getData();
NodeStack.head.setHead(NodeStack.head.getPrevious()); NodeStack.setHead(NodeStack.head.getPrevious());
NodeStack.head.setNext(null); NodeStack.head.setNext(null);
NodeStack.setSize(NodeStack.getSize() - 1); NodeStack.setSize(NodeStack.getSize() - 1);
@ -133,23 +132,11 @@ public class NodeStack<Item> {
} }
} }
/**
* Getters and setters (private)
*/
private NodeStack<?> getHead() {
return NodeStack.head;
}
private static void setHead(NodeStack<?> ns) { private static void setHead(NodeStack<?> ns) {
NodeStack.head = ns; NodeStack.head = ns;
} }
private NodeStack<?> getNext() {
return next;
}
private void setNext(NodeStack<?> next) { private void setNext(NodeStack<?> next) {
this.next = next;
} }
private NodeStack<?> getPrevious() { private NodeStack<?> getPrevious() {
@ -171,8 +158,4 @@ public class NodeStack<Item> {
private Item getData() { private Item getData() {
return this.data; return this.data;
} }
private void setData(Item item) {
this.data = item;
}
} }

View File

@ -23,8 +23,6 @@ public class GenericTree {
} }
private Node root; private Node root;
private int size;
public GenericTree() { // Constructor public GenericTree() { // Constructor
Scanner scn = new Scanner(System.in); Scanner scn = new Scanner(System.in);
root = create_treeG(null, 0, scn); root = create_treeG(null, 0, scn);
@ -44,7 +42,6 @@ public class GenericTree {
int number = scn.nextInt(); int number = scn.nextInt();
for (int i = 0; i < number; i++) { for (int i = 0; i < number; i++) {
Node child = create_treeG(node, i, scn); Node child = create_treeG(node, i, scn);
size++;
node.child.add(child); node.child.add(child);
} }
return node; return node;

View File

@ -30,11 +30,6 @@ public class TreeRandomNode {
int item; int item;
Node left, right; Node left, right;
public Node(int key) {
item = key;
left = right = null;
}
} }
// Using an arraylist to store the inorder traversal of the given binary tree // Using an arraylist to store the inorder traversal of the given binary tree

View File

@ -12,6 +12,7 @@ class Main {
int inputX0 = sc.nextInt(); int inputX0 = sc.nextInt();
int toPrint = nearestRightKey(root, inputX0); int toPrint = nearestRightKey(root, inputX0);
System.out.println("Key: " + toPrint); System.out.println("Key: " + toPrint);
sc.close();
} }
public static NRKTree BuildTree() { public static NRKTree BuildTree() {

View File

@ -1,7 +1,5 @@
package com.thealgorithms.dynamicprogramming; package com.thealgorithms.dynamicprogramming;
import java.util.Scanner;
/** /**
* Given a string containing just the characters '(' and ')', find the length of * Given a string containing just the characters '(' and ')', find the length of
* the longest valid (well-formed) parentheses substring. * the longest valid (well-formed) parentheses substring.

View File

@ -51,5 +51,6 @@ public class DeterminantOfMatrix {
} }
} }
System.out.println(determinant(a, n)); System.out.println(determinant(a, n));
in.close();
} }
} }

View File

@ -6,8 +6,6 @@
*/ */
package com.thealgorithms.maths; package com.thealgorithms.maths;
import java.io.*;
public class DudeneyNumber { public class DudeneyNumber {
// returns True if the number is a Dudeney number and False if it is not a Dudeney number. // returns True if the number is a Dudeney number and False if it is not a Dudeney number.

View File

@ -49,5 +49,6 @@ class KeithNumber {
} else { } else {
System.out.println("No, the given number is not a Keith number."); System.out.println("No, the given number is not a Keith number.");
} }
in.close();
} }
} }

View File

@ -21,6 +21,7 @@ public class LeastCommonMultiple {
System.out.println("Please enter second number >> "); System.out.println("Please enter second number >> ");
int num2 = input.nextInt(); int num2 = input.nextInt();
System.out.println("The least common multiple of two numbers is >> " + lcm(num1, num2)); System.out.println("The least common multiple of two numbers is >> " + lcm(num1, num2));
input.close();
} }
/* /*

View File

@ -45,5 +45,6 @@ public class MagicSquare {
} }
System.out.println(); System.out.println();
} }
sc.close();
} }
} }

View File

@ -50,6 +50,7 @@ public class NonRepeatingElement {
} }
System.out.println("The two non repeating elements are " + num1 + " and " + num2); System.out.println("The two non repeating elements are " + num1 + " and " + num2);
sc.close();
} }
/* /*
Explanation of the code: Explanation of the code:

View File

@ -1,7 +1,5 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
import java.util.Scanner;
/* /*
*To learn about the method, visit the link below : *To learn about the method, visit the link below :
* https://en.wikipedia.org/wiki/Newton%27s_method * https://en.wikipedia.org/wiki/Newton%27s_method

View File

@ -11,9 +11,6 @@ public class MinimizingLateness {
int t = 0; // Time required for the operation to be performed int t = 0; // Time required for the operation to be performed
int d = 0; // Time the job should be completed int d = 0; // Time the job should be completed
int s = 0; // Start time of the task
int f = 0; // End time of the operation
public Schedule(int t, int d) { public Schedule(int t, int d) {
this.t = t; this.t = t;
this.d = d; this.d = d;
@ -46,8 +43,6 @@ public class MinimizingLateness {
int tryTime = 0; // Total time worked int tryTime = 0; // Total time worked
int lateness = 0; // Lateness int lateness = 0; // Lateness
for (int j = 0; j < indexCount - 1; j++) { for (int j = 0; j < indexCount - 1; j++) {
array[j].s = tryTime; // Start time of the task
array[j].f = tryTime + array[j].t; // Time finished
tryTime = tryTime + array[j].t; // Add total work time tryTime = tryTime + array[j].t; // Add total work time
// Lateness // Lateness
lateness = lateness + Math.max(0, tryTime - array[j].d); lateness = lateness + Math.max(0, tryTime - array[j].d);

View File

@ -21,6 +21,7 @@ public class Sort012D {
a[i] = np.nextInt(); a[i] = np.nextInt();
} }
sort012(a); sort012(a);
np.close();
} }
public static void sort012(int[] a) { public static void sort012(int[] a) {

View File

@ -49,5 +49,6 @@ class Sparcity {
} }
} }
System.out.println("Sparcity of matrix is: " + sparcity(mat)); System.out.println("Sparcity of matrix is: " + sparcity(mat));
in.close();
} }
} }

View File

@ -19,6 +19,7 @@ public class ThreeSumProblem {
System.out.println("Brute Force Approach\n" + (th.BruteForce(arr, ts)) + "\n"); System.out.println("Brute Force Approach\n" + (th.BruteForce(arr, ts)) + "\n");
System.out.println("Two Pointer Approach\n" + (th.TwoPointer(arr, ts)) + "\n"); System.out.println("Two Pointer Approach\n" + (th.TwoPointer(arr, ts)) + "\n");
System.out.println("Hashmap Approach\n" + (th.Hashmap(arr, ts))); System.out.println("Hashmap Approach\n" + (th.Hashmap(arr, ts)));
scan.close();
} }
public List<List<Integer>> BruteForce(int[] nums, int target) { public List<List<Integer>> BruteForce(int[] nums, int target) {

View File

@ -44,5 +44,6 @@ public class BoyerMoore {
a[i] = input.nextInt(); a[i] = input.nextInt();
} }
System.out.println("the majority element is " + findmajor(a)); System.out.println("the majority element is " + findmajor(a));
input.close();
} }
} }

View File

@ -1,7 +1,5 @@
package com.thealgorithms.others; package com.thealgorithms.others;
import java.util.Scanner;
/** /**
* @author Marcus * @author Marcus
*/ */

View File

@ -19,6 +19,7 @@ public class HappyNumbersSeq {
} }
String res = n == 1 ? "1 Happy number" : "Sad number"; String res = n == 1 ? "1 Happy number" : "Sad number";
System.out.println(res); System.out.println(res);
in.close();
} }
private static int sumSquares(int n) { private static int sumSquares(int n) {

View File

@ -118,5 +118,6 @@ public class Huffman {
// print the codes by traversing the tree // print the codes by traversing the tree
printCode(root, ""); printCode(root, "");
s.close();
} }
} }

View File

@ -22,8 +22,7 @@ class Rotate_by_90_degree {
} }
} }
Rotate g = new Rotate(); Rotate.rotate(arr);
g.rotate(arr);
printMatrix(arr); printMatrix(arr);
} }
sc.close(); sc.close();

View File

@ -1,8 +1,5 @@
package com.thealgorithms.others.cn; package com.thealgorithms.others.cn;
import java.util.ArrayList;
import java.util.List;
final public class HammingDistance { final public class HammingDistance {
private HammingDistance() { private HammingDistance() {
} }

View File

@ -33,6 +33,7 @@ public class LinearSearchThread {
} }
boolean found = t.getResult() || t1.getResult() || t2.getResult() || t3.getResult(); boolean found = t.getResult() || t1.getResult() || t2.getResult() || t3.getResult();
System.out.println("Found = " + found); System.out.println("Found = " + found);
in.close();
} }
} }

View File

@ -21,9 +21,8 @@ class PerfectBinarySearch {
} }
public static void main(String[] args) { public static void main(String[] args) {
PerfectBinarySearch BinarySearch = new PerfectBinarySearch();
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assert BinarySearch.binarySearch(array, -1) == -1; assert PerfectBinarySearch.binarySearch(array, -1) == -1;
assert BinarySearch.binarySearch(array, 11) == -1; assert PerfectBinarySearch.binarySearch(array, 11) == -1;
} }
} }

View File

@ -1,7 +1,5 @@
package com.thealgorithms.searches; package com.thealgorithms.searches;
import java.util.Arrays;
public class SearchInARowAndColWiseSortedMatrix { public class SearchInARowAndColWiseSortedMatrix {
/** /**
* Search a key in row and column wise sorted matrix * Search a key in row and column wise sorted matrix

View File

@ -27,6 +27,7 @@ public class SquareRootBinarySearch {
int num = sc.nextInt(); int num = sc.nextInt();
long ans = squareRoot(num); long ans = squareRoot(num);
System.out.println("The square root is : " + ans); System.out.println("The square root is : " + ans);
sc.close();
} }
/** /**

View File

@ -1,5 +1,4 @@
package com.thealgorithms.searches; package com.thealgorithms.searches;
import java.util.*;
public class sortOrderAgnosticBinarySearch { public class sortOrderAgnosticBinarySearch {
public static int find(int[] arr, int key) { public static int find(int[] arr, int key) {
int start = 0; int start = 0;

View File

@ -66,5 +66,6 @@ public class MergeSortNoExtraSpace {
for (int i = 0; i < a.length; i++) { for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " "); System.out.print(a[i] + " ");
} }
inp.close();
} }
} }

View File

@ -33,22 +33,6 @@ public class TopologicalSort {
* */ * */
public final String label; public final String label;
/*
* Weight of vertex
* (more accurately defined as the time that a vertex has begun a visit in DFS)
* */
public int weight;
/*
* The time that the vertex has finished a visit in DFS
* */
public int finished;
/*
* π parent of the vertex
* */
public Vertex predecessor;
/* /*
* Represents the category of visit in DFS * Represents the category of visit in DFS
* */ * */
@ -90,11 +74,6 @@ public class TopologicalSort {
} }
} }
/*
* Time variable in DFS
* */
private static int time;
/* /*
* Depth First Search * Depth First Search
* *
@ -135,12 +114,9 @@ public class TopologicalSort {
* u.f = time * u.f = time
* */ * */
private static String sort(Graph graph, Vertex u, LinkedList<String> list) { private static String sort(Graph graph, Vertex u, LinkedList<String> list) {
time++;
u.weight = time;
u.color = Color.GRAY; u.color = Color.GRAY;
graph.adj.get(u.label).next.forEach(label -> { graph.adj.get(u.label).next.forEach(label -> {
if (graph.adj.get(label).color == Color.WHITE) { if (graph.adj.get(label).color == Color.WHITE) {
graph.adj.get(label).predecessor = u;
list.addFirst(sort(graph, graph.adj.get(label), list)); list.addFirst(sort(graph, graph.adj.get(label), list));
} else if (graph.adj.get(label).color == Color.GRAY) { } else if (graph.adj.get(label).color == Color.GRAY) {
/* /*
@ -153,8 +129,6 @@ public class TopologicalSort {
} }
}); });
u.color = Color.BLACK; u.color = Color.BLACK;
time++;
u.finished = time;
return u.label; return u.label;
} }
} }

View File

@ -12,6 +12,7 @@ class LongestPalindromicSubstring {
System.out.print("Enter the string: "); System.out.print("Enter the string: ");
str = sc.nextLine(); str = sc.nextLine();
System.out.println("Longest substring is : " + s.longestPalindrome(str)); System.out.println("Longest substring is : " + s.longestPalindrome(str));
sc.close();
} }
} }

View File

@ -1,6 +1,5 @@
package com.thealgorithms.strings; package com.thealgorithms.strings;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;

View File

@ -3,7 +3,6 @@ package com.thealgorithms.datastructures.hashmap;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import com.thealgorithms.datastructures.hashmap.hashing.HashMapCuckooHashing; import com.thealgorithms.datastructures.hashmap.hashing.HashMapCuckooHashing;
import java.util.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class HashMapCuckooHashingTest { class HashMapCuckooHashingTest {
@ -51,7 +50,6 @@ class HashMapCuckooHashingTest {
@Test @Test
void removeNone() { void removeNone() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10); HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
int initialSize = hashTable.getNumberOfKeysInTable();
try { try {
hashTable.deleteKeyFromHashTable(3); hashTable.deleteKeyFromHashTable(3);
} catch (Exception e) { } catch (Exception e) {
@ -90,11 +88,4 @@ class HashMapCuckooHashingTest {
assertTrue(hashTable.checkTableContainsKey(10)); assertTrue(hashTable.checkTableContainsKey(10));
assertTrue(hashTable.checkTableContainsKey(100)); assertTrue(hashTable.checkTableContainsKey(100));
} }
private HashMapCuckooHashing createHashMapCuckooHashing() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
int[] values = {11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222};
Arrays.stream(values).forEach(hashTable::insertKey2HashTable);
return hashTable;
}
} }

View File

@ -2,7 +2,6 @@ package com.thealgorithms.datastructures.hashmap.hashing;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import com.thealgorithms.datastructures.hashmap.hashing.MajorityElement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;

View File

@ -3,9 +3,7 @@ package com.thealgorithms.io;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class BufferedReaderTest { class BufferedReaderTest {

View File

@ -8,7 +8,6 @@ public class FactorialTest {
@Test @Test
public void test() { public void test() {
Factorial fact = new Factorial(); assertEquals(120, Factorial.factorial(5));
assertEquals(120, fact.factorial(5));
} }
} }

View File

@ -10,22 +10,22 @@ class SumOfDigitsTest {
@Test @Test
void testZero() { void testZero() {
assertEquals(0, SoD.sumOfDigits(0)); assertEquals(0, SumOfDigits.sumOfDigits(0));
assertEquals(0, SoD.sumOfDigitsRecursion(0)); assertEquals(0, SumOfDigits.sumOfDigitsRecursion(0));
assertEquals(0, SoD.sumOfDigitsFast(0)); assertEquals(0, SumOfDigits.sumOfDigitsFast(0));
} }
@Test @Test
void testPositive() { void testPositive() {
assertEquals(15, SoD.sumOfDigits(12345)); assertEquals(15, SumOfDigits.sumOfDigits(12345));
assertEquals(15, SoD.sumOfDigitsRecursion(12345)); assertEquals(15, SumOfDigits.sumOfDigitsRecursion(12345));
assertEquals(15, SoD.sumOfDigitsFast(12345)); assertEquals(15, SumOfDigits.sumOfDigitsFast(12345));
} }
@Test @Test
void testNegative() { void testNegative() {
assertEquals(6, SoD.sumOfDigits(-123)); assertEquals(6, SumOfDigits.sumOfDigits(-123));
assertEquals(6, SoD.sumOfDigitsRecursion(-123)); assertEquals(6, SumOfDigits.sumOfDigitsRecursion(-123));
assertEquals(6, SoD.sumOfDigitsFast(-123)); assertEquals(6, SumOfDigits.sumOfDigitsFast(-123));
} }
} }

View File

@ -14,7 +14,7 @@ class CRC16Test {
String textToCRC16 = "hacktoberfest!"; String textToCRC16 = "hacktoberfest!";
// when // when
String resultCRC16 = crc.crc16(textToCRC16); // Algorithm CRC16-CCITT-FALSE String resultCRC16 = CRC16.crc16(textToCRC16); // Algorithm CRC16-CCITT-FALSE
// then // then
assertEquals("10FC", resultCRC16); assertEquals("10FC", resultCRC16);

View File

@ -1,8 +1,6 @@
package com.thealgorithms.others.cn; package com.thealgorithms.others.cn;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.assertj.core.api.ThrowableTypeAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class HammingDistanceTest { public class HammingDistanceTest {
@ -42,14 +40,14 @@ public class HammingDistanceTest {
@Test @Test
public void mismatchDataBits() { public void mismatchDataBits() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("100010", "00011"); }); Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("100010", "00011"); });
Assertions.assertThat(ex.getMessage()).contains("must have the same length"); Assertions.assertThat(ex.getMessage()).contains("must have the same length");
} }
@Test @Test
public void mismatchDataBits2() { public void mismatchDataBits2() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("1", "11"); }); Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1", "11"); });
Assertions.assertThat(ex.getMessage()).contains("must have the same length"); Assertions.assertThat(ex.getMessage()).contains("must have the same length");
} }
@ -77,7 +75,7 @@ public class HammingDistanceTest {
@Test @Test
public void computeThrowsExceptionWhenInputsAreNotBitStrs() { public void computeThrowsExceptionWhenInputsAreNotBitStrs() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("1A", "11"); }); Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1A", "11"); });
Assertions.assertThat(ex.getMessage()).contains("must be a binary string"); Assertions.assertThat(ex.getMessage()).contains("must be a binary string");
} }

View File

@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.*; import java.util.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class BinarySearch2dArrayTest { public class BinarySearch2dArrayTest {
@ -16,7 +15,6 @@ public class BinarySearch2dArrayTest {
int target = 6; int target = 6;
int[] ans = BinarySearch2dArray.BinarySearch(arr, target); int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
int[] expected = {1, 1};
System.out.println(Arrays.toString(ans)); System.out.println(Arrays.toString(ans));
assertEquals(1, ans[0]); assertEquals(1, ans[0]);
assertEquals(1, ans[1]); assertEquals(1, ans[1]);
@ -29,7 +27,6 @@ public class BinarySearch2dArrayTest {
int target = 8; int target = 8;
int[] ans = BinarySearch2dArray.BinarySearch(arr, target); int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
int[] expected = {1, 3};
System.out.println(Arrays.toString(ans)); System.out.println(Arrays.toString(ans));
assertEquals(1, ans[0]); assertEquals(1, ans[0]);
assertEquals(3, ans[1]); assertEquals(3, ans[1]);
@ -42,7 +39,6 @@ public class BinarySearch2dArrayTest {
int target = 2; int target = 2;
int[] ans = BinarySearch2dArray.BinarySearch(arr, target); int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
int[] expected = {0, 1};
System.out.println(Arrays.toString(ans)); System.out.println(Arrays.toString(ans));
assertEquals(0, ans[0]); assertEquals(0, ans[0]);
assertEquals(1, ans[1]); assertEquals(1, ans[1]);
@ -55,7 +51,6 @@ public class BinarySearch2dArrayTest {
int target = 1; int target = 1;
int[] ans = BinarySearch2dArray.BinarySearch(arr, target); int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
int[] expected = {0, 0};
System.out.println(Arrays.toString(ans)); System.out.println(Arrays.toString(ans));
assertEquals(0, ans[0]); assertEquals(0, ans[0]);
assertEquals(0, ans[1]); assertEquals(0, ans[1]);
@ -68,7 +63,6 @@ public class BinarySearch2dArrayTest {
int target = 10; int target = 10;
int[] ans = BinarySearch2dArray.BinarySearch(arr, target); int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
int[] expected = {2, 1};
System.out.println(Arrays.toString(ans)); System.out.println(Arrays.toString(ans));
assertEquals(2, ans[0]); assertEquals(2, ans[0]);
assertEquals(1, ans[1]); assertEquals(1, ans[1]);
@ -81,7 +75,6 @@ public class BinarySearch2dArrayTest {
int target = 11; int target = 11;
int[] ans = BinarySearch2dArray.BinarySearch(arr, target); int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
int[] expected = {2, 2};
System.out.println(Arrays.toString(ans)); System.out.println(Arrays.toString(ans));
assertEquals(2, ans[0]); assertEquals(2, ans[0]);
assertEquals(2, ans[1]); assertEquals(2, ans[1]);
@ -94,7 +87,6 @@ public class BinarySearch2dArrayTest {
int target = 101; int target = 101;
int[] ans = BinarySearch2dArray.BinarySearch(arr, target); int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
int[] expected = {-1, -1};
System.out.println(Arrays.toString(ans)); System.out.println(Arrays.toString(ans));
assertEquals(-1, ans[0]); assertEquals(-1, ans[0]);
assertEquals(-1, ans[1]); assertEquals(-1, ans[1]);

View File

@ -2,8 +2,6 @@ package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import com.thealgorithms.searches.OrderAgnosticBinarySearch;
import java.util.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class OrderAgnosticBinarySearchTest { public class OrderAgnosticBinarySearchTest {

View File

@ -2,7 +2,6 @@ package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

View File

@ -10,12 +10,10 @@ import org.junit.jupiter.api.Test;
*/ */
public class OddEvenSortTest { public class OddEvenSortTest {
private OddEvenSort oddEvenSort = new OddEvenSort();
@Test @Test
public void oddEvenSortEmptyArray() { public void oddEvenSortEmptyArray() {
int[] inputArray = {}; int[] inputArray = {};
oddEvenSort.oddEvenSort(inputArray); OddEvenSort.oddEvenSort(inputArray);
int[] expectedOutput = {}; int[] expectedOutput = {};
assertArrayEquals(inputArray, expectedOutput); assertArrayEquals(inputArray, expectedOutput);
} }
@ -23,7 +21,7 @@ public class OddEvenSortTest {
@Test @Test
public void oddEvenSortNaturalNumberArray() { public void oddEvenSortNaturalNumberArray() {
int[] inputArray = {18, 91, 86, 60, 21, 44, 37, 78, 98, 67}; int[] inputArray = {18, 91, 86, 60, 21, 44, 37, 78, 98, 67};
oddEvenSort.oddEvenSort(inputArray); OddEvenSort.oddEvenSort(inputArray);
int[] expectedOutput = {18, 21, 37, 44, 60, 67, 78, 86, 91, 98}; int[] expectedOutput = {18, 21, 37, 44, 60, 67, 78, 86, 91, 98};
assertArrayEquals(inputArray, expectedOutput); assertArrayEquals(inputArray, expectedOutput);
} }
@ -31,7 +29,7 @@ public class OddEvenSortTest {
@Test @Test
public void oddEvenSortIntegerArray() { public void oddEvenSortIntegerArray() {
int[] inputArray = {57, 69, -45, 12, -85, 3, -76, 36, 67, -14}; int[] inputArray = {57, 69, -45, 12, -85, 3, -76, 36, 67, -14};
oddEvenSort.oddEvenSort(inputArray); OddEvenSort.oddEvenSort(inputArray);
int[] expectedOutput = {-85, -76, -45, -14, 3, 12, 36, 57, 67, 69}; int[] expectedOutput = {-85, -76, -45, -14, 3, 12, 36, 57, 67, 69};
assertArrayEquals(inputArray, expectedOutput); assertArrayEquals(inputArray, expectedOutput);
} }

View File

@ -3,9 +3,7 @@ package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public abstract class SortingAlgorithmTest { public abstract class SortingAlgorithmTest {

View File

@ -1,10 +1,5 @@
package com.thealgorithms.sorts; package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class TimSortTest extends SortingAlgorithmTest { class TimSortTest extends SortingAlgorithmTest {
@Override @Override
SortAlgorithm getSortAlgorithm() { SortAlgorithm getSortAlgorithm() {

View File

@ -2,7 +2,6 @@ package com.thealgorithms.strings;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class IsomorphicTest { public class IsomorphicTest {
@ -21,11 +20,9 @@ public class IsomorphicTest {
String str7 = "aaammmnnn"; String str7 = "aaammmnnn";
String str8 = "ggghhhbbj"; String str8 = "ggghhhbbj";
Isomorphic isomorphic = new Isomorphic(); assertTrue(Isomorphic.checkStrings(str1, str2));
assertTrue(Isomorphic.checkStrings(str3, str4));
assertTrue(isomorphic.checkStrings(str1, str2)); assertFalse(Isomorphic.checkStrings(str5, str6));
assertTrue(isomorphic.checkStrings(str3, str4)); assertFalse(Isomorphic.checkStrings(str7, str8));
assertFalse(isomorphic.checkStrings(str5, str6));
assertFalse(isomorphic.checkStrings(str7, str8));
} }
} }

View File

@ -9,29 +9,28 @@ public class LetterCombinationsOfPhoneNumberTest {
@Test @Test
public void letterCombinationsOfPhoneNumber() { public void letterCombinationsOfPhoneNumber() {
LetterCombinationsOfPhoneNumber ob = new LetterCombinationsOfPhoneNumber(); LetterCombinationsOfPhoneNumber.generateNumberToCharMap();
ob.generateNumberToCharMap();
// ** Test 1 ** // ** Test 1 **
// Input: digits = "" // Input: digits = ""
// Output: [] // Output: []
int[] numbers1 = {}; int[] numbers1 = {};
List<String> output1 = Arrays.asList(""); List<String> output1 = Arrays.asList("");
assertTrue(ob.printWords(numbers1, numbers1.length, 0, "").equals(output1)); assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers1, numbers1.length, 0, "").equals(output1));
// ** Test 2 ** // ** Test 2 **
// Input: digits = "2" // Input: digits = "2"
// Output: ["a","b","c"] // Output: ["a","b","c"]
int[] numbers2 = {2}; int[] numbers2 = {2};
List<String> output2 = Arrays.asList("a", "b", "c"); List<String> output2 = Arrays.asList("a", "b", "c");
assertTrue(ob.printWords(numbers2, numbers2.length, 0, "").equals(output2)); assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers2, numbers2.length, 0, "").equals(output2));
// ** Test 3 ** // ** Test 3 **
// Input: digits = "23" // Input: digits = "23"
// Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
int[] numbers3 = {2, 3}; int[] numbers3 = {2, 3};
List<String> output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"); List<String> output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf");
assertTrue(ob.printWords(numbers3, numbers3.length, 0, "").equals(output3)); assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers3, numbers3.length, 0, "").equals(output3));
// ** Test 4 ** // ** Test 4 **
// Input: digits = "234" // Input: digits = "234"
@ -40,6 +39,6 @@ public class LetterCombinationsOfPhoneNumberTest {
// "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"] // "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"]
int[] numbers4 = {2, 3, 4}; int[] numbers4 = {2, 3, 4};
List<String> output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"); List<String> output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi");
assertTrue(ob.printWords(numbers4, numbers4.length, 0, "").equals(output4)); assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers4, numbers4.length, 0, "").equals(output4));
} }
} }

View File

@ -10,7 +10,7 @@ public class ReverseStringRecursiveTest {
@Test @Test
void shouldAcceptWhenEmptyStringIsPassed() { void shouldAcceptWhenEmptyStringIsPassed() {
String expected = ""; String expected = "";
String reversed = stringRecursive.reverse(""); String reversed = ReverseStringRecursive.reverse("");
assertEquals(expected, reversed); assertEquals(expected, reversed);
} }
@ -18,7 +18,7 @@ public class ReverseStringRecursiveTest {
@Test @Test
void shouldAcceptNotWhenWhenSingleCharacterIsPassed() { void shouldAcceptNotWhenWhenSingleCharacterIsPassed() {
String expected = "a"; String expected = "a";
String reversed = stringRecursive.reverse("a"); String reversed = ReverseStringRecursive.reverse("a");
assertEquals(expected, reversed); assertEquals(expected, reversed);
} }
@ -26,7 +26,7 @@ public class ReverseStringRecursiveTest {
@Test @Test
void shouldAcceptWhenStringIsPassed() { void shouldAcceptWhenStringIsPassed() {
String expected = "dlroWolleH"; String expected = "dlroWolleH";
String reversed = stringRecursive.reverse("HelloWorld"); String reversed = ReverseStringRecursive.reverse("HelloWorld");
assertEquals(expected, reversed); assertEquals(expected, reversed);
} }