I'm getting errors saying that there is a syntax error for the "makeShortest" function that I defined.
- What is the problem here?
- Are there any other issues that might make this function not work at runtime?
def solution(A):
idxDict = defaultdict(list)
for i in range(len(A)):
idxDict[A[i]].append(i)
ans = -1
A.sort()
if len(A) <= 1:
return ans
hasAdj = 0
for i in range(len(A) - 1):
if A[i] != A[i + 1]:
hasAdj += 1
if hasAdj == 1:
ans = makeShortest(idxDict[A[i]], idxDict[A[i + 1]])
else:
ans = min(ans, makeShortest(idxDict[A[i]], idxDict[A[i + 1]]))
return ans
def makeShortest (list1, list2):
ans = abs(list1[0] - list2[0])
for k in range(len(list1)):
for l in range(len(list2)):
ans = min(ans, abs(list1[k] - list2[l])
return ans
1 Answer 1
Is ans2 in this line the error?
ans = min(ans2, abs(list1[k] - idxDict[list2[l]))
Sign up to request clarification or add additional context in comments.
4 Comments
jginso7
Sorry, that's a typo. The error is on
def makeShortest (list1, list2) with "invalid syntax(<unknown>, line 28) pylint(syntax-error) [28,34] " and line 28 is def makeShortest (list1, list2)Matthew
I found another error in the same line:
ans = min(ans2, abs(list1[k] - idxDict[list2[l])) The idxDict is missing a closing ] so it should be ans = min(ans2, abs(list1[k] - idxDict[list2[l]]))jginso7
So I fixed my code for makeShortest() like how I did on the above question, but now I got error on the return statement of makeShortest with message "invalid syntax(<unknown>, line 33) pylint(syntax-error) [33, 6]
Matthew
On the line 32 the code is missing a closing parenthesis
ans = min(ans, abs(list1[k] - list2[l]) It should be ans = min(ans, abs(list1[k] - list2[l]))lang-py
def makeShortest (list1, list2)with "pylint(syntax-error)