Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""String utility functions."""


def reverse_words(sentence: str) -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docstring: reverse_words is a public function and must have a docstring describing what it does, its parameters, and its return value (org guideline).

def reverse_words(sentence: str) -> str:
    """Reverse the order of words in a sentence.

    Args:
        sentence: The input string whose words will be reversed.

    Returns:
        A string with the words in reversed order.
    """
  • Mark as noise

return " ".join(sentence.split()[::-1])


def count_vowels(text: str) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docstring: count_vowels is a public function and must have a docstring (org guideline). Describe that it counts ASCII vowels (a, e, i, o, u) case-insensitively, so callers know accented vowels are not counted.

  • Mark as noise

return sum(1 for c in text.lower() if c in "aeiou")


def truncate(text: str, max_length: int, suffix: str = "...") -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docstring: truncate is a public function and must have a docstring (org guideline). Document the suffix default, the max_length constraint relative to suffix length, and the return value.

  • Mark as noise

if len(text) <= max_length:
return text
return text[: max_length - len(suffix)] + suffix
Comment on lines +12 to +15

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: When max_length is smaller than len(suffix), max_length - len(suffix) is negative. Python interprets a negative slice index as an offset from the end of the string, so text[:-1] silently returns nearly the full string and the result exceeds max_length.

Example: truncate("hello", 2, "...")max_length - len(suffix) = -1 → "hell" + "..." = "hell..." (7 chars), not 2.

Add a guard at the top of the function:

Suggested change
def truncate(text: str, max_length: int, suffix: str = "...") -> str:
if len(text) <= max_length:
return text
return text[: max_length - len(suffix)] + suffix
def truncate(text: str, max_length: int, suffix: str = "...") -> str:
if max_length < len(suffix):
raise ValueError(f"max_length ({max_length}) must be >= len(suffix) ({len(suffix)})")
if len(text) <= max_length:
return text
return text[: max_length - len(suffix)] + suffix
  • Mark as noise

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage: No test covers the edge case where max_length < len(suffix). Without a test, the silent wrong-output bug described above will go undetected. A unit test asserting that truncate("hello", 2, "...") raises ValueError (after the fix) would catch regressions here.

  • Mark as noise



def to_snake_case(text: str) -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docstring: to_snake_case is a public function and must have a docstring (org guideline). Worth noting that consecutive uppercase sequences (e.g. HTMLParser) are treated character-by-character (h_t_m_l_parser), so callers are not surprised by that behaviour.

  • Mark as noise

result = []
for i, char in enumerate(text):
if char.isupper() and i > 0:
result.append("_")
result.append(char.lower())
return "".join(result)


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docstring: is_palindrome is a public function and must have a docstring (org guideline). Document that non-alphanumeric characters are stripped and comparison is case-insensitive.

  • Mark as noise

def is_palindrome(text: str) -> bool:
cleaned = "".join(c.lower() for c in text if c.isalnum())
return cleaned == cleaned[::-1]
Loading