Skip to content

Commit f325279

Browse files
authored
refactor: refactor Alphabetical and AlphabeticalTest (#6355)
1 parent 22cba2c commit f325279

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package com.thealgorithms.strings;
22

33
/**
4+
* Utility class for checking if a string's characters are in alphabetical order.
5+
* <p>
46
* Alphabetical order is a system whereby character strings are placed in order
57
* based on the position of the characters in the conventional ordering of an
6-
* alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
8+
* alphabet.
9+
* <p>
10+
* Reference: <a href="https://en.wikipedia.org/wiki/Alphabetical_order">Wikipedia: Alphabetical Order</a>
711
*/
8-
final class Alphabetical {
12+
public final class Alphabetical {
913
private Alphabetical() {
1014
}
1115

12-
public static void main(String[] args) {
13-
assert !isAlphabetical("123abc");
14-
assert isAlphabetical("aBC");
15-
assert isAlphabetical("abc");
16-
assert !isAlphabetical("xyzabc");
17-
assert isAlphabetical("abcxyz");
18-
}
19-
2016
/**
21-
* Check if a string is alphabetical order or not
17+
* Checks whether the characters in the given string are in alphabetical order.
18+
* Non-letter characters will cause the check to fail.
2219
*
23-
* @param s a string
24-
* @return {@code true} if given string is alphabetical order, otherwise
25-
* {@code false}
20+
* @param s the input string
21+
* @return {@code true} if all characters are in alphabetical order (case-insensitive), otherwise {@code false}
2622
*/
2723
public static boolean isAlphabetical(String s) {
2824
s = s.toLowerCase();
2925
for (int i = 0; i < s.length() - 1; ++i) {
30-
if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <= s.charAt(i + 1))) {
26+
if (!Character.isLetter(s.charAt(i)) || s.charAt(i) > s.charAt(i + 1)) {
3127
return false;
3228
}
3329
}
34-
return true;
30+
return !s.isEmpty() && Character.isLetter(s.charAt(s.length() - 1));
3531
}
3632
}
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
11
package com.thealgorithms.strings;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
54

6-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
77

88
public class AlphabeticalTest {
99

10-
@Test
11-
public void isAlphabetical() {
12-
// expected to be true
13-
String input1 = "abcdefghijklmno";
14-
String input2 = "abcdxxxyzzzz";
15-
String input3 = "fpw";
16-
17-
// expected to be false
18-
String input4 = "123a";
19-
String input5 = "abcABC";
20-
String input6 = "abcdefghikjlmno";
21-
22-
assertTrue(Alphabetical.isAlphabetical(input1));
23-
assertTrue(Alphabetical.isAlphabetical(input2));
24-
assertTrue(Alphabetical.isAlphabetical(input3));
25-
26-
assertFalse(Alphabetical.isAlphabetical(input4));
27-
assertFalse(Alphabetical.isAlphabetical(input5));
28-
assertFalse(Alphabetical.isAlphabetical(input6));
10+
@ParameterizedTest(name = "\"{0}\" → Expected: {1}")
11+
@CsvSource({"'abcdefghijklmno', true", "'abcdxxxyzzzz', true", "'123a', false", "'abcABC', false", "'abcdefghikjlmno', false", "'aBC', true", "'abc', true", "'xyzabc', false", "'abcxyz', true", "'', false", "'1', false"})
12+
void testIsAlphabetical(String input, boolean expected) {
13+
assertEquals(expected, Alphabetical.isAlphabetical(input));
2914
}
3015
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy