File: goat-latin/solution.py

Date: 2026-06-06

Time: 16:56

goat-latin/solution.py

Purpose

This file is a self-contained LeetCode solution for problem 824: Goat Latin. It owns the transformation logic and its own test suite — the single function togoatlatin is the entire public API.

Key Components

togoatlatin(sentence: str) -> str — Applies three rules to each word in the sentence:

1. If a word starts with a vowel (case-insensitive), append "ma".

2. If it starts with a consonant, move the first character to the end, then append "ma".

3. Append one "a" per the word's 1-based position index.

The vowel set is "aeiouAEIOU" — stored as a set for O(1) membership testing.

TestToGoatLatin — 8 unit tests covering both LeetCode examples, single-word inputs, single-character edge cases, and all-vowel-start sentences.

Patterns

Dependencies

Imports: Only unittest from stdlib. No external or project-internal dependencies.

Imported by: The massive importedby list is misleading — those are testsolution.py files across *other* problems that happen to share a test runner or import pattern. The real direct consumer is goat-latin/test_solution.py.

Flow

1. Split sentence on whitespace into words.

2. For each word at 1-based index i:

3. Join transformed words with spaces.

Data transformation: str → list[str] → str. Single pass, no intermediate data structures beyond the result list.

Invariants

Error Handling

None. The function trusts its input matches the LeetCode constraints (non-empty sentence of English letters and single spaces). Passing an empty string would produce an empty string; passing a word with no characters would raise an IndexError on word[0].

Topics to Explore

Beliefs