Skip to content

Commit 42f84b6

Browse files
committed
fix: test case format
1 parent 0cb872e commit 42f84b6

10 files changed

+282
-115
lines changed

java/twopointers/IsPalindromeValid.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ public boolean isPalindromeValid(String s) {
2828
}
2929

3030
public static void main(String[] args) {
31-
System.out.println("Tests an empty string. Expected output: " + new IsPalindromeValid().isPalindromeValid(""));
32-
System.out.println("Tests a single-character string. Expected output: " + new IsPalindromeValid().isPalindromeValid("a"));
33-
System.out.println("Tests a palindrome with two characters. Expected output: " + new IsPalindromeValid().isPalindromeValid("aa"));
34-
System.out.println("Tests a non-palindrome with two characters. Expected output: " + new IsPalindromeValid().isPalindromeValid("ab"));
35-
System.out.println("Tests a string with no alphanumeric characters. Expected output: " + new IsPalindromeValid().isPalindromeValid("!, (?)"));
36-
System.out.println("Tests a palindrome with punctuation and numbers. Expected output: " + new IsPalindromeValid().isPalindromeValid("21.02.2021"));
37-
System.out.println("Tests a non-palindrome with punctuation. Expected output: " + new IsPalindromeValid().isPalindromeValid("hello, world!"));
38-
31+
System.out.println("Tests an empty string.\n Input: s=\"\"\n Expected output: true\n Actual output: " + new IsPalindromeValid().isPalindromeValid(""));
32+
System.out.println("Tests a single-character string.\n Input: s=\"a\"\n Expected output: true\n Actual output: " + new IsPalindromeValid().isPalindromeValid("a"));
33+
System.out.println("Tests a palindrome with two characters.\n Input: s=\"aa\"\n Expected output: true\n Actual output: " + new IsPalindromeValid().isPalindromeValid("aa"));
34+
System.out.println("Tests a non-palindrome with two characters.\n Input: s=\"ab\"\n Expected output: false\n Actual output: " + new IsPalindromeValid().isPalindromeValid("ab"));
35+
System.out.println("Tests a string with no alphanumeric characters.\n Input: s=\"!, (?)\"\n Expected output: true\n Actual output: " + new IsPalindromeValid().isPalindromeValid("!, (?)"));
36+
System.out.println("Tests a palindrome with punctuation and numbers.\n Input: s=\"21.02.2021\"\n Expected output: false\n Actual output: " + new IsPalindromeValid().isPalindromeValid("21.02.2021"));
37+
System.out.println("Tests a non-palindrome with punctuation.\n Input: s=\"hello, world!\"\n Expected output: false\n Actual output: " + new IsPalindromeValid().isPalindromeValid("hello, world!"));
3938
}
4039
}

java/twopointers/LargestContainer.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,41 @@ public int largestContainer(int[] height) {
1212
// If both lines have the same height, move both pointers inward.
1313
if (height[left] < height[right]) {
1414
left++;
15-
} else if (height[left] > height[right]) {
16-
right--;
1715
} else {
18-
left++;
1916
right--;
2017
}
2118
}
2219
return maxWater;
2320
}
2421

2522
public static void main(String[] args) {
26-
System.out.println("Tests an empty array. Expected output: " + new LargestContainer().largestContainer(new int[]{}));
27-
System.out.println("Tests an array with one element. Expected output: " + new LargestContainer().largestContainer(new int[]{1}));
28-
System.out.println("Tests an array with no containers that can contain water. Expected output: " + new LargestContainer().largestContainer(new int[]{0, 1, 0}));
29-
System.out.println("Tests an array with all heights are the same. Expected output: " + new LargestContainer().largestContainer(new int[]{3, 3, 3, 3}));
30-
System.out.println("Tests an array with strictly increasing heights. Expected output: " + new LargestContainer().largestContainer(new int[]{1, 2, 3}));
31-
System.out.println("Tests an array with strictly decreasing heights. Expected output: " + new LargestContainer().largestContainer(new int[]{3, 2, 1}));
32-
}
23+
System.out.println("Tests an empty array.\n" +
24+
" Input: height=[]\n" +
25+
" Expected output: 0\n" +
26+
" Actual output: " + new LargestContainer().largestContainer(new int[]{}));
27+
28+
System.out.println("Tests an array with one element.\n" +
29+
" Input: height=[1]\n" +
30+
" Expected output: 0\n" +
31+
" Actual output: " + new LargestContainer().largestContainer(new int[]{1}));
32+
33+
System.out.println("Tests an array with no containers that can contain water.\n" +
34+
" Input: height=[0, 1, 0]\n" +
35+
" Expected output: 0\n" +
36+
" Actual output: " + new LargestContainer().largestContainer(new int[]{0, 1, 0}));
37+
38+
System.out.println("Tests an array where all heights are the same.\n" +
39+
" Input: height=[3, 3, 3, 3]\n" +
40+
" Expected output: 9\n" +
41+
" Actual output: " + new LargestContainer().largestContainer(new int[]{3, 3, 3, 3}));
42+
43+
System.out.println("Tests an array with strictly increasing heights.\n" +
44+
" Input: height=[1, 2, 3]\n" +
45+
" Expected output: 2\n" +
46+
" Actual output: " + new LargestContainer().largestContainer(new int[]{1, 2, 3}));
47+
48+
System.out.println("Tests an array with strictly decreasing heights.\n" +
49+
" Input: height=[3, 2, 1]\n" +
50+
" Expected output: 2\n" +
51+
" Actual output: " + new LargestContainer().largestContainer(new int[]{3, 2, 1})); }
3352
}

