Test: Is Anagram

Test: Is Anagram

Specification for a function you don't need to write:

is_anagram(a, b)

Returns True if a and b are anagrams of each other (same letters, same counts, case-insensitive), False otherwise.

Your job: write a unittest.TestCase class, TestIsAnagram, with test methods test_true_case and test_false_case that verify this specification. Call is_anagram directly.

Checked against a correct implementation (all should pass) and a buggy one (at least one should fail).


Walkthrough

Thinking it through

import unittest


class TestIsAnagram(unittest.TestCase):
    def test_true_case(self):
        self.assertEqual(is_anagram("listen", "silent"), True)

    def test_false_case(self):
        self.assertEqual(is_anagram("hello", "world"), False)

The interesting decision here is the pair chosen for test_false_case. "hello" and "world" are both five letters long — same length, clearly not anagrams. That specific choice matters: a broken implementation that only checks len(a) == len(b) (never looking at the actual letters) would incorrectly say these are anagrams too, since it never gets past the length check.

If test_false_case had instead used two words of different lengths (like "hi" and "world"), the length-only bug would go completely unnoticed — a length mismatch alone is enough to correctly return False, whether or not the implementation ever compares the actual letters.

This is the same lesson as Test: List Max, in a different shape: picking a test case that happens to produce the right answer for the wrong reason is a common trap. Good test inputs are chosen specifically to rule out plausible-but-wrong shortcuts, not just to hit the "true" and "false" cases in the abstract.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.