Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c1d6b89
AB
krotname Aug 17, 2025
0670bb4
Zip
krotname Aug 17, 2025
6025546
Zip
krotname Aug 17, 2025
27e4649
Zip
krotname Aug 17, 2025
b5239c3
SlidingAverage
krotname Aug 17, 2025
3c8cfaa
Utils
krotname Aug 17, 2025
86d3192
Utils
krotname Aug 17, 2025
ac301e6
Utils
krotname Aug 17, 2025
2055d72
Solution2
krotname Feb 15, 2026
b48ddcb
Distances
krotname Feb 15, 2026
5654158
SleightOfHand
krotname Feb 15, 2026
93d5c72
Distances
krotname Feb 19, 2026
b3134d4
Distances
krotname Feb 19, 2026
baac444
Distances
krotname Feb 19, 2026
96a27e5
SleightOfHand
krotname Feb 19, 2026
be66332
Deque
krotname Feb 19, 2026
4aea0f2
Deque
krotname Feb 19, 2026
fb3fdda
Calculator
krotname Feb 19, 2026
8b1fa1d
Calculator
krotname Feb 19, 2026
57eb1f1
Deque
krotname Feb 27, 2026
419d53b
Calculator
krotname Feb 27, 2026
798f3f3
sprint3
krotname Apr 7, 2026
8a42525
sprint3 FastSort
krotname Apr 8, 2026
bbf32c4
sprint3 FastSort
krotname Apr 9, 2026
b55cd57
sprint3 FastSort
krotname Apr 9, 2026
870b6b3
sprint4
krotname Apr 11, 2026
052a615
sprint4
krotname Apr 11, 2026
f21d9e5
sprint4
krotname Apr 11, 2026
29641ef
sprint4
krotname Apr 11, 2026
1846c91
sprint4
krotname Apr 13, 2026
efb346c
sprint4
krotname Apr 13, 2026
0cf4a82
sprint4
krotname Apr 13, 2026
4c4e406
sprint5
krotname Apr 18, 2026
a41b5e8
sprint5
krotname Apr 18, 2026
5bd106f
sprint5
krotname Apr 18, 2026
0f038b3
sprint5
krotname Apr 19, 2026
b527396
DorogayaSet
krotname May 31, 2026
279d233
WaterWorld
krotname May 31, 2026
41b1359
WaterWorld
krotname May 31, 2026
0cc6a5b
sprint 6
krotname May 31, 2026
d06bfff
sprint 6
krotname May 31, 2026
2ef9b62
sprint 7
krotname Jun 1, 2026
6a2f0c4
sprint 7
krotname Jun 1, 2026
103177f
sprint 8
krotname Jun 2, 2026
bff3bb6
sprint 6
krotname Jun 2, 2026
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
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<source>21</source>
<target>17</target>
<release>17</release>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/algorithms/sprint0/AB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package algorithms.sprint0;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.InputMismatchException;
import java.util.Scanner;

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

public class AB {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
long sum = sum(a, b);
System.out.println(sum);
}

public static long sum(int a, int b) {
return (long) a + b;
}

@Test
public void test() {
assertEquals(5, sum(2, 3));
assertEquals(-1, sum(-2, 1));
}

@ParameterizedTest
@CsvSource({
"0,0,0",
"2,3,5",
"-1,4,3",
"2147483647,1,2147483648",
"2000000000,2000000000,4000000000"
})
void sum_works(int a, int b, long expected) {
assertEquals(expected, AB.sum(a, b));
}

@Test
void main_prints_sum_basic() {

Check warning on line 50 in src/main/java/algorithms/sprint0/AB.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfUcLoLvvXVa7xe2&open=AZ58YfUcLoLvvXVa7xe2&pullRequest=4
String in = "2 3\n";
InputStream oldIn = System.in;
PrintStream oldOut = System.out;
try {
System.setIn(new ByteArrayInputStream(in.getBytes(StandardCharsets.UTF_8)));
ByteArrayOutputStream buf = new ByteArrayOutputStream();
System.setOut(new PrintStream(buf, true, StandardCharsets.UTF_8));

AB.main(new String[0]);

String nl = System.lineSeparator();
assertEquals("5" + nl, buf.toString(StandardCharsets.UTF_8));
} finally {
System.setIn(oldIn);
System.setOut(oldOut);
}
}

