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.
# Python Dedent Function Here's a function that de-dents a Python code string by removing the maximum common indentation from all non-blank lines:
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 | def dedent(s): """ De-dent a string of Python code by removing the maximum common indentation. Completely blank lines are ignored when calculating the indentation level. Args: s: A string containing Python code Returns: The de-dented string """ # Split the input string into lines lines = s.splitlines() # Find the minimum indentation level among non-blank lines min_indent = float('inf') for line in lines: # Skip blank lines when calculating minimum indentation if not line.strip(): continue # Count leading whitespace indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # If all lines are blank or min_indent is still infinite, # return the original string if min_indent == float('inf'): return s # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if not line.strip(): # Blank line dedented_lines.append('') else: # Non-blank line dedented_lines.append(line[min_indent:]) # Join the lines back together result = '\n'.join(dedented_lines) # Preserve the original's final newline if present if s.endswith('\n'): result += '\n' return result |
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 | def dedent(s): """ De-dent a string of Python code by removing the maximum common indentation. Completely blank lines are ignored when calculating the indentation level. Args: s: A string containing Python code Returns: The de-dented string """ # Split the input string into lines lines = s.splitlines() # Find the minimum indentation level among non-blank lines min_indent = float('inf') for line in lines: # Skip blank lines when calculating minimum indentation if not line.strip(): continue # Count leading whitespace indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # If all lines are blank or min_indent is still infinite, # return the original string if min_indent == float('inf'): return s # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if not line.strip(): # Blank line dedented_lines.append('') else: # Non-blank line dedented_lines.append(line[min_indent:]) # Join the lines back together result = '\n'.join(dedented_lines) # Preserve the original's final newline if present if s.endswith('\n'): result += '\n' return result |
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 | def dedent(s): """ De-dent a string of Python code by removing the maximum common indentation. Completely blank lines are ignored when calculating the indentation level. Args: s: A string containing Python code Returns: The de-dented string """ # Split the input string into lines lines = s.splitlines() # Find the minimum indentation level among non-blank lines min_indent = float('inf') for line in lines: # Skip blank lines when calculating minimum indentation if not line.strip(): continue # Count leading whitespace indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # If all lines are blank or min_indent is still infinite, # return the original string if min_indent == float('inf'): return s # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if not line.strip(): # Blank line dedented_lines.append('') else: # Non-blank line dedented_lines.append(line[min_indent:]) # Join the lines back together result = '\n'.join(dedented_lines) # Preserve the original's final newline if present if s.endswith('\n'): result += '\n' return result 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
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.
# Python De-indentation Function Let me create a function that removes the maximum possible consistent indentation from a Python code string. The key is to find the minimal leading whitespace across all non-empty lines and then remove exactly that amount from each line.
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 | def dedent(str): """ De-indents a string of Python code by removing the maximum common indentation from all non-empty lines. Args: str: A string containing Python code Returns: A string with the minimum indentation removed from all lines """ # Keep track of trailing newline has_trailing_newline = str.endswith('\n') # Split into lines lines = str.splitlines() # Find the non-empty lines (those with something other than whitespace) non_empty_lines = [line for line in lines if line.strip()] # If all lines are empty, return the original string if not non_empty_lines: return str # Determine the minimum indentation for non-empty lines # We calculate how much whitespace is at the start of each non-empty line min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) # Remove the minimum indentation from all lines dedented_lines = [] for line in lines: if line.strip(): # If it's not an empty line dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) # Keep empty lines as they are # Join the lines back together result = '\n'.join(dedented_lines) # Restore trailing newline if it was present in the original if has_trailing_newline: result += '\n' return result |
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 | def dedent(str): """ De-indents a string of Python code by removing the maximum common indentation from all non-empty lines. Args: str: A string containing Python code Returns: A string with the minimum indentation removed from all lines """ # Keep track of trailing newline has_trailing_newline = str.endswith('\n') # Split into lines lines = str.splitlines() # Find the non-empty lines (those with something other than whitespace) non_empty_lines = [line for line in lines if line.strip()] # If all lines are empty, return the original string if not non_empty_lines: return str # Determine the minimum indentation for non-empty lines # We calculate how much whitespace is at the start of each non-empty line min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) # Remove the minimum indentation from all lines dedented_lines = [] for line in lines: if line.strip(): # If it's not an empty line dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) # Keep empty lines as they are # Join the lines back together result = '\n'.join(dedented_lines) # Restore trailing newline if it was present in the original if has_trailing_newline: result += '\n' return result |
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 | def dedent(str): """ De-indents a string of Python code by removing the maximum common indentation from all non-empty lines. Args: str: A string containing Python code Returns: A string with the minimum indentation removed from all lines """ # Keep track of trailing newline has_trailing_newline = str.endswith('\n') # Split into lines lines = str.splitlines() # Find the non-empty lines (those with something other than whitespace) non_empty_lines = [line for line in lines if line.strip()] # If all lines are empty, return the original string if not non_empty_lines: return str # Determine the minimum indentation for non-empty lines # We calculate how much whitespace is at the start of each non-empty line min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) # Remove the minimum indentation from all lines dedented_lines = [] for line in lines: if line.strip(): # If it's not an empty line dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) # Keep empty lines as they are # Join the lines back together result = '\n'.join(dedented_lines) # Restore trailing newline if it was present in the original if has_trailing_newline: result += '\n' return result 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