nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: TheAlgorithms/Java
Pull request #3684 (2022-10-26 18:28:02)

Add Test case for BinaryToDecimal
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


naanagon: @siriak , @yanglbme Test cases passed. Can you please take a look..?
naanagon: @siriak , @yanglbme - could you please review, build is green now.

Patch:
diff --git a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
index 95de43c5..de06ca6b 100644
--- a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
+++ b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
@@ -7,6 +7,17 @@ import java.util.Scanner;
  */
 class BinaryToDecimal {
 
+    public static int binaryToDecimal(int binNum) {
+        int binCopy, d, s = 0, power = 0;
+        binCopy = binNum;
+        while (binCopy != 0) {
+            d = binCopy % 10;
+            s += d * (int) Math.pow(2, power++);
+            binCopy /= 10;
+        }
+        return s;
+    }
+
     /**
      * Main Method
      *
@@ -14,16 +25,8 @@ class BinaryToDecimal {
      */
     public static void main(String args[]) {
         Scanner sc = new Scanner(System.in);
-        int binNum, binCopy, d, s = 0, power = 0;
         System.out.print("Binary number: ");
-        binNum = sc.nextInt();
-        binCopy = binNum;
-        while (binCopy != 0) {
-            d = binCopy % 10;
-            s += d * (int) Math.pow(2, power++);
-            binCopy /= 10;
-        }
-        System.out.println("Decimal equivalent:" + s);
+        System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextInt()));
         sc.close();
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java b/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java
new file mode 100644
index 00000000..d44c30d1
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java
@@ -0,0 +1,18 @@
+package com.thealgorithms.conversions;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class BinaryToDecimalTest {
+
+    @Test
+    public void testBinaryToDecimal() {
+        //zeros at the starting should be removed
+        assertEquals(0, BinaryToDecimal.binaryToDecimal(0));
+        assertEquals(1, BinaryToDecimal.binaryToDecimal(1));
+        assertEquals(5, BinaryToDecimal.binaryToDecimal(101));
+        assertEquals(63, BinaryToDecimal.binaryToDecimal(111111));
+        assertEquals(512, BinaryToDecimal.binaryToDecimal(1000000000));
+    }
+}
\ No newline at end of file
Repository: TheAlgorithms/Java
Pull request #3687 (2022-10-26 19:02:25)

Add Test case for EggDropping
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


naanagon: @siriak , @yanglbme Test cases passed. Can you please take a look..?
naanagon: @siriak , @yanglbme - could you please review, build is green now.

