Skip to content

Commit db488df

Browse files
committed
Add more tasks
1 parent aa660fe commit db488df

File tree

8 files changed

+1582
-0
lines changed

8 files changed

+1582
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
level = "easy"
2+
name = "bit_cycle_shift"
3+
tags = ["string", "bits_operation"]
4+
time_to_solve_sec = 250
5+
6+
description_en = """
7+
You are given a binary string `bitstring` and an integer `shift`.
8+
9+
Cyclically shift `bitstring` to the **left** by $|shift|$ positions. If $|shift| \\geq \\text{len}(\\text{bitstring})$, use the shift value modulo the length of the string. The sign of `shift` is ignored, so `shift = 3` and `shift = -3` produce the same result.
10+
11+
Return the resulting binary string after the cyclic shift.
12+
"""
13+
14+
description_ru = """
15+
Дана двоичная строка `bitstring` и целое число `shift`.
16+
17+
Необходимо циклически сдвинуть `bitstring` влево на $|shift|$ позиций. Если $|shift| \\geq \\text{len}(\\text{bitstring})$, используйте значение сдвига по модулю длины строки. Знак `shift` игнорируется, поэтому `shift = 3` и `shift = -3` дают один и тот же результат.
18+
19+
Верните двоичную строку после циклического сдвига.
20+
"""
21+
22+
limits = """
23+
- $1 \\leq \\text{len}(\\text{bitstring}) \\leq 50$
24+
- $\\text{bitstring} \\in \\{0, 1\\}^*$
25+
- $-1000 \\leq \\text{shift} \\leq 1000$
26+
"""
27+
28+
solution = """
29+
def solution(bitstring: str, shift: int) -> str:
30+
n = len(bitstring)
31+
if n == 0 or shift == 0:
32+
return bitstring
33+
k = abs(shift) % n
34+
return bitstring[k:] + bitstring[:k]
35+
"""
36+
37+
examples = """
38+
solution("1011", -2) == "1110"
39+
solution("0101", -3) == "1010"
40+
solution("10101", 5) == "10101"
41+
solution("10010", -3) == "10100"
42+
solution("111101", 3) == "101111"
43+
solution("10000000", -9) == "00000001"
44+
"""
45+
46+
[[input_signature]]
47+
argument_name = "bitstring"
48+
[input_signature.type]
49+
name = "string"
50+
51+
[[input_signature]]
52+
argument_name = "shift"
53+
[input_signature.type]
54+
name = "integer"
55+
56+
[output_signature.type]
57+
name = "string"
58+
59+
[[asserts]]
60+
arguments = ["111101", 3]
61+
comment = "Basic left cyclic shift by 3"
62+
expected = "101111"
63+
64+
[[asserts]]
65+
arguments = ["1011", -2]
66+
comment = "Negative shift, same as left shift by 2"
67+
expected = "1110"
68+
69+
[[asserts]]
70+
arguments = ["10010", -3]
71+
comment = "Matches original task example with negative shift"
72+
expected = "10100"
73+
74+
[[asserts]]
75+
arguments = ["10101", 5]
76+
comment = "Shift equal to length leaves string unchanged"
77+
expected = "10101"
78+
79+
[[asserts]]
80+
arguments = ["0", 1]
81+
comment = "Single-character string, no visible change"
82+
expected = "0"
83+
84+
[[asserts]]
85+
arguments = ["1", -3]
86+
comment = "Single-character string with large negative shift"
87+
expected = "1"
88+
89+
[[asserts]]
90+
arguments = ["01", 1]
91+
comment = "Two characters, left shift by 1"
92+
expected = "10"
93+
94+
[[asserts]]
95+
arguments = ["01", -1]
96+
comment = "Sign is ignored, same result as +1"
97+
expected = "10"
98+
99+
[[asserts]]
100+
arguments = ["0101", 2]
101+
comment = "Period-2 pattern, shift by period returns same string"
102+
expected = "0101"
103+
104+
[[asserts]]
105+
arguments = ["0101", -3]
106+
comment = "Negative shift magnitude greater than half length"
107+
expected = "1010"
108+
109+
[[asserts]]
110+
arguments = ["000111", 1]
111+
comment = "Simple mixed zeros and ones, shift by 1"
112+
expected = "001110"
113+
114+
[[asserts]]
115+
arguments = ["000111", -4]
116+
comment = "Negative shift with magnitude larger than half length"
117+
expected = "110001"
118+
119+
[[asserts]]
120+
arguments = ["101000", 2]
121+
comment = "Shift by 2, trailing zeros move to middle"
122+
expected = "100010"
123+
124+
[[asserts]]
125+
arguments = ["101000", -2]
126+
comment = "Same magnitude, negative sign ignored"
127+
expected = "100010"
128+
129+
[[asserts]]
130+
arguments = ["11001100", 3]
131+
comment = "Length 8, shift 3"
132+
expected = "01100110"
133+
134+
[[asserts]]
135+
arguments = ["11001100", -5]
136+
comment = "Negative shift, effectively left shift by 5 mod 8"
137+
expected = "10011001"
138+
139+
[[asserts]]
140+
arguments = ["1010101", 7]
141+
comment = "Shift equal to length for odd-length alternating pattern"
142+
expected = "1010101"
143+
144+
[[asserts]]
145+
arguments = ["1010101", -14]
146+
comment = "Shift magnitude multiple of length"
147+
expected = "1010101"
148+
149+
[[asserts]]
150+
arguments = ["111000111", 4]
151+
comment = "Asymmetric pattern, non-trivial rotation"
152+
expected = "001111110"
153+
154+
[[asserts]]
155+
arguments = ["111000111", -7]
156+
comment = "Negative shift leading to different rotation of same pattern"
157+
expected = "111110001"
158+
159+
[[asserts]]
160+
arguments = ["10000000", 1]
161+
comment = "Single 1 at start moves to end"
162+
expected = "00000001"
163+
164+
[[asserts]]
165+
arguments = ["10000000", -9]
166+
comment = "Large negative shift, reduced modulo length"
167+
expected = "00000001"
168+
169+
[[asserts]]
170+
arguments = ["0101010101", 3]
171+
comment = "Long alternating pattern, shift by 3"
172+
expected = "1010101010"
173+
174+
[[asserts]]
175+
arguments = ["0101010101", -8]
176+
comment = "Shift magnitude multiple of pattern period"
177+
expected = "0101010101"
178+
179+
[[asserts]]
180+
arguments = ["11111111", 5]
181+
comment = "All ones, any shift yields same string"
182+
expected = "11111111"
183+
184+
[[asserts]]
185+
arguments = ["00000000", -123]
186+
comment = "All zeros, very large negative shift"
187+
expected = "00000000"

