"""Group words by length while preserving their relative order."""def group_by_length(words: list[str]) -> dict[int, list[str]]: """Group words by their length, preserving their relative order. words: list[str] - the list of words to group by length returns: a dictionary mapping each word length to the list of words of that length in their original order """ result: dict[int, list[str]] = {} word_item: str for word_item in words: length: int = len(word_item) bucket: list[str] = result.setdefault(length, []) bucket.append(word_item) return result"""Module to calculate the next weekday following a given date."""from datetime import date, timedeltaSATURDAY_WEEKDAY: int = 5def next_weekday(day: date) -> date: """Return the earliest weekday that is strictly after the given date. day: date - the starting date returns: the earliest weekday that is strictly after the given date """ candidate: date = day + timedelta(days=1) while candidate.weekday() >= SATURDAY_WEEKDAY: candidate = candidate + timedelta(days=1) return candidate"""Module for calculating the total size of Python files in a directory."""from pathlib import Pathdef total_python_bytes(directory: Path) -> int: """Calculate the total size in bytes of all .py files recursively.""" if not directory.exists(): message = f"Directory does not exist: {directory}" raise FileNotFoundError(message) total_size: int = 0 item: Path for item in directory.rglob("*.py"): total_size += item.stat().st_size return total_size"""Module providing the unique_in_order function for removing consecutive duplicates."""def unique_in_order(values: list[int]) -> list[int]: """Remove consecutive duplicate elements from a list of integers.""" result: list[int] = [] has_previous: bool = False previous: int = 0 for current in values: if not has_previous: result.append(current) previous = current has_previous = True elif current != previous: result.append(current) previous = current return result"""Module for computing vector dot products."""def dot_product(first: list[int], second: list[int]) -> int: """Compute the dot product of two integer vectors. Raises ValueError if their lengths differ. """ if len(first) != len(second): message = "Vectors must have the same length" raise ValueError(message) total: int = 0 index: int = 0 while index < len(first): total += first[index] * second[index] index += 1 return total"""Module to calculate the weekday name from an ISO 8601 date string."""from datetime import datedef weekday_name(date_text: str) -> str: """Parse an ISO 8601 date string and return the English name of its weekday. date_text: str - An ISO 8601 date string (e.g., 'YYYY-MM-DD') returns: The full English name of the weekday (e.g. 'Monday') """ try: parsed_date: date = date.fromisoformat(date_text) except ValueError as err: message = "invalid ISO 8601 date string" raise ValueError(message) from err return parsed_date.strftime("%A")"""Environment integer lookup module."""from collections.abc import Mappingdef env_int(environment: Mapping[str, str], name: str, default: int) -> int: """Read an integer from an environment mapping. Returns the default if absent or raises ValueError if invalid. """ if name not in environment: return default raw_value: str = environment[name] try: return int(raw_value) except ValueError as error: message = "Environment variable is not a valid integer." raise ValueError(message) from error"""Module to read UTF-8 lines from a text file."""from pathlib import Pathdef read_utf8_lines(path: Path) -> list[str]: """Read a UTF-8 text file and return its lines without their line endings.""" text = path.read_text(encoding="utf-8") if not text: return [] if text.endswith(("\r\n",)): lines = text[:-2].splitlines() lines.append("") return lines if text.endswith(("\n", "\r")): lines = text[:-1].splitlines() lines.append("") return lines return text.splitlines()"""Group words by length while preserving their relative order."""def group_by_length(words: list[str]) -> dict[int, list[str]]: """Group words by their length, preserving their relative order. words: list[str] - the list of words to group by length returns: a dictionary mapping each word length to the list of words of that length in their original order """ result: dict[int, list[str]] = {} word_item: str for word_item in words: length: int = len(word_item) bucket: list[str] = result.setdefault(length, []) bucket.append(word_item) return result"""Module to calculate the next weekday following a given date."""from datetime import date, timedeltaSATURDAY_WEEKDAY: int = 5def next_weekday(day: date) -> date: """Return the earliest weekday that is strictly after the given date. day: date - the starting date returns: the earliest weekday that is strictly after the given date """ candidate: date = day + timedelta(days=1) while candidate.weekday() >= SATURDAY_WEEKDAY: candidate = candidate + timedelta(days=1) return candidate"""Module for calculating the total size of Python files in a directory."""from pathlib import Pathdef total_python_bytes(directory: Path) -> int: """Calculate the total size in bytes of all .py files recursively.""" if not directory.exists(): message = f"Directory does not exist: {directory}" raise FileNotFoundError(message) total_size: int = 0 item: Path for item in directory.rglob("*.py"): total_size += item.stat().st_size return total_size"""Module providing the unique_in_order function for removing consecutive duplicates."""def unique_in_order(values: list[int]) -> list[int]: """Remove consecutive duplicate elements from a list of integers.""" result: list[int] = [] has_previous: bool = False previous: int = 0 for current in values: if not has_previous: result.append(current) previous = current has_previous = True elif current != previous: result.append(current) previous = current return result"""Module for computing vector dot products."""def dot_product(first: list[int], second: list[int]) -> int: """Compute the dot product of two integer vectors. Raises ValueError if their lengths differ. """ if len(first) != len(second): message = "Vectors must have the same length" raise ValueError(message) total: int = 0 index: int = 0 while index < len(first): total += first[index] * second[index] index += 1 return total"""Module to calculate the weekday name from an ISO 8601 date string."""from datetime import datedef weekday_name(date_text: str) -> str: """Parse an ISO 8601 date string and return the English name of its weekday. date_text: str - An ISO 8601 date string (e.g., 'YYYY-MM-DD') returns: The full English name of the weekday (e.g. 'Monday') """ try: parsed_date: date = date.fromisoformat(date_text) except ValueError as err: message = "invalid ISO 8601 date string" raise ValueError(message) from err return parsed_date.strftime("%A")"""Environment integer lookup module."""from collections.abc import Mappingdef env_int(environment: Mapping[str, str], name: str, default: int) -> int: """Read an integer from an environment mapping. Returns the default if absent or raises ValueError if invalid. """ if name not in environment: return default raw_value: str = environment[name] try: return int(raw_value) except ValueError as error: message = "Environment variable is not a valid integer." raise ValueError(message) from error"""Module to read UTF-8 lines from a text file."""from pathlib import Pathdef read_utf8_lines(path: Path) -> list[str]: """Read a UTF-8 text file and return its lines without their line endings.""" text = path.read_text(encoding="utf-8") if not text: return [] if text.endswith(("\r\n",)): lines = text[:-2].splitlines() lines.append("") return lines if text.endswith(("\n", "\r")): lines = text[:-1].splitlines() lines.append("") return lines return text.splitlines()