Hone logo
Hone
Problems

Simple Python Code Analyzer

This challenge asks you to implement a basic code analyzer in Python. Code analysis is a crucial step in software development, used for tasks like linting, static analysis, and identifying potential errors before runtime. Your analyzer will focus on identifying and counting specific code elements within a given Python code snippet.

Problem Description

You are tasked with creating a function analyze_code that takes a string containing Python code as input. The function should parse this code and return a dictionary containing the counts of the following elements:

  • function_definitions: The number of function definitions (identified by the def keyword).
  • class_definitions: The number of class definitions (identified by the class keyword).
  • import_statements: The number of import statements (identified by the import keyword).
  • variable_assignments: The number of variable assignments (identified by the = operator).

The function should be robust and handle various code structures, including nested functions and classes. It should accurately count each element, even if they appear multiple times within the code.

Key Requirements:

  • The function must accept a string as input.
  • The function must return a dictionary with the keys mentioned above and their corresponding integer counts.
  • The function should be case-sensitive (e.g., Def is not a function definition).
  • The function should not execute the code; it should only analyze the text.

Expected Behavior:

The function should correctly identify and count the specified code elements based on the keywords and operators provided. It should not be affected by comments or whitespace.

Edge Cases to Consider:

  • Empty input string.
  • Code with no occurrences of any of the target elements.
  • Code with multiple occurrences of the same element on a single line.
  • Nested function and class definitions.
  • Import statements with multiple modules (e.g., import os, sys). Count each import statement as one.

Examples

Example 1:

Input: "def my_function():\n  x = 10\n  return x\n\nclass MyClass:\n  pass\nimport os"
Output: {'function_definitions': 1, 'class_definitions': 1, 'import_statements': 1, 'variable_assignments': 1}
Explanation: The code contains one function definition (`def my_function`), one class definition (`class MyClass`), one import statement (`import os`), and one variable assignment (`x = 10`).

Example 2:

Input: "x = 5\ny = x + 2\nimport math, random\n"
Output: {'function_definitions': 0, 'class_definitions': 0, 'import_statements': 1, 'variable_assignments': 2}
Explanation: The code contains zero function definitions, zero class definitions, one import statement (`import math, random`), and two variable assignments (`x = 5`, `y = x + 2`).

Example 3:

Input: ""
Output: {'function_definitions': 0, 'class_definitions': 0, 'import_statements': 0, 'variable_assignments': 0}
Explanation: The input is an empty string, so all counts are zero.

Constraints

  • The input string can be up to 10000 characters long.
  • The code should be implemented in Python 3.
  • The solution should be reasonably efficient; avoid excessively complex or slow algorithms. A simple string search approach is acceptable for this level of complexity.
  • The function must return a dictionary with the exact keys specified, even if a count is zero.

Notes

  • You can use string manipulation techniques like count() or regular expressions to identify the code elements.
  • Consider breaking down the problem into smaller, manageable steps. For example, you could write separate functions to count each element type.
  • Focus on accuracy and robustness. Test your code with a variety of inputs, including edge cases.
  • This is a simplified code analyzer; it does not cover all aspects of Python syntax or semantics. The goal is to demonstrate basic code parsing and counting capabilities.
Loading editor...
python