What is wrong with my python code


  • 0
    H

    I just tried the following code:

    class Solution(object):
        def numberOfBoomerangs(self, points):
            """
            :type points: List[List[int]]
            :rtype: int
            """
            dics = []
            for i in range(len(points)):
                subDic = {}
                for j in range(len(points)):
                    dis = math.sqrt((points[i][0] - points[j][0])**2 + (points[i][1] - points[j][1])**2)
                    subDic[dis] = subDic[dis].append(points[j]) if dis in subDic else [points[j]]
                dics.append(subDic)
            count = 0
            for dic in dics:
                for key in dic:
                    if len(dic[key]) >= 2:
                        count += math.factorial(len(dic[key]))
            return count
    

    It failed at line 18 which is if len(dic[key]) >= 2: with error of Line 18: TypeError: object of type 'NoneType' has no len(). The test input is [[0,0],[1,0],[2,0]]

    I was trying to debug it but wasn't able to figure it our. Can anyone help with this please?


  • 0

    "for key in dic:" then 'key' should be the key of dic and dic should be subDic above. But key of subDic should be dis which is a real number. Maybe what you want is len(subDic[dis]), not len(dis)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.