Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

given an infinite sequence break it into intervals, and return a new infinite sequence with the average of each interval

i have to calculate the average of a Infinite Sequence using Stream API

Input:

Stream<Double> s = a,b,c,d ...
int interval = 3

Expected Result:

Stream<Double> result = avg(a,b,c), avg(d,e,f), ....

the result can be also an Iterator, or any other type as long as it mantains the structure of an infinite list

of course what i written is pseudo code and doesnt run

like image 344
florentin nica Avatar asked May 05 '21 17:05

florentin nica


People also ask

What is an infinite sequence of numbers with example?

The meaning is the same: For example, a 1 is equivalent to f (1). A simple example of an infinite sequence is 1, 4, 9, 16, 25, …. The elements here (a.k.a. the range) are called the terms of the sequence. Note that you can’t just write down a list of numbers and call it a “sequence”. It has to be a function.

What is an infinite series in math?

An infinite series (also called an infinite sum) is a series that keeps on going until infinity. For example, 1 + 1 + … or 1 + 2 + 3 +…. In notation, it’s written as:

How to merge intervals in a time series?

Given a set of time intervals in any order, merge all overlapping intervals into one and output the result which should have only mutually exclusive intervals. Let the intervals be represented as pairs of integers for simplicity.

How many diverges in the Fibonacci sequence?

5) The Fibonacci sequence 1, 1, 2, 3, 5, 8, … diverges. 6) The sequence 2, − 1, 1 2, − 1 4, 1 8, − 1 16, … is an example of a geometric sequence (see below; here, a = 2 and r = − 1 2 ).


Video Answer


2 Answers

There is a @Beta API termed mapWithIndex within Guava that could help here with certain assumption:

static Stream<Double> stepAverage(Stream<Double> stream, int step) {
    return Streams.mapWithIndex(stream, (from, index) -> Map.entry(index, from))
            .collect(Collectors.groupingBy(e -> (e.getKey() / step), TreeMap::new,
                    Collectors.averagingDouble(Map.Entry::getValue)))
            .values().stream();
}

The assumption that it brings in is detailed in the documentation clearly(emphasized by me):

The resulting stream is efficiently splittable if and only if stream was efficiently splittable and its underlying spliterator reported Spliterator.SUBSIZED. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.

like image 67
Naman Avatar answered Oct 06 '22 16:10

Naman


This should work fine using vanilla Java

I'm using Stream#mapMulti and a Set external to the Stream to aggregate the doubles

As you see, I also used DoubleSummaryStatistics to count the average. I could have use the traditional looping and summing then dividing but I found this way more explicit

Update: I changed the Collection used from Set to List as a Set could cause unexpected behaviour

int step = 3;
List<Double> list = new ArrayList<>();
Stream<Double> averagesStream =
        infiniteStream.mapMulti(((Double aDouble, Consumer<Double> doubleConsumer) -> {
            list.add(aDouble);
            if (list.size() == step) {
                DoubleSummaryStatistics doubleSummaryStatistics = new DoubleSummaryStatistics();
                list.forEach(doubleSummaryStatistics::accept);
                list.clear();
                doubleConsumer.accept(doubleSummaryStatistics.getAverage());
            }
        }));
like image 22
Yassin Hajaj Avatar answered Oct 06 '22 17:10

Yassin Hajaj