Index Mapping ( or Trivial Hashing ) with negatives allowed | Python3 program to implement direct index mapping with negative values allowed . ; Since array is global , it is initialized as 0. ; Searching if X is Present in the given array or not . ; if X is negative take the absolute value of X . ; Driver code ; Since array is global , it is initialized as 0.
MAX = 1000 NEW_LINE has = [ [ 0 for i in range ( 2 ) ] for j in range ( MAX + 1 ) ] NEW_LINE def search ( X ) : NEW_LINE INDENT if X >= 0 : NEW_LINE INDENT return has [ X ] [ 0 ] == 1 NEW_LINE DEDENT X = abs ( X ) NEW_LINE return has [ X ] [ 1 ] == 1 NEW_LINE DEDENT def insert ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if a [ i ] >= 0 : NEW_LINE INDENT has [ a [ i ] ] [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT has [ abs ( a [ i ] ) ] [ 1 ] = 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ - 1 , 9 , - 5 , - 8 , - 5 , - 2 ] NEW_LINE n = len ( a ) NEW_LINE insert ( a , n ) NEW_LINE X = - 5 NEW_LINE if search ( X ) == True : NEW_LINE INDENT print ( " Present " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Present " ) NEW_LINE DEDENT DEDENT