|
| 1 | +level = "easy" |
| 2 | +name = "bulls_and_cows" |
| 3 | +tags = ["stdlib"] |
| 4 | +time_to_solve_sec = 300 |
| 5 | + |
| 6 | +description_en = """ |
| 7 | +You are given two 4-digit integers: `secret` and `guess`. |
| 8 | +
|
| 9 | +For each position: |
| 10 | +
|
| 11 | +- If the digit in `guess` is the **same** as in `secret` and in the **same position**, it counts as a **bull**. |
| 12 | +- If the digit appears in `secret` but in a **different position**, it counts as a **cow**. |
| 13 | +
|
| 14 | +Return an array `[bulls, cows]` with the number of bulls and cows for this pair. |
| 15 | +
|
| 16 | +Although in the classic game all digits are different, your function must correctly handle **any** 4-digit numbers, including ones with repeated digits. |
| 17 | +""" |
| 18 | + |
| 19 | +description_ru = """ |
| 20 | +Даны два четырёхзначных числа: `secret` и `guess`. |
| 21 | +
|
| 22 | +Для каждой позиции: |
| 23 | +
|
| 24 | +- Если цифра в `guess` совпадает с цифрой в `secret` и стоит на **том же месте**, это **бык**. |
| 25 | +- Если цифра присутствует в `secret`, но стоит на **другом месте**, это **корова**. |
| 26 | +
|
| 27 | +Верните массив `[bulls, cows]` с количеством быков и коров для этой пары. |
| 28 | +
|
| 29 | +Хотя в классической игре все цифры различны, ваша функция должна корректно обрабатывать **любые** четырёхзначные числа, включая числа с повторяющимися цифрами. |
| 30 | +""" |
| 31 | + |
| 32 | +limits = """ |
| 33 | +- $1000 \\leq secret \\leq 9999$ |
| 34 | +- $1000 \\leq guess \\leq 9999$ |
| 35 | +""" |
| 36 | + |
| 37 | +solution = """ |
| 38 | +from collections import Counter |
| 39 | +from typing import List |
| 40 | +
|
| 41 | +def solution(secret: int, guess: int) -> List[int]: |
| 42 | + s = str(secret) |
| 43 | + g = str(guess) |
| 44 | + bulls = sum(1 for i in range(4) if s[i] == g[i]) |
| 45 | + cs, cg = Counter(s), Counter(g) |
| 46 | + cows = sum(min(cs[d], cg[d]) for d in cs) - bulls |
| 47 | + return [bulls, cows] |
| 48 | +""" |
| 49 | + |
| 50 | +examples = """ |
| 51 | +solution(1234, 5678) == [0, 0] |
| 52 | +solution(1234, 4321) == [0, 4] |
| 53 | +solution(4182, 4273) == [1, 1] |
| 54 | +solution(4271, 1234) == [1, 2] |
| 55 | +solution(4271, 4271) == [4, 0] |
| 56 | +solution(4271, 5682) == [0, 1] |
| 57 | +""" |
| 58 | + |
| 59 | +[[input_signature]] |
| 60 | +argument_name = "secret" |
| 61 | +[input_signature.type] |
| 62 | +name = "integer" |
| 63 | + |
| 64 | +[[input_signature]] |
| 65 | +argument_name = "guess" |
| 66 | +[input_signature.type] |
| 67 | +name = "integer" |
| 68 | + |
| 69 | +[output_signature.type] |
| 70 | +name = "array" |
| 71 | +nested = { name = "integer" } |
| 72 | + |
| 73 | +[[asserts]] |
| 74 | +arguments = [4271, 1234] |
| 75 | +comment = "Given example: one bull (2) and two cows (1 and 4)" |
| 76 | +expected = [1, 2] |
| 77 | + |
| 78 | +[[asserts]] |
| 79 | +arguments = [4271, 5682] |
| 80 | +comment = "Given example: one cow (2), no bulls" |
| 81 | +expected = [0, 1] |
| 82 | + |
| 83 | +[[asserts]] |
| 84 | +arguments = [4271, 4271] |
| 85 | +comment = "Given example: all digits correct and in place" |
| 86 | +expected = [4, 0] |
| 87 | + |
| 88 | +[[asserts]] |
| 89 | +arguments = [4182, 4273] |
| 90 | +comment = "Given example: one bull, one cow" |
| 91 | +expected = [1, 1] |
| 92 | + |
| 93 | +[[asserts]] |
| 94 | +arguments = [1234, 5678] |
| 95 | +comment = "No common digits" |
| 96 | +expected = [0, 0] |
| 97 | + |
| 98 | +[[asserts]] |
| 99 | +arguments = [1234, 4321] |
| 100 | +comment = "All digits present but all in wrong positions" |
| 101 | +expected = [0, 4] |
| 102 | + |
| 103 | +[[asserts]] |
| 104 | +arguments = [1234, 1278] |
| 105 | +comment = "Two bulls, no cows" |
| 106 | +expected = [2, 0] |
| 107 | + |
| 108 | +[[asserts]] |
| 109 | +arguments = [1234, 1782] |
| 110 | +comment = "One bull, one cow" |
| 111 | +expected = [1, 1] |
| 112 | + |
| 113 | +[[asserts]] |
| 114 | +arguments = [9876, 6789] |
| 115 | +comment = "All digits present but all displaced" |
| 116 | +expected = [0, 4] |
| 117 | + |
| 118 | +[[asserts]] |
| 119 | +arguments = [1111, 1111] |
| 120 | +comment = "All digits equal with repeats" |
| 121 | +expected = [4, 0] |
| 122 | + |
| 123 | +[[asserts]] |
| 124 | +arguments = [1122, 2211] |
| 125 | +comment = "All digits present but all in wrong positions with repeats" |
| 126 | +expected = [0, 4] |
| 127 | + |
| 128 | +[[asserts]] |
| 129 | +arguments = [9090, 9009] |
| 130 | +comment = "Two bulls, two cows with repeated digits" |
| 131 | +expected = [2, 2] |
| 132 | + |
| 133 | +[[asserts]] |
| 134 | +arguments = [1023, 1023] |
| 135 | +comment = "Exact match with a zero digit" |
| 136 | +expected = [4, 0] |
| 137 | + |
| 138 | +[[asserts]] |
| 139 | +arguments = [1023, 1302] |
| 140 | +comment = "One bull, three cows, all digits shared" |
| 141 | +expected = [1, 3] |
| 142 | + |
| 143 | +[[asserts]] |
| 144 | +arguments = [9087, 7890] |
| 145 | +comment = "All digits present, all displaced" |
| 146 | +expected = [0, 4] |
| 147 | + |
| 148 | +[[asserts]] |
| 149 | +arguments = [2468, 8642] |
| 150 | +comment = "Even digits, reversed order blocks" |
| 151 | +expected = [0, 4] |
| 152 | + |
| 153 | +[[asserts]] |
| 154 | +arguments = [1357, 1357] |
| 155 | +comment = "Odd digits, all correct" |
| 156 | +expected = [4, 0] |
| 157 | + |
| 158 | +[[asserts]] |
| 159 | +arguments = [1357, 7531] |
| 160 | +comment = "Same digits, rotated positions" |
| 161 | +expected = [0, 4] |
| 162 | + |
| 163 | +[[asserts]] |
| 164 | +arguments = [1234, 1294] |
| 165 | +comment = "Three digits correct and in place" |
| 166 | +expected = [3, 0] |
| 167 | + |
| 168 | +[[asserts]] |
| 169 | +arguments = [1234, 1203] |
| 170 | +comment = "Two bulls, one cow" |
| 171 | +expected = [2, 1] |
| 172 | + |
| 173 | +[[asserts]] |
| 174 | +arguments = [1234, 9834] |
| 175 | +comment = "Two bulls, remaining digits different" |
| 176 | +expected = [2, 0] |
| 177 | + |
| 178 | +[[asserts]] |
| 179 | +arguments = [5678, 7618] |
| 180 | +comment = "Two bulls and one additional shared digit" |
| 181 | +expected = [2, 1] |
| 182 | + |
| 183 | +[[asserts]] |
| 184 | +arguments = [5678, 5768] |
| 185 | +comment = "Two bulls and two cows" |
| 186 | +expected = [2, 2] |
| 187 | + |
| 188 | +[[asserts]] |
| 189 | +arguments = [5678, 5670] |
| 190 | +comment = "Three digits correct, last digit different" |
| 191 | +expected = [3, 0] |
| 192 | + |
| 193 | +[[asserts]] |
| 194 | +arguments = [5678, 8567] |
| 195 | +comment = "All digits present but permuted" |
| 196 | +expected = [0, 4] |
| 197 | + |
| 198 | +[[asserts]] |
| 199 | +arguments = [1023, 9876] |
| 200 | +comment = "No common digits between secret and guess" |
| 201 | +expected = [0, 0] |
0 commit comments