Patch:
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java
index 2bb782d2..efceb449 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/EggDropping.java
@@ -6,7 +6,7 @@ package com.thealgorithms.dynamicprogramming;
 public class EggDropping {
 
     // min trials with n eggs and m floors
-    private static int minTrials(int n, int m) {
+    public static int minTrials(int n, int m) {
         int[][] eggFloor = new int[n + 1][m + 1];
         int result, x;
 


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java
new file mode 100644
index 00000000..7d13caab
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java
@@ -0,0 +1,28 @@
+package com.thealgorithms.dynamicprogramming;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class EggDroppingTest {
+
+    @Test
+    void hasMultipleEggSingleFloor(){
+        assertEquals(1,EggDropping.minTrials(3,1));
+    }
+
+    @Test
+    void hasSingleEggSingleFloor(){
+        assertEquals(1,EggDropping.minTrials(1,1));
+    }
+
+    @Test
+    void hasSingleEggMultipleFloor(){
+        assertEquals(3,EggDropping.minTrials(1,3));
+    }
+
+    @Test
+    void hasMultipleEggMultipleFloor(){
+        assertEquals(7,EggDropping.minTrials(100,101));
+    }
+}
Repository: TheAlgorithms/Java
Pull request #3733 (2022-10-29 09:48:39)

Create CRC16.java
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/others/CRC16.java b/src/main/java/com/thealgorithms/others/CRC16.java
new file mode 100644
index 00000000..80dbbcb9
--- /dev/null
+++ b/src/main/java/com/thealgorithms/others/CRC16.java
@@ -0,0 +1,29 @@
+package com.thealgorithms.others;
+
+/**
+ * Generates a crc16 checksum for a given string 
+ */
+public class CRC16 {
+
+    public static void main(String[] args) {
+        System.out.println(crc16("Hello World!"));
+    }
+
+	public static String crc16(String message) {
+		int crc = 0xFFFF; // initial value
+		int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
+		byte[] bytes = message.getBytes();
+
+		for (byte b : bytes) {
+			for (int i = 0; i < 8; i++) {
+				boolean bit = ((b >> (7 - i) & 1) == 1);
+				boolean c15 = ((crc >> 15 & 1) == 1);
+				crc <<= 1;
+				if (c15 ^ bit)
+					crc ^= polynomial;
+			}
+		}
+		crc &= 0xffff;
+		return Integer.toHexString(crc).toUpperCase();
+	}
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/CRC16Test.java b/src/test/java/com/thealgorithms/others/CRC16Test.java
new file mode 100644
index 00000000..057d5b7d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/CRC16Test.java
@@ -0,0 +1,23 @@
+package com.thealgorithms.others;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class CRC16Test {
+
+    CRC16 crc = new CRC16();
+
+    @Test
+    void testCRC16() {
+        // given
+        String textToCRC16 = "hacktoberfest!";
+
+        // when
+        String resultCRC16 = crc.crc16(textToCRC16); // Algorithm CRC16-CCITT-FALSE
+
+        // then
+        assertEquals("10FC", resultCRC16);
+    }
+
+}
Repository: TheAlgorithms/Java
Pull request #3765 (2022-11-11 07:52:19)

Added function in AliquotSum.java
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/AliquotSum.java b/src/main/java/com/thealgorithms/maths/AliquotSum.java
index 3525c368..6eb18f26 100644
--- a/src/main/java/com/thealgorithms/maths/AliquotSum.java
+++ b/src/main/java/com/thealgorithms/maths/AliquotSum.java
@@ -30,4 +30,35 @@ public class AliquotSum {
 
         return sumWrapper.value;
     }
+    
+     /**
+     * Function to calculate the aliquot sum of an integer number
+     *
+     * @param n a positive integer
+     * @return aliquot sum of given {@code number}
+     */
+    public static int getAliquotSum(int n) {
+        if (n <= 0)
+            return -1;
+        int sum = 1;
+        double root = Math.sqrt(n);
+        /*
+         * We can get the factors after the root by dividing number by its factors
+         * before the root.
+         * Ex- Factors of 100 are 1, 2, 4, 5, 10, 20, 25, 50 and 100.
+         * Root of 100 is 10. So factors before 10 are 1, 2, 4 and 5.
+         * Now by dividing 100 by each factor before 10 we get:
+         * 100/1 = 100, 100/2 = 50, 100/4 = 25 and 100/5 = 20
+         * So we get 100, 50, 25 and 20 which are factors of 100 after 10
+         */
+        for (int i = 2; i <= root; i++) {
+            if (n % i == 0) {
+                sum += i + n / i;
+            }
+        }
+        // if n is a perfect square then its root was added twice in above loop, so subtracting root from sum
+        if (root == (int) root)
+            sum -= root;
+        return sum;
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/AliquotSumTest.java b/src/test/java/com/thealgorithms/maths/AliquotSumTest.java
index d3fef8d3..94631637 100644
--- a/src/test/java/com/thealgorithms/maths/AliquotSumTest.java
+++ b/src/test/java/com/thealgorithms/maths/AliquotSumTest.java
@@ -12,5 +12,9 @@ public class AliquotSumTest {
         assertEquals(6, AliquotSum.getAliquotValue(6));
         assertEquals(9, AliquotSum.getAliquotValue(15));
         assertEquals(1, AliquotSum.getAliquotValue(19));
+        assertEquals(0, AliquotSum.getAliquotSum(1));
+        assertEquals(6, AliquotSum.getAliquotSum(6));
+        assertEquals(9, AliquotSum.getAliquotSum(15));
+        assertEquals(1, AliquotSum.getAliquotSum(19));
     }
 }
Repository: TheAlgorithms/Java
Pull request #3882 (2023-02-17 07:01:12)

added agnosticBinarySearch algorithm and  Testcases for ascending and descending order
<!-- For completed items, change [ ] to [x] -->

- [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [ ] All filenames are in PascalCase.
- [ ] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


HumbleFool830: @siriak can you check this once is there any modifications let me know !

Patch:
diff --git a/src/main/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearch.java b/src/main/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearch.java
new file mode 100644
index 00000000..9f60b31b
--- /dev/null
+++ b/src/main/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearch.java
@@ -0,0 +1,32 @@
+package com.thealgorithms.searches;
+import java.util.*;
+public class sortOrderAgnosticBinarySearch {
+    public static int find(int arr[],int key){
+        int start = 0;
+        int end = arr.length-1;
+        boolean arrDescending = arr[start]>arr[end];  //checking for Array is in ascending order or descending order.
+        while(start<=end){
+            int mid = end-start/2;
+            if (arr[mid]==key){
+                return mid;
+            }
+            if(arrDescending){       // boolean is true then our array is in descending order 
+                if(key<arr[mid]){
+                    start=mid+1;
+                }
+                else{
+                    end=mid-1;
+                }
+            }
+            else {         // otherwise our array is in ascending order 
+                if(key>arr[mid]){
+                   start=mid+1;
+               }
+               else{
+                   end=mid-1;
+               }
+            }
+        }
+        return -1;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearchTest.java
new file mode 100644
index 00000000..37f1aa40
--- /dev/null
+++ b/src/test/java/com/thealgorithms/searches/sortOrderAgnosticBinarySearchTest.java
@@ -0,0 +1,27 @@
+package com.thealgorithms.searches;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class sortOrderAgnosticBinarySearchTest{
+
+    @Test
+    public void testAscending(){
+        int arr[] = {1,2,3,4,5};// for ascending order.
+        int target = 2;
+        int ans=sortOrderAgnosticBinarySearch.find(arr, target);
+        int excepted = 1;
+        assertEquals(excepted,ans);
+    }
+
+    @Test
+    public void testDescending(){
+        int arr[] = {5,4,3,2,1};// for descending order.
+        int target = 2;
+        int ans=sortOrderAgnosticBinarySearch.find(arr, target);
+        int excepted = 3;
+        assertEquals(excepted,ans );
+    }
+
+}
\ No newline at end of file
Repository: TheAlgorithms/Java
Pull request #4202 (2023-05-27 21:55:34)

Octal To Binary Conerter
<!-- For completed items, change [ ] to [x] -->

- [X] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [X] This pull request is all my own work -- I have not plagiarized it.
- [X] All filenames are in PascalCase.
- [X] All functions and variable names follow Java naming conventions.
- [X] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

**octal To Binary Converter in Java.**


BamaCharanChhandogi: Ok, I have updated the code and added test cases.

Patch:
diff --git a/src/main/java/com/thealgorithms/conversions/OctalToBinary.java b/src/main/java/com/thealgorithms/conversions/OctalToBinary.java
new file mode 100644
index 00000000..711381d8
--- /dev/null
+++ b/src/main/java/com/thealgorithms/conversions/OctalToBinary.java
@@ -0,0 +1,42 @@
+package com.thealgorithms.conversions;
+import java.util.Scanner;
+
+/**
+ * Converts any Octal Number to a Binary Number
+ *
+ * @author Bama Charan Chhandogi
+ */
+
+public class OctalToBinary {
+    public static long convertOctalToBinary(int octalNumber) {
+        long binaryNumber = 0;
+        int digitPosition = 1;
+
+        while (octalNumber != 0) {
+            int octalDigit = octalNumber % 10;
+            long binaryDigit = convertOctalDigitToBinary(octalDigit);
+
+            binaryNumber += binaryDigit * digitPosition;
+
+            octalNumber /= 10;
+            digitPosition *= 1000; // Move to the next group of 3 binary digits
+        }
+
+        return binaryNumber;
+    }
+
+    public static long convertOctalDigitToBinary(int octalDigit) {
+        long binaryDigit = 0;
+        int binaryMultiplier = 1;
+
+        while (octalDigit != 0) {
+            int octalDigitRemainder = octalDigit % 2;
+            binaryDigit += octalDigitRemainder * binaryMultiplier;
+
+            octalDigit /= 2;
+            binaryMultiplier *= 10;
+        }
+
+        return binaryDigit;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java b/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java
new file mode 100644
index 00000000..6c7fe870
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java
@@ -0,0 +1,15 @@
+package com.thealgorithms.conversions;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+public class OctalToBinaryTest {
+    @Test
+    public void testConvertOctalToBinary() {
+        assertEquals(101, OctalToBinary.convertOctalToBinary(5));
+        assertEquals(1001, OctalToBinary.convertOctalToBinary(11));
+        assertEquals(101010, OctalToBinary.convertOctalToBinary(52));
+        assertEquals(110, OctalToBinary.convertOctalToBinary(6));
+    }
+}
Repository: TheAlgorithms/Java
Pull request #4262 (2023-07-26 08:01:13)

fix: handle inputs like `"2 +"` in `StackPostfixNotation`
After #4261 was merged, I have realized that the method [`StackPostfixNotation.postfixEvaluate`](https://github.com/vil02/Java/blob/44dcebb6996b39b5a1a67a8633f95b6e4c1bd0f0/src/main/java/com/thealgorithms/others/StackPostfixNotation.java#L11) still does not handle properly all of the inputs (e. g. `"2 +"` or `"+"`). This PR fixes that.


<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


albina-astr: @siriak Hi Andrii! It seems I don't have access to merge PRs. Can you help to merge this PR? Thanks!

Patch:
diff --git a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java
index c6d395cb..f8591510 100644
--- a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java
+++ b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java
@@ -16,6 +16,9 @@ public final class StackPostfixNotation {
             if (tokens.hasNextInt()) {
                 s.push(tokens.nextInt()); // If int then push to stack
             } else { // else pop top two values and perform the operation
+                if (s.size() < 2) {
+                    throw new IllegalArgumentException("exp is not a proper postfix expression (too few arguments).");
+                }
                 int num2 = s.pop();
                 int num1 = s.pop();
                 String op = tokens.next();


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java
index 4894b403..9256e2bc 100644
--- a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java
+++ b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java
@@ -30,4 +30,14 @@ public class StackPostfixNotationTest {
     public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() {
         assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !"));
     }
+
+    @Test
+    public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsA() {
+        assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("+"));
+    }
+
+    @Test
+    public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsB() {
+        assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("2 +"));
+    }
 }
Repository: TheAlgorithms/Java
Pull request #4266 (2023-07-27 14:58:01)

Factorial refactoring, added unit tests
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/Factorial.java b/src/main/java/com/thealgorithms/maths/Factorial.java
index 90fcde54..063f00bd 100644
--- a/src/main/java/com/thealgorithms/maths/Factorial.java
+++ b/src/main/java/com/thealgorithms/maths/Factorial.java
@@ -1,15 +1,6 @@
 package com.thealgorithms.maths;
 
 public class Factorial {
-
-    /* Driver Code */
-    public static void main(String[] args) {
-        assert factorial(0) == 1;
-        assert factorial(1) == 1;
-        assert factorial(5) == 120;
-        assert factorial(10) == 3628800;
-    }
-
     /**
      * Calculate factorial N using iteration
      *
@@ -18,11 +9,12 @@ public class Factorial {
      */
     public static long factorial(int n) {
         if (n < 0) {
-            throw new IllegalArgumentException("number is negative");
+            throw new IllegalArgumentException("Input number cannot be negative");
         }
         long factorial = 1;
-        for (int i = 1; i <= n; factorial *= i, ++i)
-            ;
+        for (int i = 1; i <= n; ++i) {
+            factorial *= i;
+        }
         return factorial;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/FactorialTest.java b/src/test/java/com/thealgorithms/maths/FactorialTest.java
index 752cd7a7..b22ad535 100644
--- a/src/test/java/com/thealgorithms/maths/FactorialTest.java
+++ b/src/test/java/com/thealgorithms/maths/FactorialTest.java
@@ -5,9 +5,19 @@ import static org.junit.jupiter.api.Assertions.*;
 import org.junit.jupiter.api.Test;
 
 public class FactorialTest {
+    private static final String EXCEPTION_MESSAGE = "Input number cannot be negative";
 
     @Test
-    public void test() {
+    public void testWhenInvalidInoutProvidedShouldThrowException() {
+        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
+        assertEquals(exception.getMessage(), EXCEPTION_MESSAGE);
+    }
+
+    @Test
+    public void testCorrectFactorialCalculation() {
+        assertEquals(1, Factorial.factorial(0));
+        assertEquals(1, Factorial.factorial(1));
         assertEquals(120, Factorial.factorial(5));
+        assertEquals(3628800, Factorial.factorial(10));
     }
 }