Skip to content

Commit e5a57b7

Browse files
krotnameclaude
andcommitted
fix: make isPrime deterministic and stop the map reader from looping at EOF
- replace the probabilistic isPrime certainty with trial division - make the Reader EOF sticky and fail fast in nextCommand - decode "---..." as a colon and drop two junk reverse-morse entries - emit "\n" from the diamond kata instead of the platform separator - drop the dependabot entries targeting the non-existent master branch Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent e96c9b5 commit e5a57b7

8 files changed

Lines changed: 55 additions & 21 deletions

File tree

‎.github/dependabot.yml‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,9 @@ updates:
66
interval: weekly
77
target-branch: main
88
open-pull-requests-limit: 10
9-
- package-ecosystem: maven
10-
directory: "/"
11-
schedule:
12-
interval: weekly
13-
target-branch: master
14-
open-pull-requests-limit: 10
159
- package-ecosystem: github-actions
1610
directory: "/"
1711
schedule:
1812
interval: weekly
1913
target-branch: main
2014
open-pull-requests-limit: 10
21-
- package-ecosystem: github-actions
22-
directory: "/"
23-
schedule:
24-
interval: weekly
25-
target-branch: master
26-
open-pull-requests-limit: 10

‎src/main/java/algorithms/sprint4/Map.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static class Reader {
137137
private int len = 0;
138138

139139
int read() throws IOException {
140-
if (ptr == len) {
140+
if (ptr >= len) {
141141
len = in.read(buffer);
142142
ptr = 0;
143143
if (len == -1) {
@@ -189,6 +189,9 @@ int nextInt(int min, int max) throws IOException {
189189
char nextCommand() throws IOException {
190190
int c = read();
191191
while (c <= ' ') {
192+
if (c == -1) {
193+
throw new IOException("Unexpected end of input");
194+
}
192195
c = read();
193196
}
194197

‎src/main/java/kyu4/MorseCodeDecoder.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ public class MorseCodeDecoder {
5656
ALPHABET_TO_MORSE.put("0", "-----");
5757
ALPHABET_TO_MORSE.put(" ", " ");
5858

59-
MORSE_TO_ALPHABET.put("a", "b");
60-
MORSE_TO_ALPHABET.put("c", "d");
6159
MORSE_TO_ALPHABET.put("-.-.-.", ";");
6260
MORSE_TO_ALPHABET.put("-...-", "=");
6361
MORSE_TO_ALPHABET.put("---", "O");
@@ -84,7 +82,7 @@ public class MorseCodeDecoder {
8482
MORSE_TO_ALPHABET.put("-.", "N");
8583
MORSE_TO_ALPHABET.put("..---", "2");
8684
MORSE_TO_ALPHABET.put("-....", "6");
87-
MORSE_TO_ALPHABET.put("---...", ";");
85+
MORSE_TO_ALPHABET.put("---...", ":");
8886
MORSE_TO_ALPHABET.put(".-.-.", "+");
8987
MORSE_TO_ALPHABET.put(".--.-.", "@");
9088
MORSE_TO_ALPHABET.put("....-", "4");

‎src/main/java/kyu6/GiveMeDiamond.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public static String print(int n) {
1111
int count = n / 2 + 1;
1212
StringBuilder strings = new StringBuilder();
1313
for (int i = 1; i <= count; i++) {
14-
strings.append(" ".repeat(count - i)).append("*".repeat(i * 2 - 1)).append(System.lineSeparator());
14+
strings.append(" ".repeat(count - i)).append("*".repeat(i * 2 - 1)).append('\n');
1515
}
1616
for (int i = count - 1; i >= 1; i--) {
17-
strings.append(" ".repeat(count - i)).append("*".repeat(i * 2 - 1)).append(System.lineSeparator());
17+
strings.append(" ".repeat(count - i)).append("*".repeat(i * 2 - 1)).append('\n');
1818
}
1919
return strings.toString();
2020
}

‎src/main/java/kyu6/Prime.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package kyu6;
22

33

4-
import java.math.BigInteger;
5-
64

75
public class Prime {
86

97
//6 https://www.codewars.com/kata/5262119038c0985a5b00029f/train/java
108

119
public static boolean isPrime(int num) {
12-
return num > 0 && BigInteger.valueOf(num).isProbablePrime((int) Math.log(num) + 1);
10+
if (num < 2) {
11+
return false;
12+
}
13+
if (num % 2 == 0) {
14+
return num == 2;
15+
}
16+
for (int divisor = 3; (long) divisor * divisor <= num; divisor += 2) {
17+
if (num % divisor == 0) {
18+
return false;
19+
}
20+
}
21+
return true;
1322
}
1423

1524

‎src/test/java/algorithms/sprint4/MapTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package algorithms.sprint4;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56

7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.charset.StandardCharsets;
611
import java.util.OptionalInt;
712
import org.junit.jupiter.api.Tag;
813
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.Timeout;
915

1016
@Tag("unit")
1117
class MapTest {
@@ -23,6 +29,23 @@ void hashTableSupportsPutGetUpdateAndDelete() {
2329
assertTrue(table.delete(1).isEmpty());
2430
}
2531

32+
@Test
33+
@Timeout(10)
34+
void readerReportsEndOfInputInsteadOfReplayingTheBuffer() throws IOException {
35+
InputStream original = System.in;
36+
try {
37+
System.setIn(new ByteArrayInputStream("g 7".getBytes(StandardCharsets.UTF_8)));
38+
Map.Reader reader = new Map.Reader();
39+
40+
assertEquals('g', reader.nextCommand());
41+
assertEquals(7, reader.nextInt());
42+
assertThrows(IOException.class, reader::nextCommand);
43+
assertThrows(IOException.class, reader::nextInt);
44+
} finally {
45+
System.setIn(original);
46+
}
47+
}
48+
2649
@Test
2750
void hashTableHandlesCollisionsAndNegativeKeys() {
2851
Map.HashTable table = new Map.HashTable();

‎src/test/java/kyu4/MorseCodeDecoderTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static Stream<Arguments> morseMessages() {
5151
Arguments.of(".... . -.--", "HEY"),
5252
Arguments.of("...---...", "SOS"),
5353
Arguments.of(" .... . -.-- .--- ..- -.. . ", "HEY JUDE"),
54+
Arguments.of("---... -.-.-.", ":;"),
5455
Arguments.of("", "")
5556
);
5657
}

‎src/test/java/kyu6/PrimeTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ public class PrimeTest {
2121
void smokeTestsShouldExecuteApi() {
2222
quality.SmokeMethodTestHarness.verify(kyu6.Prime.class);
2323
}
24+
25+
@ParameterizedTest
26+
@ValueSource(ints = {2, 3, 5, 7, 11, 13, 101, 7919, 2147483647})
27+
void shouldAcceptPrimes(int candidate) {
28+
assertTrue(isPrime(candidate));
29+
}
30+
31+
@ParameterizedTest
32+
@ValueSource(ints = {-7, -1, 0, 1, 4, 9, 15, 25, 49, 121, 7917, 2147483645})
33+
void shouldRejectNonPrimes(int candidate) {
34+
assertFalse(isPrime(candidate));
35+
}
2436
}

0 commit comments

Comments
 (0)