Help me fix my Halstead Complexity python code. This is my expected output: Distinct…
- User
Help me fix my Halstead Complexity python code.
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
But this is my incorrect output:
Distinct Operators: 11.0000 Distinct Operands: 10.0000 Total Operators: 33.0000 Total Operands: 18.0000 Vocabulary: 21.0000 Program Length: 51.0000 Calculated Program Length: 71.2730 Volume: 224.0082 Difficulty: 9.9000 Effort: 2217.6811 Time Required to Program: 123.2045 Number of Delivered Bugs: 0.0747
Which means:
Distinct Operators: off by -1 Distinct Operands: off by +3 Total Operators: off by +6 Total Operands: off by +3
This is my code:
import re import math from collections import Counter
def halstead_metrics(code): # Define regex patterns for operators and operands in Python code operator_pattern = r'(\bdef\b|\breturn\b|=|+|-|*|/|%|&|||^|~|<|>|<=|>=|==|!=|:=|\bif\b|\belse\b|\bfor\b|\bwhile\b|\bimport\b|\bfrom\b|\bas\b|\bwith\b|\btry\b|\bexcept\b|\bfinally\b|\bclass\b|\bpass\b|\bbreak\b|\bcontinue\b|\braise\b|\byield\b|\bassert\b|\bdel\b|\blambda\b|\bglobal\b|\bnonlocal\b|\bnot\b|\band\b|\bor\b|\bin\b|\bis\b|\bNone\b|\bTrue\b|\bFalse\b|;|,|(|)|[|]|{|})' operand_pattern = r'\b[a-zA-Z_][a-zA-Z0-9_]\b|".?"|'[^']*''
code - Assistant
- User
this code is even worse than the previous one:
Distinct Operators: 7.0000 Distinct Operands: 12.0000 Total Operators: 18.0000 Total Operands: 16.0000 Vocabulary: 19.0000 Program Length: 34.0000 Calculated Program Length: 62.6710 Volume: 144.4295 Difficulty: 4.6667 Effort: 674.0045 Time Required to Program: 37.4447 Number of Delivered Bugs: 0.0481
- Assistant
- User
This was even worse than both attempts, to help you this is how the correct operators/operands should be calculated:
Step 1: Identify Operators and Operands
Distinct Operators ():
main(){}intscanf&
- Assistant