Skip to content

S3Q1 · Word Filters

⚡ Quick Reference

Function: get_words_by_criteria(words, criteria=None) -> list | None

def get_words_by_criteria(words, criteria=None):
    vowels = "aeiou"

    def is_continuous(w):
        return w.lower().endswith("ing")

    def is_vowel_rich(w):
        return sum(1 for c in w.lower() if c in vowels) > 5

    def is_consonant_rich(w):
        return sum(1 for c in w.lower() if c.isalpha() and c not in vowels) > 5

    def is_sorted(w):
        letters = [c.lower() for c in w if c.isalpha()]
        return letters == sorted(letters)

    filters = {
        "continuous":      is_continuous,
        "vowel_rich":      is_vowel_rich,
        "consonant_rich":  is_consonant_rich,
        "sorted":          is_sorted,
    }
    if criteria not in filters:
        return None
    return [w for w in words if filters[criteria](w)]

Key rules: - continuous: ends with "ing" (case-insensitive) - vowel_rich: vowel count > 5 (strict, not >= 5) - consonant_rich: consonant count > 5 (alphabetic, non-vowel) - sorted: letters in ascending order (case-insensitive) - Unknown criteria → return None


Problem Statement

Problem

Write a function get_words_by_criteria(words, criteria=None) that filters a list of words based on one of four criteria. Returns None for unknown criteria.

Sample word list:

words = ["running", "aeiobcdioe", "acc", "xyz", "BOTbot",
         "BboOTt", "jumpiNg", "SPINNING", "alphabetical"]


Breaking down each criteria

continuous - ends with "ing"

w.lower().endswith("ing")
Word .lower() ends with "ing"?
"running" "running"
"jumpiNg" "jumpng" wait - "jumping"
"SPINNING" "spinning"
"alphabetical" "alphabetical"

Result: ["running", "jumpiNg", "SPINNING"]


vowel_rich - more than 5 vowels

Count alphabetic vowels (case-insensitive), keep if > 5:

sum(1 for c in w.lower() if c in "aeiou") > 5
Word Vowels Count > 5?
"aeiobcdioe" a,e,i,o,i,o,e 7
"alphabetical" a,a,e,i,a 5 ❌ (not > 5)
"SPINNING" i,i 2

Result: ["aeiobcdioe"]


consonant_rich - more than 5 consonants

Alphabetic characters that are not vowels, count > 5:

sum(1 for c in w.lower() if c.isalpha() and c not in "aeiou") > 5
Word Consonants Count > 5?
"SPINNING" s,p,n,n,n,g 6
"alphabetical" l,p,h,b,t,c,l 7
"running" r,n,n,n,g 5

Result: ["SPINNING", "alphabetical"]


sorted - letters in ascending alphabetical order

Convert all letters to lowercase, check if the list is sorted:

letters = [c.lower() for c in w if c.isalpha()]
letters == sorted(letters)
Word Letters (lower) Sorted?
"acc" [a,c,c]
"xyz" [x,y,z]
"BboOTt" [b,b,o,o,t,t]
"BOTbot" [b,o,t,b,o,t]

Result: ["acc", "xyz", "BboOTt"]


Complete solution approaches

def get_words_by_criteria(words, criteria=None):
    vowels = "aeiou"

    def is_continuous(w):
        return w.lower().endswith("ing")

    def is_vowel_rich(w):
        return sum(1 for c in w.lower() if c in vowels) > 5

    def is_consonant_rich(w):
        return sum(1 for c in w.lower() if c.isalpha() and c not in vowels) > 5

    def is_sorted_word(w):
        letters = [c.lower() for c in w if c.isalpha()]
        return letters == sorted(letters)

    filters = {
        "continuous":     is_continuous,
        "vowel_rich":     is_vowel_rich,
        "consonant_rich": is_consonant_rich,
        "sorted":         is_sorted_word,
    }
    if criteria not in filters:
        return None
    return [w for w in words if filters[criteria](w)]
def get_words_by_criteria(words, criteria=None):
    vowels = "aeiou"
    filters = {
        "continuous":     lambda w: w.lower().endswith("ing"),
        "vowel_rich":     lambda w: sum(1 for c in w.lower() if c in vowels) > 5,
        "consonant_rich": lambda w: sum(1 for c in w.lower() if c.isalpha() and c not in vowels) > 5,
        "sorted":         lambda w: (lambda l: l == sorted(l))([c.lower() for c in w if c.isalpha()]),
    }
    if criteria not in filters:
        return None
    return list(filter(filters[criteria], words))

Each criteria is a lambda stored in a dict. filter(condition, words) applies it. The sorted lambda uses an inner lambda to avoid computing the letters list twice.


Key takeaways

01

Strict > 5, not >= 5

The problem says "more than 5" - that's > 5, not >= 5. A word with exactly 5 vowels or consonants does NOT qualify. Always re-read the boundary condition.

02

letters == sorted(letters) for order check

Converting to lowercase and comparing against sorted() checks ascending alphabetical order in one expression. Works for any length word.

03

Unknown criteria → None (not empty list)

An unknown criteria returns None, not []. These are different values - always check the problem's exact return type for the default/invalid case.