@Test
void main_bad_input_throws() {
System.setIn(new ByteArrayInputStream("x y\n".getBytes(StandardCharsets.UTF_8)));
assertThrows(InputMismatchException.class, () -> AB.main(new String[0]));
}
}
100 changes: 100 additions & 0 deletions src/main/java/algorithms/sprint0/SlidingAverage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package algorithms.sprint0;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static algorithms.sprint0.Utils.readInt;
import static algorithms.sprint0.Utils.readList;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SlidingAverage {

private static List<Double> movingAverage(int n, List<Integer> arr, int windowSize) {
int minSize = Math.min(arr.size(), n);
int count = minSize - windowSize + 1;
if (windowSize <= 0 || count <= 0) return java.util.Collections.emptyList();
List<Double> result = new ArrayList<>(count);
long sum = 0;
for (int i = 0; i < windowSize; i++) sum += arr.get(i);
result.add(sum / (double) windowSize);
for (int i = windowSize; i < minSize; i++) {
sum += arr.get(i) - arr.get(i - windowSize);
result.add(sum / (double) windowSize);
}
return result;
}

public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))) {
int n = readInt(reader);
List<Integer> arr = readList(reader);
int windowSize = readInt(reader);
List<Double> result = movingAverage(n, arr, windowSize);
for (double elem : result) {
writer.write(elem + " ");
}
}
}

private static void assertListDoubles(List<Double> actual, double... expected) {
assertEquals(expected.length, actual.size(), "size");
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], actual.get(i), 1e-9, "idx=" + i);
}
}

@Test
void test1() {
List<Double> actual = movingAverage(7, List.of(1, 2, 3, 4, 5, 6, 7), 4);
assertListDoubles(actual, 2.5, 3.5, 4.5, 5.5);
}

@Test
void w1_returnsOriginalValues() {

Check warning on line 57 in src/main/java/algorithms/sprint0/SlidingAverage.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfSpLoLvvXVa7xek&open=AZ58YfSpLoLvvXVa7xek&pullRequest=4
List<Double> actual = movingAverage(7, List.of(1, 2, 3, 4, 5, 6, 7), 1);
assertListDoubles(actual, 1, 2, 3, 4, 5, 6, 7);
}

@Test
void wEqMin_singleAverage() {
List<Double> actual = movingAverage(7, List.of(1, 2, 3, 4, 5, 6, 7), 7);
assertListDoubles(actual, 4.0);
}

@Test
void wGreaterThanMin_empty() {
List<Double> actual = movingAverage(5, List.of(1, 2, 3, 4, 5, 6, 7), 6);
assertEquals(List.of(), actual);
}

@Test
void wZeroOrNegative_empty() {
assertEquals(List.of(), movingAverage(5, List.of(1, 2, 3, 4, 5), 0));
assertEquals(List.of(), movingAverage(5, List.of(1, 2, 3, 4, 5), -3));
}

@Test
void cutByN_truncatesAndAverages() {
List<Double> actual = movingAverage(5, List.of(1, 2, 3, 4, 5, 6, 7), 3);
// окна по первым 5 элементам: [1,2,3],[2,3,4],[3,4,5]
assertListDoubles(actual, 2.0, 3.0, 4.0);
}

@Test
void emptyArray_empty() {
List<Double> actual = movingAverage(10, List.of(), 3);
assertEquals(List.of(), actual);
}

