1313public class Zip {
1414
1515 private static List <Integer > zip (List <Integer > a , List <Integer > b , int n ) {
16- ArrayList <Integer > integers = new ArrayList <>();
16+ int min = Math .min (n , Math .min (a .size (), b .size ()));
17+ ArrayList <Integer > integers = new ArrayList <>(min * 2 );
1718
18- for (int i = 0 ; i < n ; i ++) {
19+ for (int i = 0 ; i < min ; i ++) {
1920 integers .add (a .get (i ));
2021 integers .add (b .get (i ));
2122 }
@@ -33,21 +34,19 @@ public static void main(String[] args) throws IOException {
3334 }
3435
3536 private static List <Integer > readList (BufferedReader reader ) throws IOException {
36- return Arrays .stream (reader .readLine ().split (" " ))
37+ return Arrays .stream (reader .readLine ().split ("\\ s+ " ))
3738 .map (Integer ::parseInt )
3839 .collect (Collectors .toList ());
3940 }
4041
4142 private static <T > void printList (List <T > list , Writer writer ) {
42- list .forEach (elem -> {
43- try {
44- writer .write (String .valueOf (elem ));
45- writer .write (" " );
46- } catch (IOException ignored ) {
47-
48- }
49- }
50- );
43+ for (T elem : list ) {
44+ try {
45+ writer .write (String .valueOf (elem ));
46+ writer .write (" " );
47+ } catch (IOException ignored ) {
48+ }
49+ }
5150 }
5251
5352 @ Test
@@ -59,4 +58,14 @@ void test1() {
5958 List <Integer > expected = List .of (1 , 7 , 5 , 8 , 6 , 9 );
6059 assertEquals (expected , actualZip );
6160 }
61+
62+ @ Test
63+ void test2 () {
64+ List <Integer > integers1 = List .of (1 , 5 , 6 );
65+ List <Integer > integers2 = List .of (7 , 8 , 9 );
66+ List <Integer > actualZip = zip (integers1 , integers2 , 0 );
67+
68+ List <Integer > expected = List .of ();
69+ assertEquals (expected , actualZip );
70+ }
6271}
0 commit comments