Hour of Python Trinket Number of Things挑战功能扩展技术咨询
Great job getting the basic pluralization logic working for the challenge! Let’s dive into practical, actionable extensions you can add to make your solution more robust and natural. First, let’s recap your core implementation to set the baseline:
def format_things(items): count = items[0] thing = items[1] if count != 1: thing += 's' return f"{count} {thing}" # Example: print(format_things([3, "trinket"])) # Output: "3 trinkets"
Below are targeted extensions with code examples:
1. Handle Irregular Plurals
Many nouns don’t follow the simple "add s" rule (like child → children or mouse → mice). Add a lookup dictionary to cover these exceptions:
# Define common irregular plurals IRREGULAR_PLURALS = { "child": "children", "mouse": "mice", "person": "people", "tooth": "teeth", "foot": "feet" } def format_things(items): count = items[0] thing = items[1] if count != 1: # Check for irregular plural first if thing in IRREGULAR_PLURALS: thing = IRREGULAR_PLURALS[thing] else: thing += 's' return f"{count} {thing}" print(format_things([2, "child"])) # Output: "2 children"
2. Smart Pluralization for Common Word Endings
Upgrade your logic to handle patterns like nouns ending in s/x/z/ch/sh (add es) or y (replace with ies if preceded by a consonant):
def format_things(items): count = items[0] thing = items[1] if count == 1: return f"{count} {thing}" # Check irregulars first IRREGULAR_PLURALS = { "child": "children", "mouse": "mice" } if thing in IRREGULAR_PLURALS: return f"{count} {IRREGULAR_PLURALS[thing]}" # Handle special endings if thing.endswith(('s', 'x', 'z', 'ch', 'sh')): plural = thing + 'es' elif thing.endswith('y') and thing[-2].lower() not in 'aeiou': plural = thing[:-1] + 'ies' else: plural = thing + 's' return f"{count} {plural}" print(format_things([5, "box"])) # Output: "5 boxes" print(format_things([2, "baby"])) # Output: "2 babies"
3. Support Uncountable Nouns
Some nouns (like water or information) don’t have plural forms. Add a check to skip pluralization for these:
UNCOUNTABLE_NOUNS = {"water", "information", "furniture", "rice"} def format_things(items): count = items[0] thing = items[1] # Keep uncountable nouns singular regardless of count if thing in UNCOUNTABLE_NOUNS: return f"{count} {thing}" if count == 1: return f"{count} {thing}" # Rest of plural logic... IRREGULAR_PLURALS = {"child": "children", "mouse": "mice"} if thing in IRREGULAR_PLURALS: return f"{count} {IRREGULAR_PLURALS[thing]}" if thing.endswith(('s', 'x', 'z', 'ch', 'sh')): plural = thing + 'es' elif thing.endswith('y') and thing[-2].lower() not in 'aeiou': plural = thing[:-1] + 'ies' else: plural = thing + 's' return f"{count} {plural}" print(format_things([3, "water"])) # Output: "3 water"
4. Natural Zero-Count Phrasing
Instead of clunky "0 trinkets", rewrite it to sound more natural like "no trinkets":
def format_things(items): count = items[0] thing = items[1] UNCOUNTABLE_NOUNS = {"water", "information"} IRREGULAR_PLURALS = {"child": "children"} if count == 0: if thing in UNCOUNTABLE_NOUNS: return f"no {thing}" # Get plural form for countable nouns if thing in IRREGULAR_PLURALS: plural = IRREGULAR_PLURALS[thing] elif thing.endswith(('s', 'x', 'z', 'ch', 'sh')): plural = thing + 'es' elif thing.endswith('y') and thing[-2].lower() not in 'aeiou': plural = thing[:-1] + 'ies' else: plural = thing + 's' return f"no {plural}" # Rest of logic for counts >=1... if thing in UNCOUNTABLE_NOUNS: return f"{count} {thing}" if count == 1: return f"{count} {thing}" if thing in IRREGULAR_PLURALS: return f"{count} {IRREGULAR_PLURALS[thing]}" if thing.endswith(('s', 'x', 'z', 'ch', 'sh')): plural = thing + 'es' elif thing.endswith('y') and thing[-2].lower() not in 'aeiou': plural = thing[:-1] + 'ies' else: plural = thing + 's' return f"{count} {plural}" print(format_things([0, "trinket"])) # Output: "no trinkets"
Pick the extensions that fit your goals, or mix and match to build a fully polished solution!
内容的提问来源于stack exchange,提问作者Eric Castillo

