Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
5
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@saka1029

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

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
5
Help us understand the problem. What is going on with this article?