Skip to content

Commit 19a4144

Browse files
Added descriptive variable names
1 parent d1ed34b commit 19a4144

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

bit_manipulation/sliding_window_xor.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,56 @@
33
Date : October 3, 2025
44
55
This is a pure Python implementation to calculate the cumulative XOR of all
6-
sliding windows of size k in an array generated via a linear recurrence.
6+
sliding windows of size window_size in an array generated via a linear recurrence.
77
88
The problem is :
9-
Given parameters n, k, x, multiplier, increment, modulo, generate an array:
10-
arr[0] = x
9+
Given parameters array_length, window_size, first_element, multiplier, increment, modulo, generate an array:
10+
arr[0] = first_element
1111
arr[i] = (multiplier * arr[i-1] + increment) % modulo
12-
Compute the XOR of each window of size k, and cumulatively XOR all windows.
12+
Compute the XOR of each window of size window_size, and cumulatively XOR all windows.
1313
"""
1414

1515

1616
class SlidingWindowXOR:
1717
"""
1818
Use:
1919
solver = SlidingWindowXOR()
20-
result = solver.compute(n, k, x, multiplier, increment, modulo)
20+
result = solver.compute(array_length, window_size, first_element, multiplier, increment, modulo)
2121
"""
2222

23-
def compute(self, n: int, k: int, x: int, multiplier: int,
24-
increment: int, modulo: int) -> int:
23+
def compute(
24+
self,
25+
array_length: int,
26+
window_size: int,
27+
first_element: int,
28+
multiplier: int,
29+
increment: int,
30+
modulo: int
31+
) -> int:
2532
"""
26-
Compute cumulative XOR of all sliding windows of size k.
33+
Compute cumulative XOR of all sliding windows of size window_size.
2734
2835
>>> SlidingWindowXOR().compute(5, 2, 1, 1, 1, 100)
29-
0
36+
4
3037
>>> SlidingWindowXOR().compute(2, 1, 2, 3, 4, 5)
3138
2
3239
"""
3340
# Generate the array using recurrence
34-
arr = [0] * n
35-
arr[0] = x
36-
for i in range(1, n):
41+
arr = [0] * array_length
42+
arr[0] = first_element
43+
for i in range(1, array_length):
3744
arr[i] = (multiplier * arr[i - 1] + increment) % modulo
3845

3946
x1 = 0 # XOR of current window
4047
x2 = 0 # cumulative XOR of all windows
4148
left = 0
4249

43-
for right in range(n):
50+
for right in range(array_length):
4451
x1 ^= arr[right] # include current element
45-
if right - left + 1 > k:
52+
if right - left + 1 > window_size:
4653
x1 ^= arr[left] # remove leftmost element
4754
left += 1
48-
if right - left + 1 == k:
55+
if right - left + 1 == window_size:
4956
x2 ^= x1
5057

5158
return x2
@@ -65,8 +72,8 @@ def compute(self, n: int, k: int, x: int, multiplier: int,
6572
(4, 4, 3, 1, 0, 10, 0)
6673
]
6774

68-
for idx, (n, k, x, m, inc, mod, expected) in enumerate(test_cases, 1):
69-
result = solver.compute(n, k, x, m, inc, mod)
75+
for idx, (array_length, window_size, first_element, multiplier, increment, modulo, expected) in enumerate(test_cases, 1):
76+
result = solver.compute(array_length, window_size, first_element, multiplier, increment, modulo)
7077
print(f"Testcase {idx}: Expected={expected}, Got={result}")
7178
assert result == expected, f"Testcase {idx} failed!"
7279

0 commit comments

Comments
 (0)