I have no clue why this is occurring. My class function isn't returning None regardless of how I write my code. Here's my search function below:
Just before the above snippet of code, self.truck.boxes[start].volume() and self.truck.boxes[end].volume() are NOT equal to the volume of the input box (I'm 99% sure this is true, I debugged via print functions), and therefore the first condition should be satisfied and the code should return None, right?
However, when I do run my code, my function returns some generator instead: <generator object OrganizedTruck.search at 0x00000272D1424258>
Why isn't my function returning None? I know for sure that that condition should be triggered, so I have no explanation for this other than Python is ignoring the condition completely...
def search(self, box):
if len(self.truck.boxes) == 0:
return None
start = 0
end = len(self.truck.boxes) - 1
while end - start > 1:
mid = (start + end) // 2
if self.truck.boxes[end] < self.truck.boxes[start]:
if self.truck.boxes[mid].volume() <= box.volume() and box.volume() <= self.truck.boxes[end].volume(): ##indent is okay?
start = mid
elif self.truck.boxes[mid].volume() <= box.volume() and box.volume() >= self.truck.boxes[end].volume():
end = mid
elif self.truck.boxes[mid].volume() > box.volume() and box.volume() >= self.truck.boxes[start].volume():
end = mid
elif self.truck.boxes[mid].volume() > box.volume() and box.volume() <= self.truck.boxes[end].volume():
start = mid
elif self.truck.boxes[end] > self.truck.boxes[start]:
if self.truck.boxes[mid].volume() <= box.volume():
start = mid
else: #self.truck.boxes[mid].volume() > box.volume()
end = mid
right = None
if self.truck.boxes[start].volume() != box.volume() and self.truck.boxes[end] != box.volume():
return None
elif self.truck.boxes[start].volume() == box.volume():
right = start
elif self.truck.boxes[end].volume() == box.volume():
right = end
start = 0
while right - start > 1:
mid = (right + start) // 2
if self.truck.boxes[mid].volume() == self.truck.boxes[right].volume():
right = mid
else:
start = mid
begin = 0
if self.truck.boxes[start].volume() == box.volume():
begin = start
else: # self.truck.boxes[right].volume() == box.volume():
begin = right
while self.truck.boxes[begin].volume() == box.volume():
yield self.truck.boxes[begin]
begin += 1Particularly this snippet of the code here:right = None
if self.truck.boxes[start].volume() != box.volume() and self.truck.boxes[end] != box.volume():
return None
elif self.truck.boxes[start].volume() == box.volume():
right = start
elif self.truck.boxes[end].volume() == box.volume():
right = endI can assure (through my own testing) that the code should be returning None here when I input a box object whose volume is not found in the OrganizedTruck's list of boxes. Just before the above snippet of code, self.truck.boxes[start].volume() and self.truck.boxes[end].volume() are NOT equal to the volume of the input box (I'm 99% sure this is true, I debugged via print functions), and therefore the first condition should be satisfied and the code should return None, right?
However, when I do run my code, my function returns some generator instead: <generator object OrganizedTruck.search at 0x00000272D1424258>
Why isn't my function returning None? I know for sure that that condition should be triggered, so I have no explanation for this other than Python is ignoring the condition completely...
