|
| 1 | +level = "easy" |
| 2 | +name = "array_interleave" |
| 3 | +tags = ["array"] |
| 4 | +time_to_solve_sec = 200 |
| 5 | + |
| 6 | +description_en = """ |
| 7 | +Construct a new array by interleaving two input arrays. |
| 8 | +
|
| 9 | +Take the 1st element from the first array, then the 1st element from the second array, then the 2nd from the first, and so on. |
| 10 | +If one array runs out of elements, stop and return the result. |
| 11 | +""" |
| 12 | + |
| 13 | +description_ru = """ |
| 14 | +Создайте новый массив, чередуя элементы двух входных массивов. |
| 15 | +
|
| 16 | +Возьмите 1-й элемент из первого массива, затем 1-й элемент из второго, затем 2-й из первого и так далее. |
| 17 | +Если один массив заканчивается раньше другого, прекратите добавление и верните результат. |
| 18 | +""" |
| 19 | + |
| 20 | +limits = """ |
| 21 | +- $2 \\leq \\text{len}(arr1) \\leq 5$ |
| 22 | +- $2 \\leq \\text{len}(arr2) \\leq 5$ |
| 23 | +- $1 \\leq arr1_i \\leq 10$ |
| 24 | +- $1 \\leq arr2_i \\leq 10$ |
| 25 | +""" |
| 26 | + |
| 27 | +solution = """ |
| 28 | +def solution(arr1, arr2): |
| 29 | + res = [] |
| 30 | + for a, b in zip(arr1, arr2): |
| 31 | + res.append(a) |
| 32 | + res.append(b) |
| 33 | + return res |
| 34 | +""" |
| 35 | + |
| 36 | +examples = """ |
| 37 | +solution([1, 3, 5], [2, 4]) == [1, 2, 3, 4] |
| 38 | +solution([5, 1, 3], [4, 9]) == [5, 4, 1, 9] |
| 39 | +solution([1, 3, 5], [2, 4, 6]) == [1, 2, 3, 4, 5, 6] |
| 40 | +""" |
| 41 | + |
| 42 | +[[input_signature]] |
| 43 | +argument_name = "arr1" |
| 44 | +[input_signature.type] |
| 45 | +name = "array" |
| 46 | +[input_signature.type.nested] |
| 47 | +name = "integer" |
| 48 | + |
| 49 | +[[input_signature]] |
| 50 | +argument_name = "arr2" |
| 51 | +[input_signature.type] |
| 52 | +name = "array" |
| 53 | +[input_signature.type.nested] |
| 54 | +name = "integer" |
| 55 | + |
| 56 | +[output_signature.type] |
| 57 | +name = "array" |
| 58 | +[output_signature.type.nested] |
| 59 | +name = "integer" |
| 60 | + |
| 61 | +# --- TEST CASES (30 total) --- |
| 62 | + |
| 63 | +[[asserts]] |
| 64 | +arguments = [[5, 1, 3], [4, 9]] |
| 65 | +comment = "Example from original test-data" |
| 66 | +expected = [5, 4, 1, 9] |
| 67 | + |
| 68 | +[[asserts]] |
| 69 | +arguments = [[1, 3, 5], [2, 4, 6]] |
| 70 | +comment = "Equal-length arrays" |
| 71 | +expected = [1, 2, 3, 4, 5, 6] |
| 72 | + |
| 73 | +[[asserts]] |
| 74 | +arguments = [[1, 3, 5], [2, 4]] |
| 75 | +comment = "Second array shorter" |
| 76 | +expected = [1, 2, 3, 4] |
| 77 | + |
| 78 | +[[asserts]] |
| 79 | +arguments = [[2, 4], [7, 8, 9]] |
| 80 | +comment = "First array shorter" |
| 81 | +expected = [2, 7, 4, 8] |
| 82 | + |
| 83 | +[[asserts]] |
| 84 | +arguments = [[9, 8, 7], [1, 2, 3]] |
| 85 | +comment = "Reverse-ordered arrays" |
| 86 | +expected = [9, 1, 8, 2, 7, 3] |
| 87 | + |
| 88 | +[[asserts]] |
| 89 | +arguments = [[10, 10], [5, 5]] |
| 90 | +comment = "All values repeated" |
| 91 | +expected = [10, 5, 10, 5] |
| 92 | + |
| 93 | +[[asserts]] |
| 94 | +arguments = [[1, 2, 3, 4], [9, 8, 7, 6]] |
| 95 | +comment = "Two arrays of length 4" |
| 96 | +expected = [1, 9, 2, 8, 3, 7, 4, 6] |
| 97 | + |
| 98 | +[[asserts]] |
| 99 | +arguments = [[3, 3, 3], [1, 1, 1]] |
| 100 | +comment = "Uniform arrays" |
| 101 | +expected = [3, 1, 3, 1, 3, 1] |
| 102 | + |
| 103 | +[[asserts]] |
| 104 | +arguments = [[4, 5], [6, 7]] |
| 105 | +comment = "Simple two-element arrays" |
| 106 | +expected = [4, 6, 5, 7] |
| 107 | + |
| 108 | +[[asserts]] |
| 109 | +arguments = [[6, 1, 2], [3, 4, 5]] |
| 110 | +comment = "Mixed ordering" |
| 111 | +expected = [6, 3, 1, 4, 2, 5] |
| 112 | + |
| 113 | +[[asserts]] |
| 114 | +arguments = [[2, 2, 2], [9, 9]] |
| 115 | +comment = "Second array shorter with repeated values" |
| 116 | +expected = [2, 9, 2, 9] |
| 117 | + |
| 118 | +[[asserts]] |
| 119 | +arguments = [[8, 6, 4], [1, 3, 5]] |
| 120 | +comment = "Both descending and ascending" |
| 121 | +expected = [8, 1, 6, 3, 4, 5] |
| 122 | + |
| 123 | +[[asserts]] |
| 124 | +arguments = [[1, 10, 5], [9, 2, 3]] |
| 125 | +comment = "Mix of large and small values" |
| 126 | +expected = [1, 9, 10, 2, 5, 3] |
| 127 | + |
| 128 | +[[asserts]] |
| 129 | +arguments = [[7, 1], [2, 9, 8]] |
| 130 | +comment = "First array much shorter" |
| 131 | +expected = [7, 2, 1, 9] |
| 132 | + |
| 133 | +[[asserts]] |
| 134 | +arguments = [[2, 4, 6], [8, 10]] |
| 135 | +comment = "Different lengths within limits" |
| 136 | +expected = [2, 8, 4, 10] |
| 137 | + |
| 138 | +[[asserts]] |
| 139 | +arguments = [[3, 5, 7, 9], [2, 4, 6]] |
| 140 | +comment = "Second array shorter but still interleaves" |
| 141 | +expected = [3, 2, 5, 4, 7, 6] |
| 142 | + |
| 143 | +[[asserts]] |
| 144 | +arguments = [[9, 3], [4, 1]] |
| 145 | +comment = "Small arrays" |
| 146 | +expected = [9, 4, 3, 1] |
| 147 | + |
| 148 | +[[asserts]] |
| 149 | +arguments = [[1, 2, 3], [10, 9, 8]] |
| 150 | +comment = "Interleave increasing with decreasing" |
| 151 | +expected = [1, 10, 2, 9, 3, 8] |
| 152 | + |
| 153 | +[[asserts]] |
| 154 | +arguments = [[5, 6, 7], [1, 2, 3]] |
| 155 | +comment = "Standard symmetric interleave" |
| 156 | +expected = [5, 1, 6, 2, 7, 3] |
| 157 | + |
| 158 | +[[asserts]] |
| 159 | +arguments = [[8, 2], [3, 9, 1, 4]] |
| 160 | +comment = "First array shorter, second array significantly longer" |
| 161 | +expected = [8, 3, 2, 9] |
| 162 | + |
| 163 | +[[asserts]] |
| 164 | +arguments = [[1, 4, 7], [2, 5, 8]] |
| 165 | +comment = "Perfect alternation" |
| 166 | +expected = [1, 2, 4, 5, 7, 8] |
| 167 | + |
| 168 | +[[asserts]] |
| 169 | +arguments = [[9, 9, 9], [1, 2, 3]] |
| 170 | +comment = "All nines with ascending second array" |
| 171 | +expected = [9, 1, 9, 2, 9, 3] |
| 172 | + |
| 173 | +[[asserts]] |
| 174 | +arguments = [[3, 1, 4], [9, 2, 6]] |
| 175 | +comment = "Classic digits interleave" |
| 176 | +expected = [3, 9, 1, 2, 4, 6] |
| 177 | + |
| 178 | +[[asserts]] |
| 179 | +arguments = [[2, 9], [5, 7, 8]] |
| 180 | +comment = "Check correct early stop" |
| 181 | +expected = [2, 5, 9, 7] |
| 182 | + |
| 183 | +[[asserts]] |
| 184 | +arguments = [[6, 3, 1], [4, 8, 9]] |
| 185 | +comment = "Descending first array" |
| 186 | +expected = [6, 4, 3, 8, 1, 9] |
| 187 | + |
| 188 | +[[asserts]] |
| 189 | +arguments = [[5, 7, 9, 1], [2, 4, 6, 8]] |
| 190 | +comment = "Two length-4 arrays, full interleave" |
| 191 | +expected = [5, 2, 7, 4, 9, 6, 1, 8] |
| 192 | + |
| 193 | +[[asserts]] |
| 194 | +arguments = [[10, 3], [9, 1]] |
| 195 | +comment = "Values at extremes" |
| 196 | +expected = [10, 9, 3, 1] |
| 197 | + |
| 198 | +[[asserts]] |
| 199 | +arguments = [[7, 8, 9], [1, 2]] |
| 200 | +comment = "Second array ends early" |
| 201 | +expected = [7, 1, 8, 2] |
| 202 | + |
| 203 | +[[asserts]] |
| 204 | +arguments = [[2, 4, 6], [1, 3, 5, 7]] |
| 205 | +comment = "First array shorter but aligns well" |
| 206 | +expected = [2, 1, 4, 3, 6, 5] |
| 207 | + |
| 208 | +[[asserts]] |
| 209 | +arguments = [[3, 6], [9, 12]] |
| 210 | +comment = "Edge case with upper-bound values" |
| 211 | +expected = [3, 9, 6, 12] |
0 commit comments