Skip to content

Commit b5239c3

Browse files
committed
SlidingAverage
1 parent 27e4649 commit b5239c3

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package algorithms.sprint0;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.*;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class SlidingAverage {
14+
15+
private static List<Double> movingAverage(int n, List<Integer> arr, int windowSize) {
16+
int minSize = Math.min(arr.size(), n);
17+
int count = minSize - windowSize + 1;
18+
if (windowSize <= 0 || count <= 0) return java.util.Collections.emptyList();
19+
List<Double> result = new ArrayList<>(count);
20+
long sum = 0;
21+
for (int i = 0; i < windowSize; i++) sum += arr.get(i);
22+
result.add(sum / (double) windowSize);
23+
for (int i = windowSize; i < minSize; i++) {
24+
sum += arr.get(i) - arr.get(i - windowSize);
25+
result.add(sum / (double) windowSize);
26+
}
27+
return result;
28+
}
29+
30+
public static void main(String[] args) throws IOException {
31+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
32+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))) {
33+
int n = readInt(reader);
34+
List<Integer> arr = readList(reader);
35+
int windowSize = readInt(reader);
36+
List<Double> result = movingAverage(n, arr, windowSize);
37+
for (double elem : result) {
38+
writer.write(elem + " ");
39+
}
40+
}
41+
}
42+
43+
44+
private static int readInt(BufferedReader reader) throws IOException {
45+
return Integer.parseInt(reader.readLine());
46+
}
47+
48+
private static List<Integer> readList(BufferedReader reader) throws IOException {
49+
return Arrays.asList(reader.readLine().split(" "))
50+
.stream()
51+
.map(elem -> Integer.parseInt(elem))
52+
.collect(Collectors.toList());
53+
}
54+
55+
private static void assertListDoubles(List<Double> actual, double... expected) {
56+
assertEquals(expected.length, actual.size(), "size");
57+
for (int i = 0; i < expected.length; i++) {
58+
assertEquals(expected[i], actual.get(i), 1e-9, "idx=" + i);
59+
}
60+
}
61+
62+
@Test
63+
void test1() {
64+
List<Double> actual = movingAverage(7, List.of(1, 2, 3, 4, 5, 6, 7), 4);
65+
assertListDoubles(actual, 2.5, 3.5, 4.5, 5.5);
66+
}
67+
68+
@Test
69+
void w1_returnsOriginalValues() {
70+
List<Double> actual = movingAverage(7, List.of(1,2,3,4,5,6,7), 1);
71+
assertListDoubles(actual, 1,2,3,4,5,6,7);
72+
}
73+
74+
@Test
75+
void wEqMin_singleAverage() {
76+
List<Double> actual = movingAverage(7, List.of(1,2,3,4,5,6,7), 7);
77+
assertListDoubles(actual, 4.0);
78+
}
79+
80+
@Test
81+
void wGreaterThanMin_empty() {
82+
List<Double> actual = movingAverage(5, List.of(1,2,3,4,5,6,7), 6);
83+
assertEquals(List.of(), actual);
84+
}
85+
86+
@Test
87+
void wZeroOrNegative_empty() {
88+
assertEquals(List.of(), movingAverage(5, List.of(1,2,3,4,5), 0));
89+
assertEquals(List.of(), movingAverage(5, List.of(1,2,3,4,5), -3));
90+
}
91+
92+
@Test
93+
void cutByN_truncatesAndAverages() {
94+
List<Double> actual = movingAverage(5, List.of(1,2,3,4,5,6,7), 3);
95+
// окна по первым 5 элементам: [1,2,3],[2,3,4],[3,4,5]
96+
assertListDoubles(actual, 2.0, 3.0, 4.0);
97+
}
98+
99+
@Test
100+
void emptyArray_empty() {
101+
List<Double> actual = movingAverage(10, List.of(), 3);
102+
assertEquals(List.of(), actual);
103+
}
104+
105+
@Test
106+
void largeValues_noOverflowInSum() {
107+
int M = Integer.MAX_VALUE;
108+
List<Double> actual = movingAverage(4, List.of(M, M, M, M), 2);
109+
// три окна: [M,M],[M,M],[M,M] → среднее = M
110+
assertListDoubles(actual, M, M, M);
111+
}
112+
}

0 commit comments

Comments
 (0)