Home > Blockchain >  returning an error of int has no len in python 3.8.3
returning an error of int has no len in python 3.8.3

Time:09-27

I'm facing an issue with int has no len.

Below is the code and the issue

def find_pair(inArr):
    outStr = -1
    for i in range(len(inArr)):
        for j in range(i 1, len(inArr)):
            if len(set(inArr[i]) | set(inArr[j])) == 6:
                if len(inArr[i]   inArr[j]) > len(outStr):
                    outStr = inArr[i]   inArr[j]
    return outStr

inArr = ["30012","250232","53201","3004355","124111"]
print(find_pair(inArr))

The output is below

TypeError                                 Traceback (most recent call last)
<ipython-input-7-6a7c01fe00b4> in <module>
      9 
     10 inArr = ["30012","250232","53201","3004355","124111"]
---> 11 print(find_pair(inArr))

<ipython-input-7-6a7c01fe00b4> in find_pair(inArr)
      4         for j in range(i 1, len(inArr)):
      5             if len(set(inArr[i]) | set(inArr[j])) == 6:
----> 6                 if len(inArr[i]   inArr[j]) > len(outStr):
      7                     outStr = inArr[i]   inArr[j]
      8     return outStr

TypeError: object of type 'int' has no len()

The expected output is

Input: ["30012","250232","53201","3004355","124111"]
Output: 300123004355

And

Input: ["01221","21313","12321"]
Output: -1

Can anyone help me with this issue?

CodePudding user response:

You defined outStr as -1 which is of type integer. I assume you want to define it as an empty string. As a consequence you have to subtract 1 when comparing - as an empty string has a length of 0.

This modified function returns the desired values:

def find_pair(inArr):
    outStr = ""
    for i in range(len(inArr)):
        for j in range(i   1, len(inArr)):
            if len(set(inArr[i]) | set(inArr[j])) == 6:
                if len(inArr[i]   inArr[j]) - 1 > len(outStr):
                    outStr = inArr[i]   inArr[j]
    return outStr or -1

print(find_pair(["30012", "250232", "53201", "3004355", "124111"]))  # 300123004355
print(find_pair(["01221", "21313", "12321"]))  # -1

CodePudding user response:

in this line if len(inArr[i] inArr[j]) > len(outStr): you try to do this len(outStr) when outStr = -1 is integer
so of course you can't measure the length of an integer and you get an error

  • Related