Executing Directories in Python

Ever wondered where is the starting point of the program in python. Though python is interpreted line by line from the source file, but don’t we need a file where our main function or main class persists. Programmers from C++ and Java background will be definitely obsessed with *void main()*.

In case of Java or C++ we are giving a particular file to the compiler in which main function resides.

But in case of python we can give any .py file to the python interpreter and it reads it line by line and executes it irrespective of the contents.

Here is a answer for the beginners learning python. We have __main__.py file in a directory which acts as a entry point of the execution.

Step 1: Create a directory e.g. "mathops" (or any name you like)
Step 2: Create "printcontent.py" in mathops directory.
paste the following -
def printContent(content):
      print(content)
Step 3:Open Terminal and pass mathops directory to python interpreter.
>python mathops
python.exe: can't find '__main__' module in 'mathops'
(it gives the above error because interpreter is expecting __main__.py)

Step 4:Create __main__.py in mathops directory.
paste the following -

import printcontent

print("executing __main__")
printcontent.printContent("This content is passed to function")

Output: 
executing __main__
This content is passed to function

Using this feature of python we can define an entry point of executing a directory(project) in python where __main__.py can import and execute logic present in other python files in the scope of the interpreter.

Binary Search (Python)

The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).

input_int_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
input_alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ]
input_words = ["Ajinkya", "Abhijeet", "Abhishek", "Prakash", "Sunil"]

# Function for binary search in a sorted array
def binary_search(input_list_param, element):

    midpoint = len(input_list_param) // 2

    if len(input_list_param) != 0:
        if input_list_param[midpoint] == element:
            return True
        else:
            if element < input_list_param[midpoint]:
                return binary_search(input_list_param[:midpoint], element)
            else:
                return binary_search(input_list_param[midpoint + 1:], element)
    return False


if __name__ == "__main__":
    print("Found 3: ", binary_search(input_int_list, 3))
    print("Found j: ",binary_search(input_alphabets, 'j'))
    print("Found 'Prakash': ",binary_search(input_words, "Prakash"))

Output:

Found 3: True
Found j: True
Found ‘Prakash’: True

String Reversal in JAVA with O(n * log(n))

A normal String Reversal program may take a brute force method with O(n). But with the help of recursion we can reduce the complexity, making it as O(n* log(n)).

Input: mahabaleshwar

Output: rawhselabaham

public class StringUtility{

     public static String getReverse(String in){
           int length = s.length();
           
           if(length <= 1)
              return in;
    
           String leftSubStr = in.substring(0, length / 2);
           String rightSubStr = in.substring(length / 2, length );

           return getReverse(rightSubStr) + getReverse(leftSubStr);
     }
}