Created
March 28, 2020 05:26
-
-
Save ramannanda9/4dd2c775b52c66085b6cba4592c170de to your computer and use it in GitHub Desktop.
FunctionTP - to test basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.orastack; | |
import java.lang.reflect.Array; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.stream.Stream; | |
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import lombok.Setter; | |
public class FunctionalTP { | |
public static List<Double> runningAvg(List<Double> list) { | |
int count = 0; | |
List<Double> results = new ArrayList<>(); | |
double sum = 0; | |
for (Double item : list) { | |
sum += item; | |
count++; | |
results.add(sum / count); | |
} | |
return results; | |
} | |
public static double[] runningAvgWindow(double[] items, int windowSize) { | |
double[] cumSum = new double[items.length + 1]; | |
int currentWindowSize = 1; | |
double[] result = new double[items.length]; | |
for (int i = 0; i < items.length; i++) { | |
cumSum[i + 1] = cumSum[i] + items[i]; | |
if (currentWindowSize >= windowSize) { | |
result[i] = (cumSum[i + 1] - cumSum[i - windowSize + 1]) / windowSize; | |
} else { | |
result[i] = cumSum[i + 1] / currentWindowSize; | |
currentWindowSize++; | |
} | |
} | |
return result; | |
} | |
@AllArgsConstructor | |
private static class Pair<T, V> { | |
T first; | |
V second; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment