Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions tasks/easy/stdlib/bulls_and_cows.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ You are given two 4-digit integers: `secret` and `guess`.

For each position:

- If the digit in `guess` is the **same** as in `secret` and in the **same position**, it counts as a **bull**.
- If the digit appears in `secret` but in a **different position**, it counts as a **cow**.
- If a digit from `guess` matches the digit in `secret` at the **same position**, then it counts as a **bull**.
- If a digit from `guess` appears in `secret`, but at the **another position**, then it counts as a **cow**. Note that no more than one digit from `guess` can be considered a **cow** in relation to the same digit in `secret`.

Return an array `[bulls, cows]` with the number of bulls and cows for this pair.

Although in the classic game all digits are different, your function must correctly handle **any** 4-digit numbers, including ones with repeated digits.
Although all the digits in `secret` should be different in the classic game, your function must correctly handle **any** 4-digit numbers, including numbers with repeating digits (this is why the description of **cows** was specified).
"""

description_ru = """
Даны два четырёхзначных числа: `secret` и `guess`.

Для каждой позиции:

- Если цифра в `guess` совпадает с цифрой в `secret` и стоит на **том же месте**, это **бык**.
- Если цифра присутствует в `secret`, но стоит на **другом месте**, это **корова**.
- Если цифра из `guess` совпадает с цифрой в `secret` на **том же месте**, то это **бык**.
- Если цифра из `guess` присутствует в `secret`, но стоит на **другом месте**, то это **корова**. При этом по отношению к одной и той же цифре в `secret` **коровой** может считаться не более одной цифры из `guess`.

Верните массив `[bulls, cows]` с количеством быков и коров для этой пары.

Хотя в классической игре все цифры различны, ваша функция должна корректно обрабатывать **любые** четырёхзначные числа, включая числа с повторяющимися цифрами.
Хотя в классической игре все цифры в `secret` должны быть различны, ваша функция должна корректно обрабатывать **любые** четырёхзначные числа, включая числа с повторяющимися цифрами (именно для этого было дано уточнение в описании **коров**).
"""

limits = """
Expand All @@ -39,9 +39,8 @@ from collections import Counter
from typing import List

def solution(secret: int, guess: int) -> List[int]:
s = str(secret)
g = str(guess)
bulls = sum(1 for i in range(4) if s[i] == g[i])
s, g = str(secret), str(guess)
bulls = sum(s[i] == g[i] for i in range(4))
cs, cg = Counter(s), Counter(g)
cows = sum(min(cs[d], cg[d]) for d in cs) - bulls
return [bulls, cows]
Expand Down Expand Up @@ -199,3 +198,23 @@ expected = [0, 4]
arguments = [1023, 9876]
comment = "No common digits between secret and guess"
expected = [0, 0]

[[asserts]]
arguments = [7792, 8997]
comment = "One bull, one cow"
expected = [1, 1]

[[asserts]]
arguments = [4020, 5447]
comment = "One 4 in secret, two 4's in guess"
expected = [0, 1]

[[asserts]]
arguments = [2026, 7217]
comment = "Two 2's in secret, one 2 in guess"
expected = [0, 1]

[[asserts]]
arguments = [9429, 1999]
comment = "One 9 is bull, one 9 is cow, one 9 is nothing"
expected = [1, 1]
Loading