Skip to content

Commit 2dbd1c1

Browse files
committed
Added GrokkingSlidingWindow.
1 parent 8d09394 commit 2dbd1c1

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

GrokkingSlidingWindow.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.cunningdj.grokJava;
2+
3+
class GrokkingSlidingWindow {
4+
public static void main(String[] args) {
5+
Tester tester = new Tester();
6+
String testTitle="";
7+
8+
// TEST CLASS METHODS HERE USING TESTER CLASS
9+
// WINDOW_AVERAGE
10+
testTitle = "WINDOW_AVERAGE";
11+
tester.testDoubleArrayEquals(new double[]{2,3,4,5}, slidingAverage(new int[]{1,2,3,4,5,6}, 3), testTitle);
12+
tester.testDoubleArrayEquals(new double[]{3.5}, slidingAverage(new int[]{1,2,3,4,5,6}, 6), testTitle);
13+
tester.testDoubleArrayEquals(new double[]{1.5,2.5,3.5,4.5,5.5}, slidingAverage(new int[]{1,2,3,4,5,6}, 2), testTitle);
14+
}
15+
16+
public static double[] slidingAverage(int[] values, int windowSize) {
17+
if (windowSize > values.length) {
18+
return null;
19+
}
20+
// 1 2 3 4 5 6
21+
double windowSizeDouble = windowSize;
22+
double[] averages = new double[values.length - windowSize + 1];
23+
int averagesIdx = 0;
24+
double currAvg = 0;
25+
int i=0;
26+
while (i < windowSize) {
27+
currAvg += values[i];
28+
++i;
29+
}
30+
currAvg = currAvg / windowSizeDouble;
31+
averages[averagesIdx] = currAvg;
32+
while (i < values.length) {
33+
averagesIdx += 1;
34+
currAvg += (values[i] / windowSizeDouble) - (values[i-windowSize] / windowSizeDouble);
35+
averages[averagesIdx] = currAvg;
36+
++i;
37+
}
38+
return averages;
39+
}
40+
}

Tester.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ public void testEquals(Object expected, Object actual, String testTitle) {
4141
}
4242
}
4343

44+
public void testDoubleArrayEquals(double[] expected, double[] actual, String testTitle) {
45+
if (Arrays.equals(expected, actual)) {
46+
printTestSuccess(testTitle);
47+
} else {
48+
printTestFailure(testTitle, toString(expected), toString(actual));
49+
}
50+
}
51+
52+
public void testFloatArrayEquals(float[] expected, float[] actual, String testTitle) {
53+
if (Arrays.equals(expected, actual)) {
54+
printTestSuccess(testTitle);
55+
} else {
56+
printTestFailure(testTitle, toString(expected), toString(actual));
57+
}
58+
}
59+
4460
public void testIntArrayEquals(int[] expected, int[] actual, String testTitle) {
4561
if (Arrays.equals(expected, actual)) {
4662
printTestSuccess(testTitle);
@@ -71,6 +87,34 @@ private static String toString(int[] values) {
7187
return str;
7288
}
7389

90+
private static String toString(float[] values) {
91+
if (values == null) {
92+
return "null";
93+
}
94+
if (values.length < 1) {
95+
return "";
96+
}
97+
String str = String.valueOf(values[0]);
98+
for (int i=1; i < values.length; ++i) {
99+
str += "-" + String.valueOf(values[i]);
100+
}
101+
return str;
102+
}
103+
104+
private static String toString(double[] values) {
105+
if (values == null) {
106+
return "null";
107+
}
108+
if (values.length < 1) {
109+
return "";
110+
}
111+
String str = String.valueOf(values[0]);
112+
for (int i=1; i < values.length; ++i) {
113+
str += "-" + String.valueOf(values[i]);
114+
}
115+
return str;
116+
}
117+
74118
private static String toString(List<List<Integer>> allValues) {
75119
if (allValues == null) {
76120
return "null";

0 commit comments

Comments
 (0)