Help us understand the problem. What is going on with this article?

1時間以内に解けなければプログラマ失格となってしまう5つの問題の問題5

More than 3 years have passed since last update.

1時間以内に解けなければプログラマ失格となってしまう5つの問題が話題に」の問題5をJava8で解いてみました。
Streamとforループの組み合わせが気持ち悪いですが、とりあえず動きます。

static <T> List<T> cons(T head, List<T> tail) {
    List<T> list = new ArrayList<>();
    list.add(head);
    list.addAll(tail);
    return list;
}
static Stream<List<String>> split(String s) {
    if (s.length() <= 0) return Stream.of(Arrays.asList());
    Stream<List<String>> b = Stream.empty();
    for (int i = 1, size = s.length(); i <= size; ++i) {
        String head = s.substring(0, i);
        b = Stream.concat(b,
            split(s.substring(i))
                .flatMap(l -> Stream.of(cons(head, l), cons("-" + head, l))));
    }
    return b;
}

@Test
public void q5() {
    split("123456789")
        .filter(l -> l.stream()
            .map(s -> Integer.parseInt(s))
            .reduce(0, (a, b) -> a + b) == 100)
        .forEach(l -> System.out.println(l));
}

ストリームではなくリストのリストを返す場合は以下のようになります。

static List<List<String>> splitList(String s) {
    if (s.length() <= 0) return Arrays.asList(Arrays.asList());
    List<List<String>> b = new ArrayList<>();
    for (int i = 1, size = s.length(); i <= size; ++i) {
        String head = s.substring(0, i);
        for (List<String> l : splitList(s.substring(i))) {
            b.add(cons(head, l));
            b.add(cons("-" + head, l));
        }
    }
    return b;
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away