Skip to content

Commit 3e6fa44

Browse files
committed
Ignore Whitespaces in submission when compared to model answer
1 parent 6226053 commit 3e6fa44

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

exercise/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ def get_compared_submission_file_data(self, submission_id: int):
570570
except ValueError as e:
571571
raise Http404() from e
572572

573+
@staticmethod
574+
def strip_trailing_whitespace(text: str) -> str:
575+
return '\n'.join(line.rstrip() for line in text.splitlines())
576+
573577
def get(self, request, *args, **kwargs):
574578
try:
575579
with self.file.file_object.open() as f:
@@ -586,9 +590,15 @@ def get(self, request, *args, **kwargs):
586590
else self.get_compared_submission_file_data(compare_to)
587591
)
588592
submitted_data = bytedata.decode('utf-8', 'ignore')
593+
594+
compared_data = self.strip_trailing_whitespace(compared_data)
595+
submitted_data = self.strip_trailing_whitespace(submitted_data)
596+
597+
589598
# Ensure that both files end with a newline, otherwise the diff output might be mangled
590599
submitted_data += '\n' if not submitted_data.endswith('\n') else ''
591600
compared_data += '\n' if not compared_data.endswith('\n') else ''
601+
592602
diff = ndiff(compared_data.splitlines(keepends=True), submitted_data.splitlines(keepends=True))
593603
diff_text = ''.join([line for line in diff if line[0] != '?'])
594604
bytedata = diff_text.encode('utf-8')

0 commit comments

Comments
 (0)