🪟Sliding Window Problems

Practice:

  • Calculate average temperature in windows of 5 readings from a weather dataset.

  • Count frequency of words in batches of 10 tweets to find trending terms.

  • Check for abnormal jumps in a stock price by comparing windows of daily returns.

The key steps are:

  1. Define window size

  2. Perform operation on each window

  3. Slide window and repeat

  4. Analyze results

Calculate average temperature in windows of 5 readings from a weather dataset.

{25.5, 26.2, 24.8, 23.9, 25.1, 26.8, 27.3, 24.6, 23.5, 24.9, 25.7, 26.5};
public class AverageTemperature {
    public static void main(String[] args) {
        // Assuming temperatureReadings is an array containing the temperature readings
        double[] temperatureReadings = {25.5, 26.2, 24.8, 23.9, 25.1, 26.8, 27.3, 24.6, 23.5, 24.9, 25.7, 26.5};
    
    // Initialize variables
    int windowSize = 5;
    int totalWindows = temperatureReadings.length - windowSize + 1;
    double[] averageTemperatures = new double[totalWindows];
    
    // Calculate average temperatures for each window
    for (int i = 0; i < totalWindows; i++) {
        double sum = 0;
        
        // Calculate sum of temperatures in the window
        for (int j = i; j < i + windowSize; j++) {
            sum += temperatureReadings[j];
        }
        
        // Calculate average temperature for the window
        averageTemperatures[i] = sum / windowSize;
    }
    
    // Print the average temperatures for each window
    for (double averageTemperature : averageTemperatures) {
        System.out.println("Average Temperature: " + averageTemperature);
    }
}

Counting Frequency of Words in Batches of 10 Tweets

Checking for Abnormal Jumps in Stock Prices

Last updated

Was this helpful?