Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Strain in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement the keep and discard operation on collections. Given a collection and a predicate on the collection's elements, keep returns a new collection containing those elements where the predicate is true, while discard returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

  • 1, 2, 3, 4, 5

And the predicate:

  • is the number even?

Then your keep operation should produce:

  • 2, 4

While your discard operation should produce:

  • 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called keep and discard, or they may need different names in order to not clash with existing functions or concepts in your language.

Restrictions

Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

Conversation with James Edward Gray II https://twitter.com/jeg2

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

StrainTest.java

import org.junit.Assert;
import org.junit.Test;
import org.junit.Ignore;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class StrainTest {


    @Test
    public void emptyKeep() {
        List<Integer> input = new LinkedList<>();
        List<Integer> expectedOutput = new LinkedList<>();
        Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x < 10));
    }

    @Ignore("Remove to run test")
    @Test
    public void keepEverything() {
        List<Integer> input = Arrays.asList(1, 2, 3);
        List<Integer> expectedOutput = Arrays.asList(1, 2, 3);
        Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x < 10));
    }

    @Ignore("Remove to run test")
    @Test
    public void keepFirstAndLast() {
        List<Integer> input = Arrays.asList(1, 2, 3);
        List<Integer> expectedOutput = Arrays.asList(1, 3);
        Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x % 2 != 0));
    }

    @Ignore("Remove to run test")
    @Test
    public void keepNeitherFirstNorLast() {
        List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> expectedOutput = Arrays.asList(2, 4);
        Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x % 2 == 0));
    }

    @Ignore("Remove to run test")
    @Test
    public void KeepStrings() {
        List<String> words = Arrays
                .asList("apple zebra banana zombies cherimoya zelot".split(" "));
        List<String> expectedOutput = Arrays.asList("zebra", "zombies", "zelot");
        Assert.assertEquals(expectedOutput,
                Strain.keep(words, x -> x.startsWith("z")));
    }

    @Ignore("Remove to run test")
    @Test
    public void KeepArrays() {
        List<List<Integer>> actual = Arrays.asList(
                Arrays.asList(1, 2, 3),
                Arrays.asList(5, 5, 5),
                Arrays.asList(5, 1, 2),
                Arrays.asList(2, 1, 2),
                Arrays.asList(1, 5, 2),
                Arrays.asList(2, 2, 1),
                Arrays.asList(1, 2, 5));
        List<List<Integer>> expectedOutput = Arrays.asList(
                Arrays.asList(5, 5, 5),
                Arrays.asList(5, 1, 2),
                Arrays.asList(1, 5, 2),
                Arrays.asList(1, 2, 5));
        Assert.assertEquals(expectedOutput,
                Strain.keep(actual, col -> col.contains(5)));
    }

    @Ignore("Remove to run test")
    @Test
    public void emptyDiscard() {
        List<Integer> input = new LinkedList<>();
        List<Integer> expectedOutput = new LinkedList<>();
        Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x < 10));
    }

    @Ignore("Remove to run test")
    @Test
    public void discardNothing() {
        List<Integer> input = Arrays.asList(1, 2, 3);
        List<Integer> expectedOutput = Arrays.asList(1, 2, 3);
        Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x > 10));
    }

    @Ignore("Remove to run test")
    @Test
    public void discardFirstAndLast() {
        List<Integer> input = Arrays.asList(1, 2, 3);
        List<Integer> expectedOutput = Arrays.asList(2);
        Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x % 2 != 0));

    }

    @Ignore("Remove to run test")
    @Test
    public void discardNeitherFirstNorLast() {
        List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> expectedOutput = Arrays.asList(1, 3, 5);
        Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x % 2 == 0));
    }

    @Ignore("Remove to run test")
    @Test
    public void discardStrings() {
        List<String> words = Arrays
                .asList("apple zebra banana zombies cherimoya zelot".split(" "));
        List<String> expectedOutput = Arrays.asList("apple", "banana", "cherimoya");
        Assert.assertEquals(expectedOutput,
                Strain.discard(words, x -> x.startsWith("z")));
    }

    @Ignore("Remove to run test")
    @Test
    public void discardArrays() {
        List<List<Integer>> actual = Arrays.asList(
                Arrays.asList(1, 2, 3),
                Arrays.asList(5, 5, 5),
                Arrays.asList(5, 1, 2),
                Arrays.asList(2, 1, 2),
                Arrays.asList(1, 5, 2),
                Arrays.asList(2, 2, 1),
                Arrays.asList(1, 2, 5));
        List<List<Integer>> expectedOutput = Arrays.asList(
                Arrays.asList(1, 2, 3),
                Arrays.asList(2, 1, 2),
                Arrays.asList(2, 2, 1));
        Assert.assertEquals(expectedOutput,
                Strain.discard(actual, col -> col.contains(5)));
    }
}
import java.util.List;
import java.util.ArrayList;
import java.util.function.Predicate;

public class Strain {

  public static <T> List<T> keep(List<T> list, Predicate<T> p) {
    List<T> results = new ArrayList<>();

    for (T item : list) {
      if (p.test(item)) {
        results.add(item);
      }
    }

    return results;
  }

  public static <T> List<T> discard(List<T> list, Predicate<T> p) {
    List<T> results = new ArrayList<>();

    for (T item : list) {
      if (!p.test(item)) {
        results.add(item);
      }
    }

    return results;
  }

}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?