I am building a function to split an input string into two lists as they are numbers, and find an intersection between both. My code runs fine in VSCode(Desktop) Python 3.12, no errors, but when I tried to run it on a Cloud(Coderbytes), returns an error saying the input is a list, and hence it has no split attribute … Really confusing
def FindIntersection(strArr):
# Split the input string using '"' and extract the lists
strArr = strArr.split('"')
# The lists are at indices 1 and 3 after splitting by '"'
first = strArr[1]
second = strArr[3]
# Convert the strings to lists of integers
One = [int(x) for x in first.strip("[] ").split(", ")]
Two = [int(x) for x in second.strip("[] ").split(", ")]
# Find the intersection using set intersection
intersection = list(set(One).intersection(Two))
# Create a comma-separated string from the intersection
strArr = ", ".join(map(str, intersection))
# Return the result
return strArr
# Keep this function call here
print(FindIntersection(input()))
If I enter this string as input
[“1, 3, 4, 7, 13”, “1, 2, 4, 13, 15”]
The Output is: 1,4,13
and is fine in VSCode desktop version, but in
Coderbytes Cloud interpreter it gets the error
*
Traceback (most recent call last):
File “/home/glot/main.py”, line 23, in
print(FindIntersection([“1, 3, 4, 7, 13”, “1, 2, 4, 13, 15”]))
File “/home/glot/main.py”, line 3, in FindIntersection
strArr = strArr.split(‘”‘)
AttributeError: ‘list’ object has no attribute ‘split’*
So my question is why Coderbytes recognizes the string as a list?
While VSCode validates it?