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 | def dedent(code): """De-dents a string of Python code maximally. This function calculates the minimum indentation level (excluding entirely blank lines) and removes that amount of indentation from all lines in the code. Args: code: A string of Python code. Returns: The de-dented Python code. """ lines = code.splitlines() min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent_level = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent_level) de_dented_lines = [line[min_indent:] for line in lines] return '\n'.join(de_dented_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 | def dedent(code): """De-dents a string of Python code maximally. This function calculates the minimum indentation level (excluding entirely blank lines) and removes that amount of indentation from all lines in the code. Args: code: A string of Python code. Returns: The de-dented Python code. """ lines = code.splitlines() min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent_level = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent_level) de_dented_lines = [line[min_indent:] for line in lines] return '\n'.join(de_dented_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 | def dedent(code): """De-dents a string of Python code maximally. This function calculates the minimum indentation level (excluding entirely blank lines) and removes that amount of indentation from all lines in the code. Args: code: A string of Python code. Returns: The de-dented Python code. """ lines = code.splitlines() min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent_level = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent_level) de_dented_lines = [line[min_indent:] for line in lines] return '\n'.join(de_dented_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
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 | def dedent(code): """De-dents a string of Python code maximally, ignoring fully blank lines. Args: code: The Python code string to dedent. Returns: The dedented code string. """ lines = code.splitlines() non_empty_lines = [line for line in lines if line.strip()] # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in non_empty_lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent all lines by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) |
1 2 3 4 5 6 7 8 9 10 | code = """ def my_function(): print("Hello, world!") if __name__ == "__main__": my_function() """ dedented_code = dedent(code) print(dedented_code) |
1 2 3 4 5 | def my_function(): print("Hello, world!") if __name__ == "__main__": my_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 | def dedent(code): """De-dents a string of Python code maximally, ignoring fully blank lines. Args: code: The Python code string to dedent. Returns: The dedented code string. """ lines = code.splitlines() non_empty_lines = [line for line in lines if line.strip()] # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in non_empty_lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent all lines by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) if __name__ == "__main__": code = """ def my_function(): print("Hello, world!") if __name__ == "__main__": my_function() """ dedented_code = dedent(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 | def dedent(code): """De-dents a string of Python code maximally, ignoring fully blank lines. Args: code: The Python code string to dedent. Returns: The dedented code string. """ lines = code.splitlines() non_empty_lines = [line for line in lines if line.strip()] # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in non_empty_lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent all lines by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) if __name__ == "__main__": code = """ def my_function(): print("Hello, world!") if __name__ == "__main__": my_function() """ dedented_code = dedent(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') |
def my_function():
print("Hello, world!")
if __name__ == "__main__":
my_function()
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | def dedent(code): """De-dents a string of Python code maximally. This function calculates the maximum common indentation level across all non-blank lines in the input code and then removes that amount of indentation from each line. Args: code: A string of Python code. Returns: The de-dented string of Python code. """ lines = code.splitlines() common_indent = float('inf') for line in lines: if line.strip(): # Ignore blank lines indent = len(line) - len(line.lstrip()) common_indent = min(common_indent, indent) return '\n'.join([line[common_indent:] for line in 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 | def dedent(code): """De-dents a string of Python code maximally. This function calculates the maximum common indentation level across all non-blank lines in the input code and then removes that amount of indentation from each line. Args: code: A string of Python code. Returns: The de-dented string of Python code. """ lines = code.splitlines() common_indent = float('inf') for line in lines: if line.strip(): # Ignore blank lines indent = len(line) - len(line.lstrip()) common_indent = min(common_indent, indent) return '\n'.join([line[common_indent:] for line in 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 | def dedent(code): """De-dents a string of Python code maximally. This function calculates the maximum common indentation level across all non-blank lines in the input code and then removes that amount of indentation from each line. Args: code: A string of Python code. Returns: The de-dented string of Python code. """ lines = code.splitlines() common_indent = float('inf') for line in lines: if line.strip(): # Ignore blank lines indent = len(line) - len(line.lstrip()) common_indent = min(common_indent, indent) return '\n'.join([line[common_indent:] for line in 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
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 | def dedent(code): """ De-dents a string of Python code maximally, ignoring entirely blank lines. Args: code: The Python code string to dedent. Returns: The dedented Python code string. """ lines = code.splitlines() # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent each line by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) |
1 2 3 4 5 6 7 8 9 | code = """ def my_function(): print("Hello, world!") print("Outside the function") """ dedented_code = dedent(code) print(dedented_code) |
1 2 3 4 | def my_function(): print("Hello, world!") print("Outside the 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 | def dedent(code): """ De-dents a string of Python code maximally, ignoring entirely blank lines. Args: code: The Python code string to dedent. Returns: The dedented Python code string. """ lines = code.splitlines() # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent each line by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) if __name__ == "__main__": code = """ def my_function(): print("Hello, world!") print("Outside the function") """ dedented_code = dedent(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 | def dedent(code): """ De-dents a string of Python code maximally, ignoring entirely blank lines. Args: code: The Python code string to dedent. Returns: The dedented Python code string. """ lines = code.splitlines() # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent each line by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) if __name__ == "__main__": code = """ def my_function(): print("Hello, world!") print("Outside the function") """ dedented_code = dedent(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') |
def my_function():
print("Hello, world!")
print("Outside the function")
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.
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): """De-dents a string of Python code maximally. This function removes leading whitespace from each line in the input string, ensuring that the least indented lines (excluding entirely blank lines) start at the beginning of the line. Args: code: The string of Python code to dedent. Returns: The dedented string of Python code. """ lines = code.splitlines() # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent all lines by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) |
1 2 3 4 5 6 7 8 9 | code = """ def my_function(): # This is indented print("Hello!") """ dedented_code = dedent(code) print(dedented_code) |
1 2 3 | def my_function(): # This is indented print("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 | def dedent(code): """De-dents a string of Python code maximally. This function removes leading whitespace from each line in the input string, ensuring that the least indented lines (excluding entirely blank lines) start at the beginning of the line. Args: code: The string of Python code to dedent. Returns: The dedented string of Python code. """ lines = code.splitlines() # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent all lines by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) if __name__ == "__main__": code = """ def my_function(): # This is indented print("Hello!") """ dedented_code = dedent(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 | def dedent(code): """De-dents a string of Python code maximally. This function removes leading whitespace from each line in the input string, ensuring that the least indented lines (excluding entirely blank lines) start at the beginning of the line. Args: code: The string of Python code to dedent. Returns: The dedented string of Python code. """ lines = code.splitlines() # Find the minimum indentation level among non-empty lines min_indent = float('inf') for line in lines: if line.strip(): # Ignore empty lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent all lines by the minimum indentation level dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) if __name__ == "__main__": code = """ def my_function(): # This is indented print("Hello!") """ dedented_code = dedent(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') |
def my_function():
# This is indented
print("Hello!")
All tests passed
Testing if the previous output contains the string All tests passed
: True