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 solve this problem, we need to write a Python function `dedent` that takes a string of Python code and dedents it maximally. The goal is to remove the maximum possible leading whitespace from each line such that no line ends up with negative indentation. Lines that are entirely blank should not affect the calculation of the minimum indentation. ### Approach 1. **Split the Input String**: First, split the input string into individual lines using the newline character `\n`. 2. **Calculate Leading Whitespace**: For each non-blank line, compute the length of the leading whitespace. This is done by comparing the length of the original line with the length of the line after removing leading whitespace. 3. **Determine Minimum Indentation**: Find the minimum leading whitespace length among all non-blank lines. This value determines how much indentation can be safely removed from each line. 4. **Process Each Line**: For each line, if it is blank, leave it unchanged. Otherwise, remove the calculated minimum indentation from the start of the line. 5. **Reconstruct the String**: Join the processed lines back into a single string using newline characters. ### Solution Code
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 | def dedent(s): lines = s.split('\n') leading_whitespaces = [] # Calculate leading whitespace for each non-blank line for line in lines: stripped = line.lstrip() if stripped: # line is not blank leading_length = len(line) - len(stripped) leading_whitespaces.append(leading_length) # Determine the minimum leading whitespace (default to 0 if all lines are blank) min_leading = min(leading_whitespaces) if leading_whitespaces else 0 processed_lines = [] for line in lines: stripped = line.lstrip() if not stripped: # blank line, leave as is processed_lines.append(line) else: # Remove the minimal leading whitespace processed_line = line[min_leading:] processed_lines.append(processed_line) return '\n'.join(processed_lines) |
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 | def dedent(s): lines = s.split('\n') leading_whitespaces = [] # Calculate leading whitespace for each non-blank line for line in lines: stripped = line.lstrip() if stripped: # line is not blank leading_length = len(line) - len(stripped) leading_whitespaces.append(leading_length) # Determine the minimum leading whitespace (default to 0 if all lines are blank) min_leading = min(leading_whitespaces) if leading_whitespaces else 0 processed_lines = [] for line in lines: stripped = line.lstrip() if not stripped: # blank line, leave as is processed_lines.append(line) else: # Remove the minimal leading whitespace processed_line = line[min_leading:] processed_lines.append(processed_line) return '\n'.join(processed_lines) |
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 | def dedent(s): lines = s.split('\n') leading_whitespaces = [] # Calculate leading whitespace for each non-blank line for line in lines: stripped = line.lstrip() if stripped: # line is not blank leading_length = len(line) - len(stripped) leading_whitespaces.append(leading_length) # Determine the minimum leading whitespace (default to 0 if all lines are blank) min_leading = min(leading_whitespaces) if leading_whitespaces else 0 processed_lines = [] for line in lines: stripped = line.lstrip() if not stripped: # blank line, leave as is processed_lines.append(line) else: # Remove the minimal leading whitespace processed_line = line[min_leading:] processed_lines.append(processed_line) return '\n'.join(processed_lines) 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') |
All tests passed
Testing if the previous output contains the string All tests passed
: True