Skip to content
Advertisement

Why does a function in a class does not return anything?

For exercising reasons, I am trying to implement a class SSM which stands for Static Sorted Map in python in order to implement the methods

min_value(self) : find the minimum value

max_value(self) : find the maximum value

search(self, key): to find an element in the list

The list is assumed to be sorted.

Here is the code for the class:

JavaScript

As you can see from the code, for the method search (self, K) I have an inner function __Bin_Search(s, e, K) which goes recursive on the left or right of the list in order to find the element (it is based on the Binary Search Algorithm). And so, I expect that the methods search (self, K) returns the result given by __Bin_Search since it is called in the last line.

My problem is that by using search(self, K) nothing is returned.

JavaScript

Where is the error in the code? How can I fix that?

Advertisement

Answer

Your list always has a length greater than 0 so s will never equal e and therefore never reach a return statement. You need to add a conditional statement in __Bin_Search where s != e.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement