|
| 1 | +level = "easy" |
| 2 | +name = "apply_discount" |
| 3 | +tags = ["math", "array"] |
| 4 | +time_to_solve_sec = 200 |
| 5 | + |
| 6 | +description_en = """ |
| 7 | +You are given two equal-length arrays: prices $p$ and discounts $d$. |
| 8 | +For each index $i$, compute $\\max(p_i - d_i, 0)$ and return the sum of these values. |
| 9 | +""" |
| 10 | + |
| 11 | +description_ru = """ |
| 12 | +Даны два массива одинаковой длины: цены $p$ и скидки $d$. |
| 13 | +Для каждого индекса $i$ вычислите $\\max(p_i - d_i, 0)$ и верните сумму этих значений. |
| 14 | +""" |
| 15 | + |
| 16 | +limits = """ |
| 17 | +- $0 \\leq \\text{len}(p) = \\text{len}(d) \\leq 42$ |
| 18 | +- $0 \\leq p_i, d_i \\leq 10^6$ |
| 19 | +""" |
| 20 | + |
| 21 | +solution = """ |
| 22 | +def solution(prices, discounts): |
| 23 | + s = 0 |
| 24 | + for a,b in zip(prices, discounts): |
| 25 | + x = a - b |
| 26 | + if x > 0: |
| 27 | + s += x |
| 28 | + return s |
| 29 | +""" |
| 30 | + |
| 31 | +examples = """ |
| 32 | +solution([10, 15], [10, 10]) == 5 |
| 33 | +solution([2, 4, 6, 10], [1, 5, 7, 9]) == 2 |
| 34 | +solution([10, 20, 40, 100], [9, 18, 40, 200]) == 3 |
| 35 | +""" |
| 36 | + |
| 37 | +[[input_signature]] |
| 38 | +argument_name = "prices" |
| 39 | +[input_signature.type] |
| 40 | +name = "array" |
| 41 | +[input_signature.type.nested] |
| 42 | +name = "integer" |
| 43 | + |
| 44 | +[[input_signature]] |
| 45 | +argument_name = "discounts" |
| 46 | +[input_signature.type] |
| 47 | +name = "array" |
| 48 | +[input_signature.type.nested] |
| 49 | +name = "integer" |
| 50 | + |
| 51 | +[output_signature.type] |
| 52 | +name = "integer" |
| 53 | + |
| 54 | +[[asserts]] |
| 55 | +arguments = [[10, 15], [10, 10]] |
| 56 | +comment = "From original: 0 + 5" |
| 57 | +expected = 5 |
| 58 | + |
| 59 | +[[asserts]] |
| 60 | +arguments = [[2, 4, 6, 10], [1, 5, 7, 9]] |
| 61 | +comment = "1 + 0 + 0 + 1" |
| 62 | +expected = 2 |
| 63 | + |
| 64 | +[[asserts]] |
| 65 | +arguments = [[10, 20, 40, 100], [9, 18, 40, 200]] |
| 66 | +comment = "1 + 2 + 0 + 0" |
| 67 | +expected = 3 |
| 68 | + |
| 69 | +[[asserts]] |
| 70 | +arguments = [[10, 20, 40, 100], [1, 2, 30, 50]] |
| 71 | +comment = "9 + 18 + 10 + 50" |
| 72 | +expected = 87 |
| 73 | + |
| 74 | +[[asserts]] |
| 75 | +arguments = [[0], [0]] |
| 76 | +comment = "Zero case" |
| 77 | +expected = 0 |
| 78 | + |
| 79 | +[[asserts]] |
| 80 | +arguments = [[0, 0, 0], [1, 2, 3]] |
| 81 | +comment = "All clamp to zero" |
| 82 | +expected = 0 |
| 83 | + |
| 84 | +[[asserts]] |
| 85 | +arguments = [[100], [0]] |
| 86 | +comment = "No discount" |
| 87 | +expected = 100 |
| 88 | + |
| 89 | +[[asserts]] |
| 90 | +arguments = [[100], [100]] |
| 91 | +comment = "Exact cancellation" |
| 92 | +expected = 0 |
| 93 | + |
| 94 | +[[asserts]] |
| 95 | +arguments = [[100], [200]] |
| 96 | +comment = "Negative intermediate becomes zero" |
| 97 | +expected = 0 |
| 98 | + |
| 99 | +[[asserts]] |
| 100 | +arguments = [[1000000], [1000000]] |
| 101 | +comment = "Upper bound cancel" |
| 102 | +expected = 0 |
| 103 | + |
| 104 | +[[asserts]] |
| 105 | +arguments = [[1000000], [0]] |
| 106 | +comment = "Upper bound no discount" |
| 107 | +expected = 1000000 |
| 108 | + |
| 109 | +[[asserts]] |
| 110 | +arguments = [[500000, 500000], [499999, 1]] |
| 111 | +comment = "Both positive after discount" |
| 112 | +expected = 500_000 |
| 113 | + |
| 114 | +[[asserts]] |
| 115 | +arguments = [[10, 0, 10, 0], [9, 1, 11, 2]] |
| 116 | +comment = "Some negatives clamp to zero" |
| 117 | +expected = 1 |
| 118 | + |
| 119 | +[[asserts]] |
| 120 | +arguments = [[3, 3, 3], [1, 1, 10]] |
| 121 | +comment = "Only first two contribute" |
| 122 | +expected = 4 |
| 123 | + |
| 124 | +[[asserts]] |
| 125 | +arguments = [[7, 14, 21], [7, 7, 7]] |
| 126 | +comment = "Half off scenario; last two positive" |
| 127 | +expected = 21 |
| 128 | + |
| 129 | +[[asserts]] |
| 130 | +arguments = [[9, 8, 7, 6, 5], [10, 10, 10, 10, 10]] |
| 131 | +comment = "All clamp to zero (discounts larger)" |
| 132 | +expected = 0 |
| 133 | + |
| 134 | +[[asserts]] |
| 135 | +arguments = [[100, 200, 300, 400], [50, 50, 50, 50]] |
| 136 | +comment = "Uniform discount" |
| 137 | +expected = 800 |
| 138 | + |
| 139 | +[[asserts]] |
| 140 | +arguments = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] |
| 141 | +comment = "Staircase" |
| 142 | +expected = 10 |
| 143 | + |
| 144 | +[[asserts]] |
| 145 | +arguments = [[10], [11]] |
| 146 | +comment = "Single, clamps to zero" |
| 147 | +expected = 0 |
| 148 | + |
| 149 | +[[asserts]] |
| 150 | +arguments = [[11], [10]] |
| 151 | +comment = "Single, positive" |
| 152 | +expected = 1 |
| 153 | + |
| 154 | +[[asserts]] |
| 155 | +arguments = [[12345, 67890, 13579, 24680], [54321, 1000, 13579, 20000]] |
| 156 | +comment = "Mix with zeroes and positives" |
| 157 | +expected = 71570 |
| 158 | + |
| 159 | +[[asserts]] |
| 160 | +arguments = [[999999, 1, 500000], [1, 2, 600000]] |
| 161 | +comment = "Edge behaviors mixed" |
| 162 | +expected = 999998 |
| 163 | + |
| 164 | +[[asserts]] |
| 165 | +arguments = [[42, 42, 42, 42, 42], [41, 42, 43, 0, 1000]] |
| 166 | +comment = "Varied outcomes" |
| 167 | +expected = 43 |
| 168 | + |
| 169 | +[[asserts]] |
| 170 | +arguments = [[0, 100, 0, 100], [0, 0, 100, 100]] |
| 171 | +comment = "Zeros and equals" |
| 172 | +expected = 100 |
| 173 | + |
| 174 | +[[asserts]] |
| 175 | +arguments = [[], []] |
| 176 | +comment = "Empty arrays" |
| 177 | +expected = 0 |
0 commit comments