Skip to content

Commit d5e8e64

Browse files
Merge branch 'master' into docs/improve-bubble-sort-doc
2 parents 5c06696 + 8106aea commit d5e8e64

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

ciphers/caesar_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
4545
And our shift is ``2``
4646
4747
We can then encode the message, one letter at a time. ``H`` would become ``J``,
48-
since ``J`` is two letters away, and so on. If the shift is ever two large, or
48+
since ``J`` is two letters away, and so on. If the shift is ever too large, or
4949
our letter is at the end of the alphabet, we just start at the beginning
5050
(``Z`` would shift to ``a`` then ``b`` and so on).
5151

other/sliding_window_maximum.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from collections import deque
2+
3+
4+
def sliding_window_maximum(numbers: list[int], window_size: int) -> list[int]:
5+
"""
6+
Return a list containing the maximum of each sliding window of size window_size.
7+
8+
This implementation uses a monotonic deque to achieve O(n) time complexity.
9+
10+
Args:
11+
numbers: List of integers representing the input array.
12+
window_size: Size of the sliding window (must be positive).
13+
14+
Returns:
15+
List of maximum values for each valid window.
16+
17+
Raises:
18+
ValueError: If window_size is not a positive integer.
19+
20+
Time Complexity: O(n) - each element is added and removed at most once
21+
Space Complexity: O(k) - deque stores at most window_size indices
22+
23+
Examples:
24+
>>> sliding_window_maximum([1, 3, -1, -3, 5, 3, 6, 7], 3)
25+
[3, 3, 5, 5, 6, 7]
26+
>>> sliding_window_maximum([9, 11], 2)
27+
[11]
28+
>>> sliding_window_maximum([], 3)
29+
[]
30+
>>> sliding_window_maximum([4, 2, 12, 3], 1)
31+
[4, 2, 12, 3]
32+
>>> sliding_window_maximum([1], 1)
33+
[1]
34+
"""
35+
if window_size <= 0:
36+
raise ValueError("Window size must be a positive integer")
37+
if not numbers:
38+
return []
39+
40+
result: list[int] = []
41+
index_deque: deque[int] = deque()
42+
43+
for current_index, current_value in enumerate(numbers):
44+
# Remove the element which is out of this window
45+
if index_deque and index_deque[0] == current_index - window_size:
46+
index_deque.popleft()
47+
48+
# Remove useless elements (smaller than current) from back
49+
while index_deque and numbers[index_deque[-1]] < current_value:
50+
index_deque.pop()
51+
52+
index_deque.append(current_index)
53+
54+
# Start adding to result once we have a full window
55+
if current_index >= window_size - 1:
56+
result.append(numbers[index_deque[0]])
57+
58+
return result

strings/reverse_words.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
def reverse_words(input_str: str) -> str:
2-
"""
3-
Reverses words in a given string
1+
def reverse_words(sentence: str) -> str:
2+
"""Reverse the order of words in a given string.
3+
4+
Extra whitespace between words is ignored.
5+
46
>>> reverse_words("I love Python")
57
'Python love I'
68
>>> reverse_words("I Love Python")
79
'Python Love I'
810
"""
9-
return " ".join(input_str.split()[::-1])
11+
return " ".join(sentence.split()[::-1])
1012

1113

1214
if __name__ == "__main__":

0 commit comments

Comments
 (0)