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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) return '\n'.join(dedented_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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) return '\n'.join(dedented_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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [] for line in lines: if line.strip(): dedented_lines.append(line[min_indent:]) else: dedented_lines.append(line) return '\n'.join(dedented_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.
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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) 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 11 12 | code = ''' def hello(): print("Hello, world!") if True: print("This is inside the if block") hello() ''' dedented_code = dedent(code) print(dedented_code) |
1 2 3 4 5 6 7 | def hello(): print("Hello, world!") if True: print("This is inside the if block") hello() |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_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.
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 | def dedent(code): lines = code.split('\n') 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) dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_lines) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 | def dedent(code): lines = code.split('\n') 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) dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_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 | def dedent(code): lines = code.split('\n') 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) dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] return '\n'.join(dedented_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.
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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip() != ''] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [line[min_indent:] for line in lines] dedented_code = '\n'.join(dedented_lines) return dedented_code |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip() != ''] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [line[min_indent:] for line in lines] dedented_code = '\n'.join(dedented_lines) return 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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip() != ''] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [line[min_indent:] for line in lines] dedented_code = '\n'.join(dedented_lines) return 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') |
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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] dedented_code = '\n'.join(dedented_lines) return dedented_code |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] dedented_code = '\n'.join(dedented_lines) return 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 | def dedent(code): lines = code.split('\n') non_empty_lines = [line for line in lines if line.strip()] if not non_empty_lines: return code min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines) dedented_lines = [line[min_indent:] if line.strip() else line for line in lines] dedented_code = '\n'.join(dedented_lines) return 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') |
All tests passed
Testing if the previous output contains the string All tests passed
: True