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 | def dedent(code): lines = code.split('\n') # Find the minimum number of leading spaces min_spaces = float('inf') for line in lines: if line.strip(): # Skip blank lines leading_spaces = len(line) - len(line.lstrip()) min_spaces = min(min_spaces, leading_spaces) # Remove the minimum number of leading spaces from each line dedented_code = '' for line in lines: dedented_code += line[min_spaces:] + '\n' if line.strip() else '\n' return dedented_code # Test the function code = ''' def hello(): print("Hello, world!") def goodbye(): print("Goodbye, world!") ''' print(dedent(code)) |
1 2 3 4 | def hello(): print("Hello, world!") def goodbye(): print("Goodbye, world!") |
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): lines = code.split('\n') # Find the minimum number of leading spaces min_spaces = float('inf') for line in lines: if line.strip(): # Skip blank lines leading_spaces = len(line) - len(line.lstrip()) min_spaces = min(min_spaces, leading_spaces) # Remove the minimum number of leading spaces from each line dedented_code = '' for line in lines: dedented_code += line[min_spaces:] + '\n' if line.strip() else '\n' return dedented_code # Test the function code = ''' def hello(): print("Hello, world!") def goodbye(): print("Goodbye, world!") ''' print(dedent(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 | def dedent(code): lines = code.split('\n') # Find the minimum number of leading spaces min_spaces = float('inf') for line in lines: if line.strip(): # Skip blank lines leading_spaces = len(line) - len(line.lstrip()) min_spaces = min(min_spaces, leading_spaces) # Remove the minimum number of leading spaces from each line dedented_code = '' for line in lines: dedented_code += line[min_spaces:] + '\n' if line.strip() else '\n' return dedented_code # Test the function code = ''' def hello(): print("Hello, world!") def goodbye(): print("Goodbye, world!") ''' print(dedent(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!")
def goodbye():
print("Goodbye, world!")
Traceback (most recent call last):
File "/usr/src/app/main.py", line 45, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got print("hello")
print("world")
if True:
print("true")
else:
print("false")
instead of print("hello")
print("world")
if True:
print("true")
else:
print("false")
Testing if the previous output contains the string All tests passed
: False
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 is the `dedent` function:
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_code = '\n'.join(line[min_indent:] if line.strip() != '' else line for line in lines) return dedented_code |
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_code = '\n'.join(line[min_indent:] if line.strip() != '' else line for line in 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 | 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_code = '\n'.join(line[min_indent:] if line.strip() != '' else line for line in 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def dedent(code): lines = code.split('\n') min_indent = float('inf') for line in lines: if line.strip(): # ignore blank lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) dedented_code = '' for line in lines: if line.strip(): # ignore blank lines dedented_code += line[min_indent:] + '\n' return dedented_code.rstrip('\n') |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def dedent(code): lines = code.split('\n') min_indent = float('inf') for line in lines: if line.strip(): # ignore blank lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) dedented_code = '' for line in lines: if line.strip(): # ignore blank lines dedented_code += line[min_indent:] + '\n' return dedented_code.rstrip('\n') |
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 | def dedent(code): lines = code.split('\n') min_indent = float('inf') for line in lines: if line.strip(): # ignore blank lines indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) dedented_code = '' for line in lines: if line.strip(): # ignore blank lines dedented_code += line[min_indent:] + '\n' return dedented_code.rstrip('\n') 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') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 36, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got print("hello")
print("world")
if True:
print("true")
else:
print("false") instead of print("hello")
print("world")
if True:
print("true")
else:
print("false")
Testing if the previous output contains the string All tests passed
: False
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 29 30 | def dedent(code): lines = code.split('\n') # Remove leading and trailing blank lines while lines and not lines[0].strip(): lines.pop(0) while lines and not lines[-1].strip(): lines.pop() # Determine the maximum common leading whitespace min_indent = float('inf') for line in lines: if line.strip(): indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent the code dedented_code = '' for line in lines: if line.strip(): dedented_code += line[min_indent:] dedented_code += '\n' return dedented_code # Test the function code = """ def add(a, b): return a + b """ print(dedent(code)) |
1 2 | def add(a, b): return a + b |
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 | def dedent(code): lines = code.split('\n') # Remove leading and trailing blank lines while lines and not lines[0].strip(): lines.pop(0) while lines and not lines[-1].strip(): lines.pop() # Determine the maximum common leading whitespace min_indent = float('inf') for line in lines: if line.strip(): indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent the code dedented_code = '' for line in lines: if line.strip(): dedented_code += line[min_indent:] dedented_code += '\n' return dedented_code # Test the function code = """ def add(a, b): return a + b """ print(dedent(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 | def dedent(code): lines = code.split('\n') # Remove leading and trailing blank lines while lines and not lines[0].strip(): lines.pop(0) while lines and not lines[-1].strip(): lines.pop() # Determine the maximum common leading whitespace min_indent = float('inf') for line in lines: if line.strip(): indent = len(line) - len(line.lstrip()) min_indent = min(min_indent, indent) # Dedent the code dedented_code = '' for line in lines: if line.strip(): dedented_code += line[min_indent:] dedented_code += '\n' return dedented_code # Test the function code = """ def add(a, b): return a + b """ print(dedent(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 add(a, b):
return a + b
Traceback (most recent call last):
File "/usr/src/app/main.py", line 51, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got print("hello")
print("world")
if True:
print("true")
else:
print("false")
instead of print("hello")
print("world")
if True:
print("true")
else:
print("false")
Testing if the previous output contains the string All tests passed
: False
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 is a possible implementation of the `dedent` function:
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(code): lines = code.split('\n') # Find the minimum number of leading spaces in non-empty 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 indent from each line dedented_code = "" for line in lines: if line.strip() != "": dedented_code += line[min_indent:] + '\n' else: dedented_code += '\n' return dedented_code # Test the function code = """ def add(x, y): return x + y result = add(3, 5) print(result) """ dedented_code = dedent(code) print(dedented_code) |
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 | def dedent(code): lines = code.split('\n') # Find the minimum number of leading spaces in non-empty 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 indent from each line dedented_code = "" for line in lines: if line.strip() != "": dedented_code += line[min_indent:] + '\n' else: dedented_code += '\n' return dedented_code # Test the function code = """ def add(x, y): return x + y result = add(3, 5) print(result) """ 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 | def dedent(code): lines = code.split('\n') # Find the minimum number of leading spaces in non-empty 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 indent from each line dedented_code = "" for line in lines: if line.strip() != "": dedented_code += line[min_indent:] + '\n' else: dedented_code += '\n' return dedented_code # Test the function code = """ def add(x, y): return x + y result = add(3, 5) print(result) """ 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 add(x, y):
return x + y
result = add(3, 5)
print(result)
Traceback (most recent call last):
File "/usr/src/app/main.py", line 51, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got print("hello")
print("world")
if True:
print("true")
else:
print("false")
instead of print("hello")
print("world")
if True:
print("true")
else:
print("false")
Testing if the previous output contains the string All tests passed
: False