0

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
2
  • 1
    What is the exact error you're receiving? Commented Sep 7, 2020 at 20:37
  • Sorry, that's a typo. The error is on def makeShortest (list1, list2) with "pylint(syntax-error) Commented Sep 7, 2020 at 22:06

1 Answer 1

2

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

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)
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]]))
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]
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]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.