def matching(guess, answer):
    """
    given the guessed word and answer, returns the matching result (pattern).

    guess:  string, a five-letter word.
    answer: string, a five-letter word.

    returns the matching pattern, a tuple of 5 elements.
    each element is composed of a single-letter string: 'C', 'M', or 'X'.

    - C (correct)   the guessed letter is in the word and in the correct spot.
    - M (misplaced) the guessed letter is in the word but in the wrong spot.
    - X (excluded)  the guessed letter is not in the word in any spot.

    matching mechanism:
    1. finding CORRECT spots
    2. search the not-correct letters in not-correct spots of the answer:
       1) if exist, the letter is MISPLACED
       2) if the letter does not exist in the not-correct spots of the answer,
          the letter is EXCLUDED
    """
    not_used = [c for c in answer]
    matching_result = ['' for _ in range(5)]

    # CORRECT 🟩
    for i in range(5):
        if guess[i] == answer[i]:
            matching_result[i] = 'C'
            not_used.remove(guess[i])

    for i in range(5):
        if matching_result[i] == '':
            # MISPLACED 🟨
            if guess[i] in not_used:
                matching_result[i] = 'M'
                not_used.remove(guess[i])

            # EXCLUDED ⬜
            else:
                matching_result[i] = 'X'

    return tuple(matching_result)