Skip to content

Commit bf9b5af

Browse files
committed
Fix multiple string split bugs
1 parent b5c55f4 commit bf9b5af

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

autoload/leetcode.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,23 @@ def get_problems(categories):
234234
return sorted(problems, key=lambda p: p['id'])
235235

236236

237+
def _split(s):
238+
if isinstance(s, list):
239+
lines = []
240+
for element in s:
241+
lines.extend(_split(element))
242+
return lines
243+
244+
# Replace all \r\n to \n and all \r (alone) to \n
245+
s = s.replace('\r\n', '\n').replace('\r', '\n').replace('\0', '\n')
246+
# str.split has an disadvantage that ''.split('\n') results in [''], but what we want
247+
# is []. This small function returns [] if `s` is a blank string, that is, containing no
248+
# characters other than whitespaces.
249+
if s.strip() == '':
250+
return []
251+
return s.split('\n')
252+
253+
237254
def get_problem(slug):
238255
assert is_login()
239256
headers = _make_headers()
@@ -277,23 +294,14 @@ def get_problem(slug):
277294
for t in json.loads(q['codeDefinition']):
278295
problem['templates'][t['value']] = _break_code_lines(t['defaultCode'])
279296
problem['testable'] = q['enableRunCode']
280-
problem['testcase'] = q['sampleTestCase']
297+
problem['testcase'] = _split(q['sampleTestCase'])
281298
stats = json.loads(q['stats'])
282299
problem['total_accepted'] = stats['totalAccepted']
283300
problem['total_submission'] = stats['totalSubmission']
284301
problem['ac_rate'] = stats['acRate']
285302
return problem
286303

287304

288-
def _split(s):
289-
# str.split has an disadvantage that ''.split('\n') results in [''], but what we want
290-
# is []. This small function returns [] if `s` is a blank string, that is, containing no
291-
# characters other than whitespaces.
292-
if s.strip() == '':
293-
return []
294-
return s.split('\n')
295-
296-
297305
def _check_result(submission_id):
298306
global task_progress
299307
if _in_task():
@@ -333,7 +341,7 @@ def _check_result(submission_id):
333341
'testcase': _split(r.get('input', r.get('last_testcase', ''))),
334342
'passed': r.get('total_correct') or 0,
335343
'total': r.get('total_testcases') or 0,
336-
'error': [v for k, v in r.items() if 'error' in k and v]
344+
'error': _split([v for k, v in r.items() if 'error' in k and v])
337345
}
338346

339347
# the keys differs between the result of testing the code and submitting it

autoload/leetcode.vim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,7 @@ function! s:AskTestInputAndRunTest(problem, filetype, code) abort
832832
if has_key(s:saved_test_input, slug)
833833
let default_test_input = s:saved_test_input[slug]
834834
else
835-
let default_test_input = ['# Test Input'] +
836-
\ split(a:problem['testcase'], '\n', 1)
835+
let default_test_input = ['# Test Input'] + a:problem['testcase']
837836
let s:saved_test_input[slug] = default_test_input
838837
endif
839838

0 commit comments

Comments
 (0)