Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,9 @@ updates:
interval: weekly
target-branch: main
open-pull-requests-limit: 10
- package-ecosystem: maven
directory: "/"
schedule:
interval: weekly
target-branch: master
open-pull-requests-limit: 10
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
target-branch: main
open-pull-requests-limit: 10
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
target-branch: master
open-pull-requests-limit: 10
5 changes: 4 additions & 1 deletion src/main/java/algorithms/sprint4/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static class Reader {
private int len = 0;

int read() throws IOException {
if (ptr == len) {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len == -1) {
Expand Down Expand Up @@ -189,6 +189,9 @@ int nextInt(int min, int max) throws IOException {
char nextCommand() throws IOException {
int c = read();
while (c <= ' ') {
if (c == -1) {
throw new IOException("Unexpected end of input");
}
c = read();
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/kyu4/MorseCodeDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public class MorseCodeDecoder {
ALPHABET_TO_MORSE.put("0", "-----");
ALPHABET_TO_MORSE.put(" ", " ");

MORSE_TO_ALPHABET.put("a", "b");
MORSE_TO_ALPHABET.put("c", "d");
MORSE_TO_ALPHABET.put("-.-.-.", ";");
MORSE_TO_ALPHABET.put("-...-", "=");
MORSE_TO_ALPHABET.put("---", "O");
Expand All @@ -84,7 +82,7 @@ public class MorseCodeDecoder {
MORSE_TO_ALPHABET.put("-.", "N");
MORSE_TO_ALPHABET.put("..---", "2");
MORSE_TO_ALPHABET.put("-....", "6");
MORSE_TO_ALPHABET.put("---...", ";");
MORSE_TO_ALPHABET.put("---...", ":");
MORSE_TO_ALPHABET.put(".-.-.", "+");
MORSE_TO_ALPHABET.put(".--.-.", "@");
MORSE_TO_ALPHABET.put("....-", "4");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/kyu6/GiveMeDiamond.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public static String print(int n) {
int count = n / 2 + 1;
StringBuilder strings = new StringBuilder();
for (int i = 1; i <= count; i++) {
strings.append(" ".repeat(count - i)).append("*".repeat(i * 2 - 1)).append(System.lineSeparator());
strings.append(" ".repeat(count - i)).append("*".repeat(i * 2 - 1)).append('\n');
}
for (int i = count - 1; i >= 1; i--) {
strings.append(" ".repeat(count - i)).append("*".repeat(i * 2 - 1)).append(System.lineSeparator());
strings.append(" ".repeat(count - i)).append("*".repeat(i * 2 - 1)).append('\n');
}
return strings.toString();
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/kyu6/Prime.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package kyu6;


import java.math.BigInteger;


public class Prime {

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

public static boolean isPrime(int num) {
return num > 0 && BigInteger.valueOf(num).isProbablePrime((int) Math.log(num) + 1);
if (num < 2) {
return false;
}
if (num % 2 == 0) {
return num == 2;
}
for (int divisor = 3; (long) divisor * divisor <= num; divisor += 2) {
if (num % divisor == 0) {
return false;
}
}
return true;
}


Expand Down
23 changes: 23 additions & 0 deletions src/test/java/algorithms/sprint4/MapTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package algorithms.sprint4;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.OptionalInt;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

@Tag("unit")
class MapTest {
Expand All @@ -23,6 +29,23 @@ void hashTableSupportsPutGetUpdateAndDelete() {
assertTrue(table.delete(1).isEmpty());
}

@Test
@Timeout(10)
void readerReportsEndOfInputInsteadOfReplayingTheBuffer() throws IOException {
InputStream original = System.in;
try {
System.setIn(new ByteArrayInputStream("g 7".getBytes(StandardCharsets.UTF_8)));
Map.Reader reader = new Map.Reader();

assertEquals('g', reader.nextCommand());
assertEquals(7, reader.nextInt());
assertThrows(IOException.class, reader::nextCommand);
assertThrows(IOException.class, reader::nextInt);
} finally {
System.setIn(original);
}
}

@Test
void hashTableHandlesCollisionsAndNegativeKeys() {
Map.HashTable table = new Map.HashTable();
Expand Down
1 change: 1 addition & 0 deletions src/test/java/kyu4/MorseCodeDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static Stream<Arguments> morseMessages() {
Arguments.of(".... . -.--", "HEY"),
Arguments.of("...---...", "SOS"),
Arguments.of(" .... . -.-- .--- ..- -.. . ", "HEY JUDE"),
Arguments.of("---... -.-.-.", ":;"),
Arguments.of("", "")
);
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/kyu6/PrimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@ public class PrimeTest {
void smokeTestsShouldExecuteApi() {
quality.SmokeMethodTestHarness.verify(kyu6.Prime.class);
}

@ParameterizedTest
@ValueSource(ints = {2, 3, 5, 7, 11, 13, 101, 7919, 2147483647})
void shouldAcceptPrimes(int candidate) {
assertTrue(isPrime(candidate));
}

@ParameterizedTest
@ValueSource(ints = {-7, -1, 0, 1, 4, 9, 15, 25, 49, 121, 7917, 2147483645})
void shouldRejectNonPrimes(int candidate) {
assertFalse(isPrime(candidate));
}
}