Write a python function `dedent(str)` that takes as input a string of python code and de-dents it maximally. Lines that are entirely blank don't count.
Write a python function `dedent(str)` that takes as input a string of python code and de-dents it maximally. Lines that are entirely blank don't count.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import textwrap def dedent(code): """ De-dent a string of Python code maximally. Lines that are entirely blank don't count towards the indentation level. Args: code (str): A string of Python code. Returns: str: The de-dented code. """ lines = code.splitlines() # Filter out blank lines non_blank_lines = [line for line in lines if line.strip()] # Calculate the minimum indentation level min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) if non_blank_lines else 0 # Remove the minimum indentation level from each line de_dented_lines = [line[min_indent:] for line in lines] # Join the lines back into a single string de_dented_code = '\n'.join(de_dented_lines) return de_dented_code # Alternatively, you can use the textwrap.dedent function def dedent_textwrap(code): """ De-dent a string of Python code maximally using textwrap.dedent. Lines that are entirely blank don't count towards the indentation level. Args: code (str): A string of Python code. Returns: str: The de-dented code. """ return textwrap.dedent(code) # Example usage if __name__ == "__main__": code = """ def hello(): print("Hello, World!") """ print(dedent(code)) print(dedent_textwrap(code)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import textwrap def dedent(code): """ De-dent a string of Python code maximally. Lines that are entirely blank don't count towards the indentation level. Args: code (str): A string of Python code. Returns: str: The de-dented code. """ lines = code.splitlines() # Filter out blank lines non_blank_lines = [line for line in lines if line.strip()] # Calculate the minimum indentation level min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) if non_blank_lines else 0 # Remove the minimum indentation level from each line de_dented_lines = [line[min_indent:] for line in lines] # Join the lines back into a single string de_dented_code = '\n'.join(de_dented_lines) return de_dented_code # Alternatively, you can use the textwrap.dedent function def dedent_textwrap(code): """ De-dent a string of Python code maximally using textwrap.dedent. Lines that are entirely blank don't count towards the indentation level. Args: code (str): A string of Python code. Returns: str: The de-dented code. """ return textwrap.dedent(code) # Example usage if __name__ == "__main__": code = """ def hello(): print("Hello, World!") """ print(dedent(code)) print(dedent_textwrap(code)) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import textwrap def dedent(code): """ De-dent a string of Python code maximally. Lines that are entirely blank don't count towards the indentation level. Args: code (str): A string of Python code. Returns: str: The de-dented code. """ lines = code.splitlines() # Filter out blank lines non_blank_lines = [line for line in lines if line.strip()] # Calculate the minimum indentation level min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) if non_blank_lines else 0 # Remove the minimum indentation level from each line de_dented_lines = [line[min_indent:] for line in lines] # Join the lines back into a single string de_dented_code = '\n'.join(de_dented_lines) return de_dented_code # Alternatively, you can use the textwrap.dedent function def dedent_textwrap(code): """ De-dent a string of Python code maximally using textwrap.dedent. Lines that are entirely blank don't count towards the indentation level. Args: code (str): A string of Python code. Returns: str: The de-dented code. """ return textwrap.dedent(code) # Example usage if __name__ == "__main__": code = """ def hello(): print("Hello, World!") """ print(dedent(code)) print(dedent_textwrap(code)) answer = dedent(''' print("hello") print("world") if True: print("true") else: print("false")''') expected = """print("hello") print("world") if True: print("true") else: print("false")""" assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
def hello():
print("Hello, World!")
def hello():
print("Hello, World!")
All tests passed
Testing if the previous output contains the string All tests passed
: True