-4

my question/issue (Both really) is I want to check for a increment pattern in Java via a list.

What I mean basically is lets say I have a list of 10 samples worth of floats (Or doubles, or whantnot) with the following values:

3.2675781, 3.2800293, 3.232666, 3.1662598, 3.0898438, 3.0302734, 3.0253906, 2.9074707, 2.9343262, 2.9179688

Notice how it goes from small > bigger > small > smaller> smaller, etc.?

How can I detect this pattern if possible and what would be the most efficient way to do it.

Thanks!

Clysic
  • 25
  • 9
  • 1
    A couple of loops. Might this be relevant? [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) (The second last is getting greater again.) – Ole V.V. Feb 26 '19 at 18:16
  • I think there won't be any more efficient solution than `O(n)` (`n` == list size): iterate through array/list/structure (once), and return/store the output... – xerx593 Feb 26 '19 at 18:17
  • Pretty much impossible to detect an intentional pattern in such a short list. Best you can do is take the differences between successive elements and look for a common denominator. – Hot Licks Feb 26 '19 at 18:19
  • (Perhaps you need to explain better what you mean by "pattern".) – Hot Licks Feb 26 '19 at 18:20
  • `for(int i = 0; i < data.size() - 1; i++) { final int compare = data.get(i).compareTo(data.get(i + 1)); if (compare == 0) { System.out.println("equal"); } else if (compare < 0) { System.out.println("smaller"); } else { System.out.println("bigger"); }}` – xerx593 Feb 26 '19 at 18:23
  • Do you have an example of what you are trying to do for which you already have an answer? This might be the best way to explain to us what you're looking for. – CryptoFool Feb 26 '19 at 18:30
  • @Steve I basically have a list of float values that I put just for the heck of it. I was curious as per if there a way to detect if there was a way to detect a pattern as I said above. – Clysic Feb 26 '19 at 18:38

1 Answers1

0

You simply need a generic method to do that.

The idea is to take a custom Comparator that lets you compare the type, or your types should be comparable (should implement Comparable).

The following code shows this:

public <T extends Comparable<T>> boolean checkPattern(List<T> list) {
    return checkPattern(list, T::compareTo);
}

public <T> boolean checkPattern(List<T> list, Comparator<T> comparator) {
    // assuming null/empty/list with 1 element satisfy the pattern
    if (list == null || list.size() == 0 || list.size() == 1)
        return true;
    if (comparator.compare(list.get(0), list.get(1)) >= 0)
        return false;
    for (int i = 1; i < list.size() - 1; i++) {
        T current = list.get(i);
        T next = list.get(i + 1);
        if (comparator.compare(current, next) <= 0) {
            System.out.println(current + " " + next);
            return false;
        }
    }
    return true;
}

You can call it like:

System.out.println(new Test().checkPattern(Arrays.asList(3.2675781, 3.2800293, 3.232666, 3.1662598, 3.0898438, 3.0302734, 3.0253906, 2.9074707, 2.9343262, 2.9179688)));

And the output for this will be:

false

because 2.9074707 < 2.9343262.

Talking about the efficiency, the asymptotic complexity of the above solution is O(n) where n is the number of elements in the input list. We can't do better than this as we need to visit every element of the list (except the last one) to check if satisfy the condition or not.

Lavish Kothari
  • 2,211
  • 21
  • 29