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.
Sure, here's a Python function that dedents a string of Python code maximally, ignoring entirely blank lines:
1 2 3 4 5 6 7 8 9 | def dedent(code): """ Removes the maximum common leading whitespace from each non-blank line in the given string of code. """ lines = code.splitlines() non_blank_lines = [line for line in lines if line.strip()] min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) dedented_lines = [line[min_indent:] for line in lines] return '\n'.join(dedented_lines) |
1 2 3 4 5 6 7 8 9 10 | code = """ def hello(): print("Hello, World!") print("Indented further") hello() """ dedented_code = dedent(code) print(dedented_code) |
1 2 3 4 5 | def hello(): print("Hello, World!") print("Indented further") hello() |
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 | def dedent(code): """ Removes the maximum common leading whitespace from each non-blank line in the given string of code. """ lines = code.splitlines() non_blank_lines = [line for line in lines if line.strip()] min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) dedented_lines = [line[min_indent:] for line in lines] return '\n'.join(dedented_lines) def main(): code = """ def hello(): print("Hello, World!") print("Indented further") hello() """ dedented_code = dedent(code) print(dedented_code) if __name__ == "__main__": main() |
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 | def dedent(code): """ Removes the maximum common leading whitespace from each non-blank line in the given string of code. """ lines = code.splitlines() non_blank_lines = [line for line in lines if line.strip()] min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) dedented_lines = [line[min_indent:] for line in lines] return '\n'.join(dedented_lines) def main(): code = """ def hello(): print("Hello, World!") print("Indented further") hello() """ dedented_code = dedent(code) print(dedented_code) if __name__ == "__main__": main() 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!")
print("Indented further")
hello()
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.
Here's a Python function that de-dents a string of Python code maximally, ignoring entirely blank lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def dedent(code): """ Dedents a string of Python code maximally, ignoring entirely blank lines. """ lines = code.splitlines() non_blank_lines = [line for line in lines if line.strip()] if not non_blank_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) deindented_lines = [line[min_indent:] for line in lines] return '\n'.join(deindented_lines) |
1 2 3 4 5 6 7 8 9 | code = """ def hello(): print("Hello, World!") hello() """ deindented_code = dedent(code) print(deindented_code) |
1 2 3 4 | def hello(): print("Hello, World!") hello() |
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(code): """ Dedents a string of Python code maximally, ignoring entirely blank lines. """ lines = code.splitlines() non_blank_lines = [line for line in lines if line.strip()] if not non_blank_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) deindented_lines = [line[min_indent:] for line in lines] return '\n'.join(deindented_lines) if __name__ == "__main__": code = """ def hello(): print("Hello, World!") hello() """ deindented_code = dedent(code) print(deindented_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 | def dedent(code): """ Dedents a string of Python code maximally, ignoring entirely blank lines. """ lines = code.splitlines() non_blank_lines = [line for line in lines if line.strip()] if not non_blank_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_blank_lines) deindented_lines = [line[min_indent:] for line in lines] return '\n'.join(deindented_lines) if __name__ == "__main__": code = """ def hello(): print("Hello, World!") hello() """ deindented_code = dedent(code) print(deindented_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!")
hello()
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.
Here's a Python function `dedent(str)` that takes a string of Python code as input and removes the maximum common indentation from all non-blank lines:
1 2 3 4 5 6 7 8 9 10 | def dedent(code): """ Remove the maximum common indentation from all non-blank lines in a string of Python code. """ lines = code.splitlines() indent_lengths = [len(line) - len(line.lstrip()) for line in lines if line.strip()] if not indent_lengths: return code min_indent = min(indent_lengths) return '\n'.join(line[min_indent:] if line.strip() else line for line in lines) |
1 2 3 4 5 6 7 8 9 10 11 | code = """ def hello(): print("Hello, world!") print("This line is over-indented.") print("This line is indented correctly.") print("This line is not indented.") """ dedented_code = dedent(code) print(dedented_code) |
1 2 3 4 5 6 | def hello(): print("Hello, world!") print("This line is over-indented.") print("This line is indented correctly.") print("This line is not indented.") |
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 | def dedent(code): """ Remove the maximum common indentation from all non-blank lines in a string of Python code. """ lines = code.splitlines() indent_lengths = [len(line) - len(line.lstrip()) for line in lines if line.strip()] if not indent_lengths: return code min_indent = min(indent_lengths) return '\n'.join(line[min_indent:] if line.strip() else line for line in lines) def main(): code = """ def hello(): print("Hello, world!") print("This line is over-indented.") print("This line is indented correctly.") print("This line is not indented.") """ dedented_code = dedent(code) print(dedented_code) if __name__ == "__main__": main() |
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 | def dedent(code): """ Remove the maximum common indentation from all non-blank lines in a string of Python code. """ lines = code.splitlines() indent_lengths = [len(line) - len(line.lstrip()) for line in lines if line.strip()] if not indent_lengths: return code min_indent = min(indent_lengths) return '\n'.join(line[min_indent:] if line.strip() else line for line in lines) def main(): code = """ def hello(): print("Hello, world!") print("This line is over-indented.") print("This line is indented correctly.") print("This line is not indented.") """ dedented_code = dedent(code) print(dedented_code) if __name__ == "__main__": main() 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 6 | def hello(): print("Hello, world!") print("This line is over-indented.") print("This line is indented correctly.") print("This line is not indented.")</p> <p>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.
Here's a Python function `dedent(str)` that takes a string of Python code as input and de-dents it maximally, ignoring entirely 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 | def dedent(code): """ Remove the maximum common indentation from a string of Python code. Args: code (str): The Python code to be dedented. Returns: str: The dedented Python code. """ # Split the code into lines lines = code.splitlines() # Find the minimum indentation among non-blank lines min_indent = float('inf') for line in lines: if line.strip(): indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Remove the minimum indentation from each non-blank line dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) # Join the dedented lines back into a single string dedented_code = '\n'.join(dedented_lines) return dedented_code |
1 2 3 4 5 6 7 8 9 10 | indented_code = """ def greet(name): print(f"Hello, {name}!") def nested(): print("This is a nested function.") """ dedented_code = dedent(indented_code) print(dedented_code) |
1 2 3 4 5 | def greet(name): print(f"Hello, {name}!") def nested(): print("This is a nested function.") |
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(code): """ Remove the maximum common indentation from a string of Python code. Args: code (str): The Python code to be dedented. Returns: str: The dedented Python code. """ # Split the code into lines lines = code.splitlines() # Find the minimum indentation among non-blank lines min_indent = float('inf') for line in lines: if line.strip(): indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Remove the minimum indentation from each non-blank line dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) # Join the dedented lines back into a single string dedented_code = '\n'.join(dedented_lines) return dedented_code # Example usage if __name__ == "__main__": indented_code = """ def greet(name): print(f"Hello, {name}!") def nested(): print("This is a nested function.") """ dedented_code = dedent(indented_code) print(dedented_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 | def dedent(code): """ Remove the maximum common indentation from a string of Python code. Args: code (str): The Python code to be dedented. Returns: str: The dedented Python code. """ # Split the code into lines lines = code.splitlines() # Find the minimum indentation among non-blank lines min_indent = float('inf') for line in lines: if line.strip(): indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Remove the minimum indentation from each non-blank line dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) # Join the dedented lines back into a single string dedented_code = '\n'.join(dedented_lines) return dedented_code # Example usage if __name__ == "__main__": indented_code = """ def greet(name): print(f"Hello, {name}!") def nested(): print("This is a nested function.") """ dedented_code = dedent(indented_code) print(dedented_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 greet(name): print(f"Hello, {name}!") def nested(): print("This is a nested function.")</p> <p>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.
Here's a Python function `dedent(str)` that takes a string of Python code as input and de-dents it maximally, ignoring entirely 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 | def dedent(code): """ Remove leading indentation from a multi-line string. Args: code (str): A multi-line string of Python code. Returns: str: The input string with leading indentation removed from each line. """ lines = code.splitlines(keepends=True) # Find the minimum indentation of non-blank lines min_indent = float('inf') for line in lines: if line.strip(): min_indent = min(min_indent, len(line) - len(line.lstrip())) # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) return ''.join(dedented_lines) |
1 2 3 4 5 6 7 8 9 | indented_code = """ def hello(): print("Hello, World!") hello() """ dedented_code = dedent(indented_code) print(dedented_code) |
1 2 3 4 | def hello(): print("Hello, World!") hello() |
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 | def dedent(code): """ Remove leading indentation from a multi-line string. Args: code (str): A multi-line string of Python code. Returns: str: The input string with leading indentation removed from each line. """ lines = code.splitlines(keepends=True) # Find the minimum indentation of non-blank lines min_indent = float('inf') for line in lines: if line.strip(): min_indent = min(min_indent, len(line) - len(line.lstrip())) # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) return ''.join(dedented_lines) # Example usage if __name__ == "__main__": indented_code = """ def hello(): print("Hello, World!") hello() """ dedented_code = dedent(indented_code) print(dedented_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 | def dedent(code): """ Remove leading indentation from a multi-line string. Args: code (str): A multi-line string of Python code. Returns: str: The input string with leading indentation removed from each line. """ lines = code.splitlines(keepends=True) # Find the minimum indentation of non-blank lines min_indent = float('inf') for line in lines: if line.strip(): min_indent = min(min_indent, len(line) - len(line.lstrip())) # Remove the minimum indentation from each line dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) return ''.join(dedented_lines) # Example usage if __name__ == "__main__": indented_code = """ def hello(): print("Hello, World!") hello() """ dedented_code = dedent(indented_code) print(dedented_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 | def hello(): print("Hello, World!")</p> <p>hello()</p> <p>All tests passed |
Testing if the previous output contains the string All tests passed
: True