I am programming a python Halstead complexity function, but I don't know if this is…
- User
I am programming a python Halstead complexity function, but I don't know if this is correct, do you think the output is correct or is something wrong with my code:
import re import math from collections import Counter
def halstead_metrics(code): # Define regex patterns for operators and operands operator_pattern = r'\b(def|print|=|+|/|,|:|(|)|{|}|f)\b' operand_pattern = r'\b[a-zA-Z_][a-zA-Z0-9_]\b|\d+|".?"'
# Find all operators and operands operators = re.findall(operator_pattern, code) operands = re.findall(operand_pattern, code) # Count distinct operators and operands operator_count = Counter(operators) operand_count = Counter(operands) distinct_operators = len(operator_count) distinct_operands = len(operand_count) 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 # Calculating calculated 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 metricsExample usage
if name == "main": code_sample = """ def main(): a = 5 b = 10 c = 15 print(f"{a} {b} {c}") avg = (a + b + c) / 3 print(f"avg={avg}") """
metrics_result = halstead_metrics(code_sample) for metric, value in metrics_result.items(): print(f"{metric}: {value:.4f}")OUTPUT:
Distinct Operators: 4.0000 Distinct Operands: 14.0000 Total Operators: 7.0000 Total Operands: 19.0000 Vocabulary: 18.0000 Program Length: 26.0000 Calculated Program Length: 61.3030 Volume: 108.4181 Difficulty: 2.7143 Effort: 294.2776 Time Required to Program: 16.3488 Number of Delivered Bugs: 0.0361
- Assistant
- User
now this was the output:
Distinct Operators: 3.0000 Distinct Operands: 1.0000 Total Operators: 5.0000 Total Operands: 19.0000 Vocabulary: 4.0000 Program Length: 24.0000 Calculated Program Length: 4.7549 Volume: 48.0000 Difficulty: 28.5000 Effort: 1368.0000 Time Required to Program: 76.0000 Number of Delivered Bugs: 0.0160
Do you think now is better or correct or maybe something is missing?
- Assistant
- User
your new code gave the same answer, maybe this is the correct one?
Distinct Operators: 3.0000 Distinct Operands: 1.0000 Total Operators: 5.0000 Total Operands: 19.0000 Vocabulary: 4.0000 Program Length: 24.0000 Calculated Program Length: 4.7549 Volume: 48.0000 Difficulty: 28.5000 Effort: 1368.0000 Time Required to Program: 76.0000 Number of Delivered Bugs: 0.0160
- Assistant
- User
now it did in fact change:
Distinct Operators: 3.0000 Distinct Operands: 14.0000 Total Operators: 5.0000 Total Operands: 19.0000 Vocabulary: 17.0000 Program Length: 24.0000 Calculated Program Length: 58.0579 Volume: 98.0991 Difficulty: 2.0357 Effort: 199.7018 Time Required to Program: 11.0945 Number of Delivered Bugs: 0.0327
But it got worse, it says the time required to program is 11 seconds which I don't think that is correct at all.
Can you do these calculations yourself and compare with this output to see which parts are the one wrong?
- Assistant
- User
ok now that we know whats wrong, fix the code
- Assistant
- User
- Assistant
- User
- Assistant