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.
To create a Python function `dedent(str)` that removes the maximum common indentation from a string of Python code, we can follow these steps: 1. Split the input string into lines. 2. Identify the minimum indentation among non-blank lines. 3. Remove that minimum indentation from each line. 4. Join the lines back into a single string. Here's the implementation:
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 | def dedent(code): # Split the code into lines lines = code.splitlines() # Find the minimum indentation among non-blank lines min_indent = None for line in lines: stripped_line = line.lstrip() if stripped_line: # Skip blank lines indent = len(line) - len(stripped_line) if min_indent is None or indent < min_indent: min_indent = indent # If all lines are blank, return the original code if min_indent is None: return code # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if line.strip(): # Non-blank line dedented_lines.append(line[min_indent:]) else: # Blank line dedented_lines.append(line) # Join the lines back into a single string return '\n'.join(dedented_lines) # Example usage: code = """ def foo(): print("Hello, World!") def bar(): print("Goodbye, World!") """ print(dedent(code)) |
1 2 3 4 5 | def foo(): print("Hello, World!") def bar(): print("Goodbye, World!") |
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 | def dedent(code): # Split the code into lines lines = code.splitlines() # Find the minimum indentation among non-blank lines min_indent = None for line in lines: stripped_line = line.lstrip() if stripped_line: # Skip blank lines indent = len(line) - len(stripped_line) if min_indent is None or indent < min_indent: min_indent = indent # If all lines are blank, return the original code if min_indent is None: return code # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if line.strip(): # Non-blank line dedented_lines.append(line[min_indent:]) else: # Blank line dedented_lines.append(line) # Join the lines back into a single string return '\n'.join(dedented_lines) # Example usage: code = """ def foo(): print("Hello, World!") def bar(): print("Goodbye, World!") """ print(dedent(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 | def dedent(code): # Split the code into lines lines = code.splitlines() # Find the minimum indentation among non-blank lines min_indent = None for line in lines: stripped_line = line.lstrip() if stripped_line: # Skip blank lines indent = len(line) - len(stripped_line) if min_indent is None or indent < min_indent: min_indent = indent # If all lines are blank, return the original code if min_indent is None: return code # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if line.strip(): # Non-blank line dedented_lines.append(line[min_indent:]) else: # Blank line dedented_lines.append(line) # Join the lines back into a single string return '\n'.join(dedented_lines) # Example usage: code = """ def foo(): print("Hello, World!") def bar(): print("Goodbye, World!") """ print(dedent(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') |
1 2 3 4 5 | def foo(): print("Hello, World!")</p> <p>def bar(): print("Goodbye, World!") All tests passed |
Testing if the previous output contains the string All tests passed
: True