Complete the code to define a static analysis tool that checks code style.
def check_code_style(code): issues = [] if not code.endswith([1]): issues.append("Code should end with a newline.") return issues
The code style checker expects the code to end with a newline character \n. This is a common style rule.
Complete the code to detect unused variables in a simple code snippet.
def find_unused_vars(code_vars, used_vars): unused = [var for var in code_vars if var [1] used_vars] return unused
in instead of not in, which would find used variables instead.Unused variables are those declared but not used, so we check if each variable is not in the list of used variables.
Fix the error in the code that checks for function naming conventions.
def is_valid_function_name(name): return name.[1]("_") and name.islower()
endswith which checks the end of the string.contains which is not a valid string method.Function names should start with an underscore for private functions. The method startswith checks this correctly.
Fill both blanks to create a dictionary comprehension that maps variable names to their lengths only if length is greater than 3.
lengths = {var: [1] for var in variables if len(var) [2] 3}< instead of greater than >.The dictionary maps each variable name to its length using len(var). The condition filters names with length greater than 3 using >.
Fill all three blanks to create a dictionary comprehension that maps uppercase variable names to their values only if the value is positive.
result = [1]: [2] for [3], val in data.items() if val > 0
key instead of var as loop variable.The dictionary keys are uppercase variable names using var.upper(). The values are val. The loop variable is var for the variable name.