java/twopointers/LargestContainerBruteForce.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,33 @@ public int largestContainerBruteForce(int[] height) {
1717
}
1818

1919
public static void main(String[] args) {
20-
System.out.println("Tests an empty array. Expected output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{}));
21-
System.out.println("Tests an array with one element. Expected output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{1}));
22-
System.out.println("Tests an array with no containers that can contain water. Expected output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{0, 1, 0}));
23-
System.out.println("Tests an array with all heights are the same. Expected output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{3, 3, 3, 3}));
24-
System.out.println("Tests an array with strictly increasing heights. Expected output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{1, 2, 3}));
25-
System.out.println("Tests an array with strictly decreasing heights. Expected output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{3, 2, 1}));
26-
}
20+
System.out.println("Tests an empty array.\n" +
21+
" Input: height=[]\n" +
22+
" Expected output: 0\n" +
23+
" Actual output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{}));
24+
25+
System.out.println("Tests an array with one element.\n" +
26+
" Input: height=[1]\n" +
27+
" Expected output: 0\n" +
28+
" Actual output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{1}));
29+
30+
System.out.println("Tests an array with no containers that can contain water.\n" +
31+
" Input: height=[0, 1, 0]\n" +
32+
" Expected output: 0\n" +
33+
" Actual output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{0, 1, 0}));
34+
35+
System.out.println("Tests an array where all heights are the same.\n" +
36+
" Input: height=[3, 3, 3, 3]\n" +
37+
" Expected output: 9\n" +
38+
" Actual output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{3, 3, 3, 3}));
39+
40+
System.out.println("Tests an array with strictly increasing heights.\n" +
41+
" Input: height=[1, 2, 3]\n" +
42+
" Expected output: 2\n" +
43+
" Actual output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{1, 2, 3}));
44+
45+
System.out.println("Tests an array with strictly decreasing heights.\n" +
46+
" Input: height=[3, 2, 1]\n" +
47+
" Expected output: 2\n" +
48+
" Actual output: " + new LargestContainerBruteForce().largestContainerBruteForce(new int[]{3, 2, 1})); }
2749
}

java/twopointers/NextLexicographicalSequence.java

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,50 +41,35 @@ public void reverseCharArray(char[] chars, int start, int end) {
4141
}
4242
}
4343

44-
// This approach is the way of not converting string `s` into array of chars
45-
public String nextLexicographicalSequence2(String s) {
46-
// Locate the pivot, which is the first character from the right that breaks non-increasing order.
47-
// Start searching from the second-to-last position.
48-
int pivot = s.length() - 2;
49-
while (pivot >= 0 && s.charAt(pivot) >= s.charAt(pivot + 1)) {
50-
pivot--;
51-
}
52-
// If pivot is not found, the string is already in its largest permutation.
53-
// In this case, reverse the string to obtain the smallest permutation.
54-
if (pivot == -1) {
55-
return new StringBuilder(s).reverse().toString();
56-
}
57-
// Find the rightmost successor to the pivot.
58-
int rightMostSuccessor = s.length() - 1;
59-
while (s.charAt(rightMostSuccessor) <= s.charAt(pivot)) {
60-
rightMostSuccessor--;
61-
}
62-
// Swap the rightmost successor with the pivot to increase the lexicographical order of the suffix.
63-
StringBuilder sb = new StringBuilder(s); // Convert to StringBuilder type for `setCharAt` method to swap
64-
char temp = s.charAt(pivot);
65-
sb.setCharAt(pivot, s.charAt(rightMostSuccessor));
66-
sb.setCharAt(rightMostSuccessor, temp);
44+
public static void main(String[] args) {
45+
System.out.println("Tests a string with a single character.\n" +
46+
" Input: s='a'\n" +
47+
" Expected output: 'a'\n" +
48+
" Actual output: " + new NextLexicographicalSequence().nextLexicographicalSequence("a"));
6749

68-
// Convert the prefix (up to pivot) back to a string
69-
String prefix = sb.substring(0, pivot + 1);
50+
System.out.println("Tests a string with a repeated character.\n" +
51+
" Input: s='aaaa'\n" +
52+
" Expected output: 'aaaa'\n" +
53+
" Actual output: " + new NextLexicographicalSequence().nextLexicographicalSequence("aaaa"));
7054

71-
// Reverse the suffix after the pivot to minimize its permutation.
72-
// Note: convert to String type for `substring` method
73-
// Note: convert to StringBuilder type for `reverse` method
74-
String suffix = new StringBuilder(sb.substring(pivot + 1, s.length())).reverse().toString();
55+
System.out.println("Tests a string with a random pivot character.\n" +
56+
" Input: s='ynitsed'\n" +
57+
" Expected output: 'ynsdeit'\n" +
58+
" Actual output: " + new NextLexicographicalSequence().nextLexicographicalSequence("ynitsed"));
7559

76-
// Concatenate the prefix and reversed suffix and return
77-
return prefix + suffix;
78-
}
60+
System.out.println("Tests a string with a single character.\n" +
61+
" Input: s='a'\n" +
62+
" Expected output: 'a'\n" +
63+
" Actual output: " + new NextLexicographicalSequence().nextLexicographicalSequence2("a"));
7964

80-
public static void main(String[] args) {
81-
System.out.println("Tests a string with a single character. Expected result: 'a'. Actual result : " + new NextLexicographicalSequence().nextLexicographicalSequence("a"));
82-
System.out.println("Tests a string with a repeated character. Expected result: 'aaaa'. Actual result : " + new NextLexicographicalSequence().nextLexicographicalSequence("aaaa"));
83-
System.out.println("Tests a string with a random pivot character. Expected result: 'ynsdeit'. Actual result : " + new NextLexicographicalSequence().nextLexicographicalSequence("ynitsed"));
65+
System.out.println("Tests a string with a repeated character.\n" +
66+
" Input: s='aaaa'\n" +
67+
" Expected output: 'aaaa'\n" +
68+
" Actual output: " + new NextLexicographicalSequence().nextLexicographicalSequence2("aaaa"));
8469

85-
System.out.println("Tests a string with a single character. Expected result: 'a'. Actual result : " + new NextLexicographicalSequence().nextLexicographicalSequence2("a"));
86-
System.out.println("Tests a string with a repeated character. Expected result: 'aaaa'. Actual result : " + new NextLexicographicalSequence().nextLexicographicalSequence2("aaaa"));
87-
System.out.println("Tests a string with a random pivot character. Expected result: 'ynsdeit'. Actual result : " + new NextLexicographicalSequence().nextLexicographicalSequence2("ynitsed"));
88-
}
70+
System.out.println("Tests a string with a random pivot character.\n" +
71+
" Input: s='ynitsed'\n" +
72+
" Expected output: 'ynsdeit'\n" +
73+
" Actual output: " + new NextLexicographicalSequence().nextLexicographicalSequence2("ynitsed")); }
8974

9075
}

java/twopointers/PairSumSorted.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,38 @@ public int[] pairSumSorted(int[] nums, int target) {
2020
}
2121

2222
public static void main(String[] args) {
23-
System.out.println("Tests an empty array. Expected output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{}, 0)));
24-
System.out.println("Tests an array with one element. Expected output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{1}, 1)));
25-
System.out.println("Tests a two-element array that contains a pair that sums to the target. Expected output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{2, 3}, 5)));
26-
System.out.println("Tests a two-element array that does not contain a pair that sums to the target. Expected output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{2, 4}, 5)));
27-
System.out.println("Tests an array with duplicated values. Expected output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{2, 2, 3}, 5)));
28-
System.out.println("Tests if the algorithm works with a negative number in the target pair. Expected output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{-1, 2, 3}, 2)));
29-
System.out.println("Tests if the algorithm works with both numbers of the target pair being negative. Expected output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{-3, -2, -1}, -5)));
30-
}
23+
System.out.println("Tests an empty array.\n" +
24+
" Input: nums=[], target=0\n" +
25+
" Expected output: []\n" +
26+
" Actual output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{}, 0)));
27+
28+
System.out.println("Tests an array with one element.\n" +
29+
" Input: nums=[1], target=1\n" +
30+
" Expected output: []\n" +
31+
" Actual output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{1}, 1)));
32+
33+
System.out.println("Tests a two-element array that contains a pair that sums to the target.\n" +
34+
" Input: nums=[2, 3], target=5\n" +
35+
" Expected output: [0, 1]\n" +
36+
" Actual output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{2, 3}, 5)));
37+
38+
System.out.println("Tests a two-element array that does not contain a pair that sums to the target.\n" +
39+
" Input: nums=[2, 4], target=5\n" +
40+
" Expected output: []\n" +
41+
" Actual output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{2, 4}, 5)));
42+
43+
System.out.println("Tests an array with duplicated values.\n" +
44+
" Input: nums=[2, 2, 3], target=5\n" +
45+
" Expected output: [0, 2]\n" +
46+
" Actual output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{2, 2, 3}, 5)));
47+
48+
System.out.println("Tests if the algorithm works with a negative number in the target pair.\n" +
49+
" Input: nums=[-1, 2, 3], target=2\n" +
50+
" Expected output: [0, 2]\n" +
51+
" Actual output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{-1, 2, 3}, 2)));
52+
53+
System.out.println("Tests if the algorithm works with both numbers of the target pair being negative.\n" +
54+
" Input: nums=[-3, -2, -1], target=-5\n" +
55+
" Expected output: [0, 1]\n" +
56+
" Actual output: " + Arrays.toString(new PairSumSorted().pairSumSorted(new int[]{-3, -2, -1}, -5))); }
3157
}

0 commit comments

Comments
 (0)