@Test
void largeValues_noOverflowInSum() {

Check warning on line 94 in src/main/java/algorithms/sprint0/SlidingAverage.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfSpLoLvvXVa7xeq&open=AZ58YfSpLoLvvXVa7xeq&pullRequest=4
int M = Integer.MAX_VALUE;
List<Double> actual = movingAverage(4, List.of(M, M, M, M), 2);
// три окна: [M,M],[M,M],[M,M] → среднее = M
assertListDoubles(actual, M, M, M);
}
}
150 changes: 150 additions & 0 deletions src/main/java/algorithms/sprint0/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package algorithms.sprint0;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Utils {

public static List<Integer> readList(BufferedReader reader) throws IOException {
String line = reader.readLine();
if (line == null) throw new NumberFormatException("EOF");
return Arrays.stream(line.trim().split("\\s+"))
.map(Integer::parseInt)
.collect(Collectors.toList());

Check warning on line 22 in src/main/java/algorithms/sprint0/Utils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfUiLoLvvXVa7xe5&open=AZ58YfUiLoLvvXVa7xe5&pullRequest=4
}

public static <T> void printList(List<T> list, Writer writer) {
for (T elem : list) {
try {
writer.write(String.valueOf(elem));
writer.write(" ");
} catch (IOException ignored) {
}
}
}

public static int readInt(BufferedReader reader) throws IOException {
String line = reader.readLine();
if (line == null) throw new NumberFormatException("EOF");
return Integer.parseInt(line);
}

@Test
public void readListbasic() throws Exception {
BufferedReader br = new BufferedReader(new StringReader("1 2 3"));
List<Integer> got = readList(br);
assertEquals(Arrays.asList(1, 2, 3), got);
}

@Test
void readList_mixedWhitespace_and_signs() throws Exception {
BufferedReader br = new BufferedReader(new StringReader(" -1\t0 5 "));
List<Integer> got = readList(br);
assertEquals(Arrays.asList(-1, 0, 5), got);
}

@Test
void readList_emptyLine_throwsNumberFormat() {
BufferedReader br = new BufferedReader(new StringReader("\\n"));
assertThrows(NumberFormatException.class, () -> readList(br));
}

@Test
void readList_EOF_throwsNumberFormat() {

Check warning on line 62 in src/main/java/algorithms/sprint0/Utils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfUiLoLvvXVa7xe9&open=AZ58YfUiLoLvvXVa7xe9&pullRequest=4
BufferedReader br = new BufferedReader(new StringReader(""));
assertThrows(NumberFormatException.class, () -> readList(br));
}

@Test
void readList_nonIntegerToken_throwsNumberFormat() {

Check warning on line 68 in src/main/java/algorithms/sprint0/Utils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfUiLoLvvXVa7xe-&open=AZ58YfUiLoLvvXVa7xe-&pullRequest=4
BufferedReader br = new BufferedReader(new StringReader("1 a 3"));
assertThrows(NumberFormatException.class, () -> readList(br));
}

@Test
void readList_overflow_throwsNumberFormat() {
BufferedReader br = new BufferedReader(new StringReader("2147483648"));
assertThrows(NumberFormatException.class, () -> readList(br));
}

// --- readInt ---

@Test
void readInt_basic() throws Exception {

Check warning on line 82 in src/main/java/algorithms/sprint0/Utils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfUiLoLvvXVa7xfA&open=AZ58YfUiLoLvvXVa7xfA&pullRequest=4
BufferedReader br = new BufferedReader(new StringReader("42"));
assertEquals(42, readInt(br));
}

@Test
void readInt_negative() throws Exception {

Check warning on line 88 in src/main/java/algorithms/sprint0/Utils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfUiLoLvvXVa7xfB&open=AZ58YfUiLoLvvXVa7xfB&pullRequest=4
BufferedReader br = new BufferedReader(new StringReader("-7"));
assertEquals(-7, readInt(br));
}

@Test
void readInt_withSpaces_throwsNumberFormat() {
BufferedReader br = new BufferedReader(new StringReader(" 42 "));
assertThrows(NumberFormatException.class, () -> readInt(br));
}

// --- printList ---

@Test
void printList_integers_trailingSpace_noNewline() throws Exception {
StringWriter sw = new StringWriter();
printList(Arrays.asList(1, 2, 3), sw);
assertEquals("1 2 3 ", sw.toString());
}

@Test
void printList_strings_usesToString() throws Exception {
StringWriter sw = new StringWriter();
printList(Arrays.asList("a", "b"), sw);
assertEquals("a b ", sw.toString());
}

@Test
void printList_empty_writesNothing() throws Exception {
StringWriter sw = new StringWriter();
printList(List.of(), sw);
assertEquals("", sw.toString());
}

@Test
void printList_ioExceptions_areSwallowed() {
Writer throwing = new Writer() {
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new IOException("boom");
}

@Override
public void flush() {
}

@Override
public void close() {

Check failure on line 135 in src/main/java/algorithms/sprint0/Utils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.

See more on https://sonarcloud.io/project/issues?id=krotname_Codewars&issues=AZ58YfUiLoLvvXVa7xfI&open=AZ58YfUiLoLvvXVa7xfI&pullRequest=4
}

@Override
public void write(String str) throws IOException {
throw new IOException("boom");
}

@Override
public void write(int c) throws IOException {
throw new IOException("boom");
}
};
assertDoesNotThrow(() -> printList(Arrays.asList(1, 2, 3), throwing));
}
}
Loading
Loading