Help me fix my Halstead Complexity python code, and find where the problema lies, because the counts are wrong making all the calculations wrong. 

This is my expected output:

Distinct Operators: 12.0000
Distinct Operands: 7.0000
Total Operators: 27.0000
Total Operands: 15.0000
Vocabulary: 19.0000
Program Length: 42.0000
Calculated Program Length: 62.6670
Volume: 178.4369
Difficulty: 12.8571
Effort: 2292.4400
Time Required to Program: 127.3578
Number of Delivered Bugs: 0.0595


This is how the operators and operands should be calculated:

Correct Identification of Operators and Operands

Operators: The distinct operators should be main, (), {}, int, scanf, &, =, +, /, printf, ,, and ;.
Operands: The distinct operands should be a, b, c, avg, "%d %d %d", 3, and "avg = %d".

Correct Count Total Operators and Operands

**Total Operators ($$ N_1 $$):**
1. `main` - 1
2. `()` - 2 (for main)
3. `{}` - 1
4. `int` - 1
5. `scanf` - 1
6. `&` - 3 (for each variable)
7. `=` - 3 (for avg and scanf)
8. `+` - 2 (for the sum of a, b, c)
9. `/` - 1 (for division)
10. `printf` - 1
11. `,` - 2 (in scanf)
12. `;` - 4 (end of statements)

Total: $$ N_1 = 27 $$

**Total Operands ($$ N_2 $$):**
1. `a` - 3 times
2. `b` - 3 times
3. `c` - 3 times
4. `avg` - 2 times
5. `"avg = %d"` - 1 time
6. `" %d %d %d"` - 1 time
7. `3` - 1 time

Total: $$ N_2 = 15 $$



But this is my output:

Unique Operators: ['&', ',', 'int', '{', '=', '/', '}', '+', 'printf', 'main', ';', 'scanf']
Unique Operands: ['b', '"%d %d %d"', 'avg', '"avg = %d"', 'a', 'c', '3']

Operator counts:
main: 1
{: 1
int: 1
,: 7
;: 4
scanf: 1
&: 3
=: 2
+: 2
/: 1
printf: 1
}: 1
: 1

Operand counts:
a: 3
b: 3
c: 3
avg: 3
"%d %d %d": 1
3: 1
"avg = %d": 1
Distinct Operators: 12.0000
Distinct Operands: 7.0000
Total Operators: 26.0000
Total Operands: 15.0000
Vocabulary: 19.0000
Program Length: 41.0000
Calculated Program Length: 62.6710
Volume: 174.1650
Difficulty: 12.8571
Effort: 2239.2646
Time Required to Program: 124.4036
Number of Delivered Bugs: 0.0581


This is my code:

import re
import math
from collections import Counter

def halstead_metrics(code):
    # Define regex patterns for operators and operands
    operator_pattern = r'\bmain\b|$$$$|\{|\}|\bint\b|\bscanf\b|&|=|\+|\/|\bprintf\b|,|;'
    operand_pattern = r'\b[a-zA-Z_][a-zA-Z0-9_]*\b|\"[^\"]*\"|\b\d+\b'

    # Find all operators and operands
    operators = re.findall(operator_pattern, code)
    operands = re.findall(operand_pattern, code)
    operands = [operand for operand in operands if operand not in operators]

    # Remove duplicates by converting to sets
    unique_operators = set(operators)
    unique_operators = {item for item in unique_operators if item}
    unique_operands = set(operands)
    unique_operands = {item for item in unique_operands if item}

    # Ensure that operands do not contain any operators
    final_operands = unique_operands - unique_operators

    # Convert back to lists
    unique_operators = list(unique_operators)
    unique_operands = list(final_operands)

    # Filter original lists to keep only unique elements
    operators = [op for op in operators if op in unique_operators]
    operands = [opd for opd in operands if opd in unique_operands]

    # Output the results
    print("Unique Operators:", unique_operators)
    print("Unique Operands:", unique_operands)

    # Count occurrences of each operator and operand
    operator_count = Counter(operators)
    operand_count = Counter(operands)

    # Print operator and operand counts for debugging
    print("\nOperator counts:")
    for op, count in operator_count.items():
        print(f"{op}: {count}")
    print("\nOperand counts:")
    for op, count in operand_count.items():
        print(f"{op}: {count}")

    # Distinct counts of operators and operands
    distinct_operators = len(unique_operators)
    distinct_operands = len(unique_operands)

    # Total counts of operators and operands
    total_operators = sum(operator_count.values())
    total_operands = sum(operand_count.values())

    # Calculate Halstead metrics
    vocabulary = distinct_operators + distinct_operands
    program_length = total_operators + total_operands

    # Calculated Estimated Program Length (N')
    calculated_length = (
        (distinct_operators * math.log2(distinct_operators) if distinct_operators > 0 else 0) +
        (distinct_operands * math.log2(distinct_operands) if distinct_operands > 0 else 0)
    )

    # Volume (V)
    volume = program_length * math.log2(vocabulary) if vocabulary > 0 else 0

    # Difficulty (D)
    difficulty = (distinct_operators / 2) * (total_operands / distinct_operands) if distinct_operands > 0 else 0

    # Effort (E)
    effort = difficulty * volume

    # Time Required to Program (T)
    time_required = effort / 18

    # Number of Delivered Bugs (B)
    bugs = volume / 3000

    # Return metrics in a dictionary
    metrics = {
        "Distinct Operators": distinct_operators,
        "Distinct Operands": distinct_operands,
        "Total Operators": total_operators,
        "Total Operands": total_operands,
        "Vocabulary": vocabulary,
        "Program Length": program_length,
        "Calculated Program Length": calculated_length,
        "Volume": volume,
        "Difficulty": difficulty,
        "Effort": effort,
        "Time Required to Program": time_required,
        "Number of Delivered Bugs": bugs
    }

    return metrics

# Example usage
if __name__ == "__main__":
    code_sample = """
    main()
    {
        int a, b, c, avg;
        scanf("%d %d %d", &a, &b, &c);
        avg = (a + b + c) / 3;
        printf("avg = %d", avg);
    }
    """

    metrics_result = halstead_metrics(code_sample)

    for metric, value in metrics_result.items():
        print(f"{metric}: {value:.4f}")