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 | def dedent(str): """Dedents a string of python code maximally. Lines that are entirely blank don't count. """ lines = str.split('\n') indents = [len(line) - len(line.lstrip()) for line in lines if line] if indents: min_indent = min(indents) lines = [line[min_indent:] for line in lines] return '\n'.join(lines) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 | def dedent(str): """Dedents a string of python code maximally. Lines that are entirely blank don't count. """ lines = str.split('\n') indents = [len(line) - len(line.lstrip()) for line in lines if line] if indents: min_indent = min(indents) lines = [line[min_indent:] for line in lines] return '\n'.join(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 | def dedent(str): """Dedents a string of python code maximally. Lines that are entirely blank don't count. """ lines = str.split('\n') indents = [len(line) - len(line.lstrip()) for line in lines if line] if indents: min_indent = min(indents) lines = [line[min_indent:] for line in lines] return '\n'.join(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 24 25 26 27 28 | def dedent(str): """ Dedents a string of python code maximally. Lines that are entirely blank don't count. Args: str: A string of python code. Returns: A de-dented string of python code. """ # Find the minimum indentation of any line that is not entirely blank. min_indent = None for line in str.splitlines(): if line.strip(): indent = len(line) - len(line.lstrip()) if min_indent is None or indent < min_indent: min_indent = indent # Dedent each line by the minimum indentation. if min_indent is not None: return '\n'.join(line[min_indent:] for line in str.splitlines()) else: return str |
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 | def dedent(str): """ Dedents a string of python code maximally. Lines that are entirely blank don't count. Args: str: A string of python code. Returns: A de-dented string of python code. """ # Find the minimum indentation of any line that is not entirely blank. min_indent = None for line in str.splitlines(): if line.strip(): indent = len(line) - len(line.lstrip()) if min_indent is None or indent < min_indent: min_indent = indent # Dedent each line by the minimum indentation. if min_indent is not None: return '\n'.join(line[min_indent:] for line in str.splitlines()) else: return str |
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 | def dedent(str): """ Dedents a string of python code maximally. Lines that are entirely blank don't count. Args: str: A string of python code. Returns: A de-dented string of python code. """ # Find the minimum indentation of any line that is not entirely blank. min_indent = None for line in str.splitlines(): if line.strip(): indent = len(line) - len(line.lstrip()) if min_indent is None or indent < min_indent: min_indent = indent # Dedent each line by the minimum indentation. if min_indent is not None: return '\n'.join(line[min_indent:] for line in str.splitlines()) else: return str 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 | def dedent(str): """ Dedents a string of python code maximally. Lines that are entirely blank don't count. """ lines = str.splitlines() # Find the minimum indentation of all non-empty lines min_indent = min(len(line) - len(line.lstrip()) for line in lines if line) # Dedent the string return '\n'.join(line[min_indent:] for line in lines) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 | def dedent(str): """ Dedents a string of python code maximally. Lines that are entirely blank don't count. """ lines = str.splitlines() # Find the minimum indentation of all non-empty lines min_indent = min(len(line) - len(line.lstrip()) for line in lines if line) # Dedent the string return '\n'.join(line[min_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 | def dedent(str): """ Dedents a string of python code maximally. Lines that are entirely blank don't count. """ lines = str.splitlines() # Find the minimum indentation of all non-empty lines min_indent = min(len(line) - len(line.lstrip()) for line in lines if line) # Dedent the string return '\n'.join(line[min_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 | def dedent(str): """Dedents a string of python code maximally. Lines that are entirely blank don't count. Args: str: A string of python code. Returns: A dedented string of python code. """ lines = str.splitlines() indents = [len(line) - len(line.lstrip()) for line in lines if line] min_indent = min(indents) if indents else 0 return "\n".join([line[min_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 | def dedent(str): """Dedents a string of python code maximally. Lines that are entirely blank don't count. Args: str: A string of python code. Returns: A dedented string of python code. """ lines = str.splitlines() indents = [len(line) - len(line.lstrip()) for line in lines if line] min_indent = min(indents) if indents else 0 return "\n".join([line[min_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 | def dedent(str): """Dedents a string of python code maximally. Lines that are entirely blank don't count. Args: str: A string of python code. Returns: A dedented string of python code. """ lines = str.splitlines() indents = [len(line) - len(line.lstrip()) for line in lines if line] min_indent = min(indents) if indents else 0 return "\n".join([line[min_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 | def dedent(str): """ Dedents a string of Python code maximally. Lines that are entirely blank don't count. """ lines = str.splitlines() # Find the minimum indentation of any non-blank line. min_indent = len(lines[0]) for line in lines[1:]: if line.strip(): min_indent = min(min_indent, len(line) - len(line.lstrip())) # Dedent all lines by the minimum indentation. return '\n'.join(line[min_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 | def dedent(str): """ Dedents a string of Python code maximally. Lines that are entirely blank don't count. """ lines = str.splitlines() # Find the minimum indentation of any non-blank line. min_indent = len(lines[0]) for line in lines[1:]: if line.strip(): min_indent = min(min_indent, len(line) - len(line.lstrip())) # Dedent all lines by the minimum indentation. return '\n'.join(line[min_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 | def dedent(str): """ Dedents a string of Python code maximally. Lines that are entirely blank don't count. """ lines = str.splitlines() # Find the minimum indentation of any non-blank line. min_indent = len(lines[0]) for line in lines[1:]: if line.strip(): min_indent = min(min_indent, len(line) - len(line.lstrip())) # Dedent all lines by the minimum indentation. return '\n'.join(line[min_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