Compare commits

...

14 Commits

Author SHA1 Message Date
wistrasy
3791e1954a
Merge d448a4e7e5 into f3f32ae3ca 2024-11-22 13:57:04 +01:00
pre-commit-ci[bot]
f3f32ae3ca
[pre-commit.ci] pre-commit autoupdate (#12385)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.3 → v0.7.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.3...v0.7.4)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-18 22:07:12 +01:00
Christian Clauss
e3bd7721c8
validate_filenames.py Shebang python for Windows (#12371) 2024-11-15 14:59:14 +01:00
pre-commit-ci[bot]
e3f3d668be
[pre-commit.ci] pre-commit autoupdate (#12370)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.2 → v0.7.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.2...v0.7.3)
- [github.com/abravalheri/validate-pyproject: v0.22 → v0.23](https://github.com/abravalheri/validate-pyproject/compare/v0.22...v0.23)

* Update sudoku_solver.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2024-11-11 21:05:50 +01:00
Wilson Strasilla
d448a4e7e5 Merge branch 'master' of https://github.com/wistrasy/Python 2024-10-17 15:40:33 -07:00
Wilson Strasilla
0ceeb2ccec Added link to leetcode. 2024-10-17 15:39:21 -07:00
pre-commit-ci[bot]
43993fb763 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-17 22:28:57 +00:00
Wilson Strasilla
85fcd387cc Fixed incorrect function name in test cases 2024-10-17 15:28:24 -07:00
Wilson Strasilla
deb7f89291 Fixed incorrect function name in test cases 2024-10-17 15:24:12 -07:00
Wilson Strasilla
d11acec5f3 Fixed a spelling issue 2024-10-17 15:16:28 -07:00
Wilson Strasilla
c64f65f6f0 Merge branch 'master' of https://github.com/wistrasy/Python 2024-10-17 14:37:30 -07:00
Wilson Strasilla
3418c8031f Formatting Fixes 2024-10-17 14:35:45 -07:00
pre-commit-ci[bot]
7fc42c79f0 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-17 21:20:01 +00:00
Wilson Strasilla
00b17c4c7c Added an algorithm to the strings sub folder which finds the smallest substring of a given string which contains all of the characters in another given string. 2024-10-17 13:03:14 -07:00
4 changed files with 93 additions and 4 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2
rev: v0.7.4
hooks:
- id: ruff
- id: ruff-format
@ -42,7 +42,7 @@ repos:
pass_filenames: false
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.22
rev: v0.23
hooks:
- id: validate-pyproject

View File

@ -172,7 +172,7 @@ def solved(values):
def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115
return open(filename).read().strip().split(sep)
def random_puzzle(assignments=17):

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!python
import os
try:

View File

@ -0,0 +1,89 @@
def min_window(search_str: str, target_letters: str) -> str:
"""
Given a string to search, and another string of target char_dict,
return the smallest substring of the search string that contains
all target char_dict.
This is somewhat modified from my solution to the problem
"Minimum Window Substring" on leetcode.
https://leetcode.com/problems/minimum-window-substring/description/
>>> min_window("Hello World", "lWl")
'llo W'
>>> min_window("Hello World", "f")
''
This solution uses a sliding window, alternating between shifting
the end of the window right until all target char_dict are contained
in the window, and shifting the start of the window right until the
window no longer contains every target character.
Time complexity: O(target_count + search_len) ->
The algorithm checks a dictionary at most twice for each character
in search_str.
Space complexity: O(search_len) ->
The primary contributor to additional space is the building of a
dictionary using the search string.
"""
target_count = len(target_letters)
search_len = len(search_str)
# Return if not possible due to string lengths.
if search_len < target_count:
return ""
# Build dictionary with counts for each letter in target_letters
char_dict = {}
for ch in target_letters:
if ch not in char_dict:
char_dict[ch] = 1
else:
char_dict[ch] += 1
# Initialize window
window_start = 0
window_end = 0
exists = False
min_window_len = search_len + 1
# Start sliding window algorithm
while window_end < search_len:
# Slide window end right until all search characters are contained
while target_count > 0 and window_end < search_len:
cur = search_str[window_end]
if cur in char_dict:
char_dict[cur] -= 1
if char_dict[cur] >= 0:
target_count -= 1
window_end += 1
temp = window_end - window_start
# Check if window is the smallest found so far
if target_count == 0 and temp < min_window_len:
min_window = [window_start, window_end]
exists = True
min_window_len = temp
# Slide window start right until a search character exits the window
while target_count == 0 and window_start < window_end:
cur = search_str[window_start]
window_start += 1
if cur in char_dict:
char_dict[cur] += 1
if char_dict[cur] > 0:
break
temp = window_end - window_start + 1
# Check if window is the smallest found so far
if temp < min_window_len and target_count == 0:
min_window = [window_start - 1, window_end]
min_window_len = temp
target_count = 1
if exists:
return search_str[min_window[0] : min_window[1]]
else:
return ""