a = [1,2,2,1]
b = [2,2]
if len(a) > len(b):
my_list = a
target = b
else:
my_list = b
target = a
ans = []
min = 0
max = len(my_list) -1
for i in target: #2
while min <= max: #min = 0 max = 3
mid = (min+max)//2 #1
guess = my_list[mid] # index-1(2 -value)
if guess == i: #2==2
ans.append(guess)
my_list = my_list.pop(my_list[mid])
if guess < i:
min = mid+1
else:
max = mid-1
I'm trying to solve the problem: Given two integer arrays a and b, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
But the error occurs:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[95], line 14
12 while min <= max: #min = 0 max = 3
13 mid = (min+max)//2 #1
---> 14 guess = my_list[mid] # index-1(2 -value)
15 if guess == i: #2==2
16 ans.append(guess)
TypeError: 'int' object is not subscriptable
enter code here
Why it occurs and how can i fix it? P.S: please don't pay attention to another possible mistakes, I want to handle them by myself
The problem is that the .pop() function doesn't work the way you have used it in your program just like slothrop points out. To remove an item in a list at index i you would just need to do myList.pop(i)
. So that is the first problem you have in your code.
This should fix your error however your code would still not accomplish the task you mentioned. First of all, you would need to make sure that you are performing the binary search in a sorted list, which a
is not as it is [1, 2, 2, 1]. So you would need to sort it beforehand using the built-in sort function. Second of all, even after you delete the value which you found in the array, it only deletes one instance of it, so you could end up with duplicates. Therefore, it would be better to use a set to store the results instead of a list as a set does not allow for duplicates. Lastly, the most important mistake in the code is that you don't reset the max and min values every time you end one binary search. You keep the previous max and min which means that the intersection won't be performed correctly.
a = [1, 2, 2, 1]
b = [2, 2]
if len(a) > len(b):
my_list = a
target = b
else:
my_list = b
target = a
ans = set()
min = 0
max = len(my_list)-1
my_list.sort() # sort for binary search
for i in target:
min = 0 # reset min
max = len(my_list)-1 # reset max
while min <= max:
mid = (min+max)//2
guess = my_list[mid]
if guess == i:
ans.add(guess)
my_list.pop(mid)
break # break loop
elif guess < i:
min = mid+1
else:
max = mid-1
print(list(ans)) # turn into list
Most of the code however is not needed as you could very easily convert both lists into sets and then join them together using intersection_update()
function of python.