2

I am creating a program that when given a list, returns a new integer list where the first two elements remain the same, and where each element after is the median of the three elements ending in that position in the original list. For example, given the list: [1, 2, 3, 4, 5, 6, 7, 8, 9], the program would return: [1, 2, 2, 3, 4, 5, 6, 7, 8].

This is the code I've written where I'm getting the correct results, but it is failing my tester. I'm not sure if I'm missing an odd case.

11
  • So what is your tester testing? Have you debugged through that failing test step by step? Commented Mar 13, 2020 at 12:33
  • it's a tester used in my class. I can post the code from it? Commented Mar 13, 2020 at 12:36
  • I would focus on providing a minimal reproducible example, with just the test situation that's failing. If you think your code is correct, you should carefully examine the test that's failing - do you agree with what it's testing? Commented Mar 13, 2020 at 12:40
  • @BeanieLeung I totaly agree with Jon, that a minimal example would be best. Buit still i would suggest that you post you test code too, it is a relevant part of your auestion and example. Commented Mar 13, 2020 at 12:45
  • I posted the tester Commented Mar 13, 2020 at 12:49

1 Answer 1

2

Yes you missed some cases: Your code does not calculate the median if one ore more numbers are equal.

Solution whitch will work, based on your Code:

    public static List<Integer> method(List<Integer> items) {
    List<Integer> list = new ArrayList<Integer>();
    int size = items.size();
    if (size == 0) {
        list = Arrays.asList();
    } else if (size == 1) {
        int first = items.get(0);
        list.add(first);
    } else if (size == 2) {
        int first = items.get(0);
        list.add(first);
        int second = items.get(1);
        list.add(second);
    } else {
        int first = items.get(0);
        int second = items.get(1);
        list.add(first);
        list.add(second);
        for (int i = 2; i < size; i++) {
            int med;
            if (items.get(i) <= items.get(i - 1) && items.get(i) >= items.get(i - 2)
                    || items.get(i) >= items.get(i - 1) && items.get(i) <= items.get(i - 2)) {
                med = items.get(i);

            } else if (items.get(i - 1) <= items.get(i) && items.get(i - 1) >= items.get(i - 2)
                    || items.get(i - 1) >= items.get(i) && items.get(i - 1) <= items.get(i - 2)) {
                med = items.get(i - 1);
            } else {
                med = items.get(i - 2);
            }
            list.add(med);
        }
    }
    return list;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.