言語処理100本ノック 第1章: 準備運動 02, 03
ちなみに、jdk9 の jshell をつかってます。 https://jdk9.java.net/download/
02. 「パトカー」+「タクシー」=「パタトクカシーー」
「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
java
String str1 = "パトカー"; String str2 = "タクシー"; Iterator<String> it1 = List.of(str1.split("")).iterator(); Iterator<String> it2 = List.of(str2.split("")).iterator(); ArrayList<String> sa = new ArrayList<>(); while (it1.hasNext() && it2.hasNext()){ sa.add(it1.next()); sa.add(it2.next()); } System.out.println(String.join("", sa));
Streams.zip はどこへ行ったんだろう?
String str1 = "パトカー"; String str2 = "タクシー"; ArrayList<String> sa = new ArrayList<>(); Streams.zip(List.of(str1.split("")).stream(), List.of(str2.split("")).stream(), (c1, c2) -> sa.add(c1), sa.add(c2));
こんなふうになるつもりだったのだが...
03. 円周率
"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
java
String sample = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."; Integer[] ia = Arrays.stream(sample.split(" ")).map(e -> e.length()).toArray(e -> new Integer[e]);
toArray がなんかムズいな。