instruction
stringclasses 7
values | input
stringclasses 1
value | output
stringlengths 204
833
| language
stringclasses 1
value | category
stringclasses 4
values | difficulty
stringclasses 2
values |
|---|---|---|---|---|---|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSortedHelper(List<Integer> input, List<Integer> nums) {
if (input == null || nums == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> str = new ArrayList<>(input.size() + nums.size());
int i = 0, j = 0;
while (i < input.size() && j < nums.size()) {
if (input.get(i) <= nums.get(j)) {
str.add(input.get(i++));
} else {
str.add(nums.get(j++));
}
}
while (i < input.size()) {
str.add(input.get(i++));
}
while (j < nums.size()) {
str.add(nums.get(j++));
}
return str;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafe(String n) {
try {
return Integer.parseInt(n);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + n);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafe_new(String str) {
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + str);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Write a Java method that counts the number of vowels in a string.
|
public class StringUtils {
public static int countVowels_new(String s) {
if (s == null) {
return 0;
}
int count = 0;
for (char c : s.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) {
count++;
}
}
return count;
}
}
|
java
|
function
|
easy
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int maxEx(int[] arr) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("arr must not be null or empty");
}
int max = arr[0];
for (int v : arr) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted(List<Integer> n, List<Integer> n) {
if (n == null || n == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> s = new ArrayList<>(n.size() + n.size());
int i = 0, j = 0;
while (i < n.size() && j < n.size()) {
if (n.get(i) <= n.get(j)) {
s.add(n.get(i++));
} else {
s.add(n.get(j++));
}
}
while (i < n.size()) {
s.add(n.get(i++));
}
while (j < n.size()) {
s.add(n.get(j++));
}
return s;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseStringHelper(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSortedEx(List<Integer> n, List<Integer> nums) {
if (n == null || nums == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> value = new ArrayList<>(n.size() + nums.size());
int i = 0, j = 0;
while (i < n.size() && j < nums.size()) {
if (n.get(i) <= nums.get(j)) {
value.add(n.get(i++));
} else {
value.add(nums.get(j++));
}
}
while (i < n.size()) {
value.add(n.get(i++));
}
while (j < nums.size()) {
value.add(nums.get(j++));
}
return value;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_new(List<Integer> s, List<Integer> arr) {
if (s == null || arr == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> s = new ArrayList<>(s.size() + arr.size());
int i = 0, j = 0;
while (i < s.size() && j < arr.size()) {
if (s.get(i) <= arr.get(j)) {
s.add(s.get(i++));
} else {
s.add(arr.get(j++));
}
}
while (i < s.size()) {
s.add(s.get(i++));
}
while (j < arr.size()) {
s.add(arr.get(j++));
}
return s;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_new(int nums, int list) {
if (list == 0) {
throw new IllegalArgumentException("list must not be zero");
}
return (double) nums / list;
}
}
|
java
|
bug-fix
|
easy
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int maxEx(int[] str) {
if (str == null || str.length == 0) {
throw new IllegalArgumentException("str must not be null or empty");
}
int max = str[0];
for (int v : str) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_alt(List<Integer> list, List<Integer> s) {
if (list == null || s == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> value = new ArrayList<>(list.size() + s.size());
int i = 0, j = 0;
while (i < list.size() && j < s.size()) {
if (list.get(i) <= s.get(j)) {
value.add(list.get(i++));
} else {
value.add(s.get(j++));
}
}
while (i < list.size()) {
value.add(list.get(i++));
}
while (j < s.size()) {
value.add(s.get(j++));
}
return value;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_alt(int n, int n) {
if (n == 0) {
throw new IllegalArgumentException("n must not be zero");
}
return (double) n / n;
}
}
|
java
|
bug-fix
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_new(int arr, int n) {
if (n == 0) {
throw new IllegalArgumentException("n must not be zero");
}
return (double) arr / n;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString(String n) {
if (n == null) {
return null;
}
return new StringBuilder(n).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafeHelper(String str) {
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + str);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int max_new(int[] input) {
if (input == null || input.length == 0) {
throw new IllegalArgumentException("input must not be null or empty");
}
int max = input[0];
for (int v : input) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that counts the number of vowels in a string.
|
public class StringUtils {
public static int countVowels_alt(String text) {
if (text == null) {
return 0;
}
int count = 0;
for (char c : text.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) {
count++;
}
}
return count;
}
}
|
java
|
function
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_alt(int arr, int s) {
if (s == 0) {
throw new IllegalArgumentException("s must not be zero");
}
return (double) arr / s;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrime_new(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int max_alt(int[] n) {
if (n == null || n.length == 0) {
throw new IllegalArgumentException("n must not be null or empty");
}
int max = n[0];
for (int v : n) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int max(int[] arr) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("arr must not be null or empty");
}
int max = arr[0];
for (int v : arr) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString_new(String nums) {
if (nums == null) {
return null;
}
return new StringBuilder(nums).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divideHelper(int value, int list) {
if (list == 0) {
throw new IllegalArgumentException("list must not be zero");
}
return (double) value / list;
}
}
|
java
|
bug-fix
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_alt(int nums, int str) {
if (str == 0) {
throw new IllegalArgumentException("str must not be zero");
}
return (double) nums / str;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafe_alt(String nums) {
try {
return Integer.parseInt(nums);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + nums);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int max(int[] input) {
if (input == null || input.length == 0) {
throw new IllegalArgumentException("input must not be null or empty");
}
int max = input[0];
for (int v : input) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseStringHelper(String input) {
if (input == null) {
return null;
}
return new StringBuilder(input).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSortedHelper(List<Integer> n, List<Integer> value) {
if (n == null || value == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> input = new ArrayList<>(n.size() + value.size());
int i = 0, j = 0;
while (i < n.size() && j < value.size()) {
if (n.get(i) <= value.get(j)) {
input.add(n.get(i++));
} else {
input.add(value.get(j++));
}
}
while (i < n.size()) {
input.add(n.get(i++));
}
while (j < value.size()) {
input.add(value.get(j++));
}
return input;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_alt(int s, int list) {
if (list == 0) {
throw new IllegalArgumentException("list must not be zero");
}
return (double) s / list;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString_new(String list) {
if (list == null) {
return null;
}
return new StringBuilder(list).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divideEx(int str, int nums) {
if (nums == 0) {
throw new IllegalArgumentException("nums must not be zero");
}
return (double) str / nums;
}
}
|
java
|
bug-fix
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divideEx(int nums, int n) {
if (n == 0) {
throw new IllegalArgumentException("n must not be zero");
}
return (double) nums / n;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseStringEx(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted(List<Integer> text, List<Integer> text) {
if (text == null || text == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> str = new ArrayList<>(text.size() + text.size());
int i = 0, j = 0;
while (i < text.size() && j < text.size()) {
if (text.get(i) <= text.get(j)) {
str.add(text.get(i++));
} else {
str.add(text.get(j++));
}
}
while (i < text.size()) {
str.add(text.get(i++));
}
while (j < text.size()) {
str.add(text.get(j++));
}
return str;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that counts the number of vowels in a string.
|
public class StringUtils {
public static int countVowelsHelper(String arr) {
if (arr == null) {
return 0;
}
int count = 0;
for (char c : arr.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) {
count++;
}
}
return count;
}
}
|
java
|
function
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divideHelper(int n, int input) {
if (input == 0) {
throw new IllegalArgumentException("input must not be zero");
}
return (double) n / input;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_alt(List<Integer> text, List<Integer> s) {
if (text == null || s == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> list = new ArrayList<>(text.size() + s.size());
int i = 0, j = 0;
while (i < text.size() && j < s.size()) {
if (text.get(i) <= s.get(j)) {
list.add(text.get(i++));
} else {
list.add(s.get(j++));
}
}
while (i < text.size()) {
list.add(text.get(i++));
}
while (j < s.size()) {
list.add(s.get(j++));
}
return list;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrimeEx(int nums) {
if (nums < 2) {
return false;
}
for (int i = 2; i * i <= nums; i++) {
if (nums % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_alt(List<Integer> text, List<Integer> s) {
if (text == null || s == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> arr = new ArrayList<>(text.size() + s.size());
int i = 0, j = 0;
while (i < text.size() && j < s.size()) {
if (text.get(i) <= s.get(j)) {
arr.add(text.get(i++));
} else {
arr.add(s.get(j++));
}
}
while (i < text.size()) {
arr.add(text.get(i++));
}
while (j < s.size()) {
arr.add(s.get(j++));
}
return arr;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_alt(List<Integer> n, List<Integer> n) {
if (n == null || n == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> nums = new ArrayList<>(n.size() + n.size());
int i = 0, j = 0;
while (i < n.size() && j < n.size()) {
if (n.get(i) <= n.get(j)) {
nums.add(n.get(i++));
} else {
nums.add(n.get(j++));
}
}
while (i < n.size()) {
nums.add(n.get(i++));
}
while (j < n.size()) {
nums.add(n.get(j++));
}
return nums;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that counts the number of vowels in a string.
|
public class StringUtils {
public static int countVowels_new(String n) {
if (n == null) {
return 0;
}
int count = 0;
for (char c : n.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) {
count++;
}
}
return count;
}
}
|
java
|
function
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString_v2(String nums) {
if (nums == null) {
return null;
}
return new StringBuilder(nums).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafe(String value) {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + value);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_alt(List<Integer> arr, List<Integer> nums) {
if (arr == null || nums == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> list = new ArrayList<>(arr.size() + nums.size());
int i = 0, j = 0;
while (i < arr.size() && j < nums.size()) {
if (arr.get(i) <= nums.get(j)) {
list.add(arr.get(i++));
} else {
list.add(nums.get(j++));
}
}
while (i < arr.size()) {
list.add(arr.get(i++));
}
while (j < nums.size()) {
list.add(nums.get(j++));
}
return list;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_alt(int nums, int list) {
if (list == 0) {
throw new IllegalArgumentException("list must not be zero");
}
return (double) nums / list;
}
}
|
java
|
bug-fix
|
easy
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int max_alt(int[] value) {
if (value == null || value.length == 0) {
throw new IllegalArgumentException("value must not be null or empty");
}
int max = value[0];
for (int v : value) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_alt(int n, int value) {
if (value == 0) {
throw new IllegalArgumentException("value must not be zero");
}
return (double) n / value;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted(List<Integer> text, List<Integer> value) {
if (text == null || value == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> input = new ArrayList<>(text.size() + value.size());
int i = 0, j = 0;
while (i < text.size() && j < value.size()) {
if (text.get(i) <= value.get(j)) {
input.add(text.get(i++));
} else {
input.add(value.get(j++));
}
}
while (i < text.size()) {
input.add(text.get(i++));
}
while (j < value.size()) {
input.add(value.get(j++));
}
return input;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that counts the number of vowels in a string.
|
public class StringUtils {
public static int countVowels(String arr) {
if (arr == null) {
return 0;
}
int count = 0;
for (char c : arr.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) {
count++;
}
}
return count;
}
}
|
java
|
function
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_v2(int arr, int input) {
if (input == 0) {
throw new IllegalArgumentException("input must not be zero");
}
return (double) arr / input;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSortedEx(List<Integer> n, List<Integer> nums) {
if (n == null || nums == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> nums = new ArrayList<>(n.size() + nums.size());
int i = 0, j = 0;
while (i < n.size() && j < nums.size()) {
if (n.get(i) <= nums.get(j)) {
nums.add(n.get(i++));
} else {
nums.add(nums.get(j++));
}
}
while (i < n.size()) {
nums.add(n.get(i++));
}
while (j < nums.size()) {
nums.add(nums.get(j++));
}
return nums;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafeEx(String n) {
try {
return Integer.parseInt(n);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + n);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrimeHelper(int text) {
if (text < 2) {
return false;
}
for (int i = 2; i * i <= text; i++) {
if (text % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString_alt(String nums) {
if (nums == null) {
return null;
}
return new StringBuilder(nums).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrime_alt(int nums) {
if (nums < 2) {
return false;
}
for (int i = 2; i * i <= nums; i++) {
if (nums % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafe_alt(String arr) {
try {
return Integer.parseInt(arr);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + arr);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int maxHelper(int[] n) {
if (n == null || n.length == 0) {
throw new IllegalArgumentException("n must not be null or empty");
}
int max = n[0];
for (int v : n) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrime_alt(int value) {
if (value < 2) {
return false;
}
for (int i = 2; i * i <= value; i++) {
if (value % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrimeEx(int input) {
if (input < 2) {
return false;
}
for (int i = 2; i * i <= input; i++) {
if (input % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafeEx(String value) {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + value);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_v2(int value, int arr) {
if (arr == 0) {
throw new IllegalArgumentException("arr must not be zero");
}
return (double) value / arr;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafe_v2(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + s);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int max_new(int[] n) {
if (n == null || n.length == 0) {
throw new IllegalArgumentException("n must not be null or empty");
}
int max = n[0];
for (int v : n) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_alt(List<Integer> arr, List<Integer> text) {
if (arr == null || text == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> input = new ArrayList<>(arr.size() + text.size());
int i = 0, j = 0;
while (i < arr.size() && j < text.size()) {
if (arr.get(i) <= text.get(j)) {
input.add(arr.get(i++));
} else {
input.add(text.get(j++));
}
}
while (i < arr.size()) {
input.add(arr.get(i++));
}
while (j < text.size()) {
input.add(text.get(j++));
}
return input;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_alt(int n, int nums) {
if (nums == 0) {
throw new IllegalArgumentException("nums must not be zero");
}
return (double) n / nums;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString(String arr) {
if (arr == null) {
return null;
}
return new StringBuilder(arr).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString_v2(String input) {
if (input == null) {
return null;
}
return new StringBuilder(input).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString_new(String input) {
if (input == null) {
return null;
}
return new StringBuilder(input).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSortedHelper(List<Integer> input, List<Integer> text) {
if (input == null || text == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> str = new ArrayList<>(input.size() + text.size());
int i = 0, j = 0;
while (i < input.size() && j < text.size()) {
if (input.get(i) <= text.get(j)) {
str.add(input.get(i++));
} else {
str.add(text.get(j++));
}
}
while (i < input.size()) {
str.add(input.get(i++));
}
while (j < text.size()) {
str.add(text.get(j++));
}
return str;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrime_alt(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divideHelper(int str, int n) {
if (n == 0) {
throw new IllegalArgumentException("n must not be zero");
}
return (double) str / n;
}
}
|
java
|
bug-fix
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide(int list, int text) {
if (text == 0) {
throw new IllegalArgumentException("text must not be zero");
}
return (double) list / text;
}
}
|
java
|
bug-fix
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divideHelper(int input, int n) {
if (n == 0) {
throw new IllegalArgumentException("n must not be zero");
}
return (double) input / n;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrimeHelper(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_alt(int text, int str) {
if (str == 0) {
throw new IllegalArgumentException("str must not be zero");
}
return (double) text / str;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafeHelper(String list) {
try {
return Integer.parseInt(list);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + list);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide(int text, int str) {
if (str == 0) {
throw new IllegalArgumentException("str must not be zero");
}
return (double) text / str;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted(List<Integer> list, List<Integer> text) {
if (list == null || text == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> list = new ArrayList<>(list.size() + text.size());
int i = 0, j = 0;
while (i < list.size() && j < text.size()) {
if (list.get(i) <= text.get(j)) {
list.add(list.get(i++));
} else {
list.add(text.get(j++));
}
}
while (i < list.size()) {
list.add(list.get(i++));
}
while (j < text.size()) {
list.add(text.get(j++));
}
return list;
}
}
|
java
|
function
|
medium
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int max(int[] text) {
if (text == null || text.length == 0) {
throw new IllegalArgumentException("text must not be null or empty");
}
int max = text[0];
for (int v : text) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that parses an integer from a string safely using try-catch.
|
public class Utils {
public static int parseIntSafe_new(String value) {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + value);
return 0;
}
}
}
|
java
|
exception-handling
|
medium
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted(List<Integer> arr, List<Integer> list) {
if (arr == null || list == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> str = new ArrayList<>(arr.size() + list.size());
int i = 0, j = 0;
while (i < arr.size() && j < list.size()) {
if (arr.get(i) <= list.get(j)) {
str.add(arr.get(i++));
} else {
str.add(list.get(j++));
}
}
while (i < arr.size()) {
str.add(arr.get(i++));
}
while (j < list.size()) {
str.add(list.get(j++));
}
return str;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that counts the number of vowels in a string.
|
public class StringUtils {
public static int countVowels_alt(String n) {
if (n == null) {
return 0;
}
int count = 0;
for (char c : n.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) {
count++;
}
}
return count;
}
}
|
java
|
function
|
easy
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrimeEx(int value) {
if (value < 2) {
return false;
}
for (int i = 2; i * i <= value; i++) {
if (value % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrimeHelper(int list) {
if (list < 2) {
return false;
}
for (int i = 2; i * i <= list; i++) {
if (list % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int maxEx(int[] nums) {
if (nums == null || nums.length == 0) {
throw new IllegalArgumentException("nums must not be null or empty");
}
int max = nums[0];
for (int v : nums) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_v2(List<Integer> arr, List<Integer> str) {
if (arr == null || str == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> input = new ArrayList<>(arr.size() + str.size());
int i = 0, j = 0;
while (i < arr.size() && j < str.size()) {
if (arr.get(i) <= str.get(j)) {
input.add(arr.get(i++));
} else {
input.add(str.get(j++));
}
}
while (i < arr.size()) {
input.add(arr.get(i++));
}
while (j < str.size()) {
input.add(str.get(j++));
}
return input;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_v2(List<Integer> list, List<Integer> nums) {
if (list == null || nums == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> s = new ArrayList<>(list.size() + nums.size());
int i = 0, j = 0;
while (i < list.size() && j < nums.size()) {
if (list.get(i) <= nums.get(j)) {
s.add(list.get(i++));
} else {
s.add(nums.get(j++));
}
}
while (i < list.size()) {
s.add(list.get(i++));
}
while (j < nums.size()) {
s.add(nums.get(j++));
}
return s;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted(List<Integer> arr, List<Integer> value) {
if (arr == null || value == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> value = new ArrayList<>(arr.size() + value.size());
int i = 0, j = 0;
while (i < arr.size() && j < value.size()) {
if (arr.get(i) <= value.get(j)) {
value.add(arr.get(i++));
} else {
value.add(value.get(j++));
}
}
while (i < arr.size()) {
value.add(arr.get(i++));
}
while (j < value.size()) {
value.add(value.get(j++));
}
return value;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSorted_alt(List<Integer> n, List<Integer> str) {
if (n == null || str == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> str = new ArrayList<>(n.size() + str.size());
int i = 0, j = 0;
while (i < n.size() && j < str.size()) {
if (n.get(i) <= str.get(j)) {
str.add(n.get(i++));
} else {
str.add(str.get(j++));
}
}
while (i < n.size()) {
str.add(n.get(i++));
}
while (j < str.size()) {
str.add(str.get(j++));
}
return str;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_v2(int arr, int str) {
if (str == 0) {
throw new IllegalArgumentException("str must not be zero");
}
return (double) arr / str;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that reverses a string.
|
public class StringUtils {
public static String reverseString(String s) {
if (s == null) {
return null;
}
return new StringBuilder(s).reverse().toString();
}
}
|
java
|
function
|
easy
|
|
Write a Java method that counts the number of vowels in a string.
|
public class StringUtils {
public static int countVowelsHelper(String nums) {
if (nums == null) {
return 0;
}
int count = 0;
for (char c : nums.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) {
count++;
}
}
return count;
}
}
|
java
|
function
|
easy
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrime_new(int nums) {
if (nums < 2) {
return false;
}
for (int i = 2; i * i <= nums; i++) {
if (nums % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_new(int n, int value) {
if (value == 0) {
throw new IllegalArgumentException("value must not be zero");
}
return (double) n / value;
}
}
|
java
|
bug-fix
|
easy
|
|
Write a Java method that merges two sorted lists of integers into a new sorted list.
|
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List<Integer> mergeSortedEx(List<Integer> s, List<Integer> list) {
if (s == null || list == null) {
throw new IllegalArgumentException("lists must not be null");
}
List<Integer> text = new ArrayList<>(s.size() + list.size());
int i = 0, j = 0;
while (i < s.size() && j < list.size()) {
if (s.get(i) <= list.get(j)) {
text.add(s.get(i++));
} else {
text.add(list.get(j++));
}
}
while (i < s.size()) {
text.add(s.get(i++));
}
while (j < list.size()) {
text.add(list.get(j++));
}
return text;
}
}
|
java
|
function
|
medium
|
|
Write a Java method that checks whether a number is prime.
|
public class MathUtils {
public static boolean isPrime_v2(int str) {
if (str < 2) {
return false;
}
for (int i = 2; i * i <= str; i++) {
if (str % i == 0) {
return false;
}
}
return true;
}
}
|
java
|
function
|
medium
|
|
Complete the following Java method so it returns the maximum value in an array.
|
public class ArrayUtils {
public static int max_alt(int[] text) {
if (text == null || text.length == 0) {
throw new IllegalArgumentException("text must not be null or empty");
}
int max = text[0];
for (int v : text) {
if (v > max) {
max = v;
}
}
return max;
}
}
|
java
|
completion
|
easy
|
|
Fix the following Java method so it avoids division by zero.
|
public class MathUtils {
public static double divide_v2(int n, int value) {
if (value == 0) {
throw new IllegalArgumentException("value must not be zero");
}
return (double) n / value;
}
}
|
java
|
bug-fix
|
easy
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.