tasks/easy/string/bitmask.toml

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
level = "easy"
2+
name = "bitmask"
3+
tags = ["string", "bits_operation"]
4+
time_to_solve_sec = 300
5+
6+
description_en = """
7+
You are given two binary strings `bitstring` and `bitmask` of the same length.
8+
9+
Apply the logical operator `AND` to them **bit by bit**: each position in the result is `"1"` if both corresponding bits in `bitstring` and `bitmask` are `"1"`, and `"0"` otherwise.
10+
11+
Return the resulting binary string.
12+
"""
13+
14+
description_ru = """
15+
Даны две двоичные строки `bitstring` и `bitmask` одинаковой длины.
16+
17+
Необходимо применить к ним побитовый логический оператор `И`: каждый символ результата равен `"1"`, если соответствующие биты в `bitstring` и `bitmask` равны `"1"`, и `"0"` в противном случае.
18+
19+
Верните получившуюся двоичную строку.
20+
"""
21+
22+
limits = """
23+
- $1 \\leq \\text{len}(\\text{bitstring}) \\leq 60$
24+
- $\\text{len}(\\text{bitstring}) = \\text{len}(\\text{bitmask})$
25+
- $\\text{bitstring}, \\text{bitmask} \\in \\{0, 1\\}^*$
26+
"""
27+
28+
solution = """
29+
def solution(bitstring: str, bitmask: str) -> str:
30+
res = []
31+
for a, b in zip(bitstring, bitmask):
32+
res.append("1" if a == "1" and b == "1" else "0")
33+
return "".join(res)
34+
"""
35+
36+
examples = """
37+
solution("101", "110") == "100"
38+
solution("1011", "0111") == "0011"
39+
solution("1111", "0000") == "0000"
40+
solution("1010", "0101") == "0000"
41+
solution("101101", "101101") == "101101"
42+
solution("111111111", "000000001") == "000000001"
43+
"""
44+
45+
[[input_signature]]
46+
argument_name = "bitstring"
47+
[input_signature.type]
48+
name = "string"
49+
50+
[[input_signature]]
51+
argument_name = "bitmask"
52+
[input_signature.type]
53+
name = "string"
54+
55+
[output_signature.type]
56+
name = "string"
57+
58+
[[asserts]]
59+
arguments = ["101101", "101101"]
60+
comment = "Bitwise AND with itself returns the same string"
61+
expected = "101101"
62+
63+
[[asserts]]
64+
arguments = ["1011", "0111"]
65+
comment = "Example from original task, partial overlap"
66+
expected = "0011"
67+
68+
[[asserts]]
69+
arguments = ["101", "110"]
70+
comment = "Example from original task, length 3"
71+
expected = "100"
72+
73+
[[asserts]]
74+
arguments = ["0", "0"]
75+
comment = "Single zero AND zero"
76+
expected = "0"
77+
78+
[[asserts]]
79+
arguments = ["1", "1"]
80+
comment = "Single one AND one"
81+
expected = "1"
82+
83+
[[asserts]]
84+
arguments = ["1", "0"]
85+
comment = "Single one AND zero"
86+
expected = "0"
87+
88+
[[asserts]]
89+
arguments = ["0", "1"]
90+
comment = "Single zero AND one"
91+
expected = "0"
92+
93+
[[asserts]]
94+
arguments = ["1111", "0000"]
95+
comment = "All ones AND all zeros"
96+
expected = "0000"
97+
98+
[[asserts]]
99+
arguments = ["1111", "1111"]
100+
comment = "All ones AND all ones"
101+
expected = "1111"
102+
103+
[[asserts]]
104+
arguments = ["1010", "0101"]
105+
comment = "Alternating bits with complementary mask"
106+
expected = "0000"
107+
108+
[[asserts]]
109+
arguments = ["1010", "0110"]
110+
comment = "Only one overlapping position"
111+
expected = "0010"
112+
113+
[[asserts]]
114+
arguments = ["0101", "0011"]
115+
comment = "Overlap only at last bit"
116+
expected = "0001"
117+
118+
[[asserts]]
119+
arguments = ["000111", "111000"]
120+
comment = "Disjoint groups of ones"
121+
expected = "000000"
122+
123+
[[asserts]]
124+
arguments = ["000111", "000111"]
125+
comment = "Same non-trivial pattern on both"
126+
expected = "000111"
127+
128+
[[asserts]]
129+
arguments = ["101010", "111000"]
130+
comment = "Mixed overlap in the first three bits"
131+
expected = "101000"
132+
133+
[[asserts]]
134+
arguments = ["111000", "101010"]
135+
comment = "Same result with operands swapped"
136+
expected = "101000"
137+
138+
[[asserts]]
139+
arguments = ["1000001", "0111110"]
140+
comment = "Ones are in non-overlapping positions"
141+
expected = "0000000"
142+
143+
[[asserts]]
144+
arguments = ["1100110", "1010101"]
145+
comment = "Complex overlapping pattern"
146+
expected = "1000100"
147+
148+
[[asserts]]
149+
arguments = ["0101010", "0101010"]
150+
comment = "Alternating pattern AND itself"
151+
expected = "0101010"
152+
153+
[[asserts]]
154+
arguments = ["111000111", "101101011"]
155+
comment = "Longer strings with scattered ones"
156+
expected = "101000011"
157+
158+
[[asserts]]
159+
arguments = ["101111000", "111000111"]
160+
comment = "Overlap concentrated at the beginning"
161+
expected = "101000000"
162+
163+
[[asserts]]
164+
arguments = ["000000000", "111111111"]
165+
comment = "All zeros AND all ones"
166+
expected = "000000000"
167+
168+
[[asserts]]
169+
arguments = ["111111111", "000000001"]
170+
comment = "Only the last bit overlaps"
171+
expected = "000000001"
172+
173+
[[asserts]]
174+
arguments = ["000000001", "111111111"]
175+
comment = "Same as previous but swapped arguments"
176+
expected = "000000001"
177+
178+
[[asserts]]
179+
arguments = ["10100101", "00110011"]
180+
comment = "Multiple isolated overlapping ones"
181+
expected = "00100001"
182+
183+
[[asserts]]
184+
arguments = ["11011011", "11000011"]
185+
comment = "Mask clears the middle bits only"
186+
expected = "11000011"

0 commit comments

Comments
 (0)