Exemplo n.º 1
0
def test_binary_search_first():
    n = 5
    arr = [1, 2, 3, 4, 5]
    x = 4
    true_key = 3
    key = bin_search(arr, 0, 0, x)
    np.testing.assert_equal(key, true_key)
Exemplo n.º 2
0
def test_binary_search_even_array():
    n = 8
    arr = [1, 2, 3, 4, 5, 6, 7, 8]
    x = 7
    true_key = 6
    key = bin_search(arr, 0, 0, x)
    np.testing.assert_equal(key, true_key)
Exemplo n.º 3
0
def simulation():

    # lists to store # of comparisons
    linear_search = []
    better_linear_search = []
    binary_search = []

    # run experiments
    for i in range(EXPERIMENTS):

        # create a list with some number of values from the given range
        my_list = random.sample(range(MAX_VALUE), LIST_SIZE)
        my_list.sort()
        value = random.sample(range(MAX_VALUE), 1)[0]

        # run linear search, store # of comparisons done
        linear_found, linear_comp = sequential_search(my_list, value)
        linear_search.append(linear_comp)

        # run better linear search, store # of comparisons
        better_found, better_comp = better_sequential_search(my_list, value)
        better_linear_search.append(better_comp)

        # run binary search, store # of comparisons
        binary_found, binary_comp = bin_search(my_list, value)
        binary_search.append(binary_comp)

    # print out average # of comparisons
    print("Sequential", sum(linear_search) / len(linear_search))
    print("Better Sequential", sum(better_linear_search) / len(better_linear_search))
    print("Binary", sum(binary_search) / len(binary_search))
Exemplo n.º 4
0
def test_binary_search_second():
    n = 5
    arr = [11, 22, 33, 44, 55]
    x = 445
    true_key = -1
    key = bin_search(arr, 0, 0, x)
    np.testing.assert_equal(key, true_key)
Exemplo n.º 5
0
def test_binary_search_end_item():
    n = 68
    arr = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22,
        25, 26, 27, 29, 31, 32, 33, 35, 36, 40, 41, 42, 43, 46, 47, 48, 51, 54,
        55, 56, 57, 58, 61, 63, 64, 65, 67, 69, 71, 74, 77, 78, 79, 80, 81, 82,
        83, 85, 86, 87, 88, 89, 90, 91, 93, 95, 99, 100
    ]
    x = 100
    true_key = 67
    key = bin_search(arr, 0, 0, x)
    np.testing.assert_equal(key, true_key)
Exemplo n.º 6
0
    def _select_where_bin(self,
                          return_columns,
                          condition=None,
                          order_by=None,
                          asc=False,
                          top_k=None):
        '''
        Select and return a table containing specified columns and rows where condition is met
        '''

        # if * return all columns, else find the column indexes for the columns specified
        if return_columns == '*':
            return_cols = [i for i in range(len(self.column_names))]
        elif isinstance(return_columns, str):
            raise Exception(
                f'Return columns should be "*" or of type list. (the second parameter is return_columns not condition)'
            )
        else:
            return_cols = [
                self.column_names.index(colname) for colname in return_columns
            ]

        # if condition is None, return all rows
        # if not, return the rows with values where condition is met for value
        if condition is not None:
            column_name, operator, value = self._parse_condition(condition)
            column = self.columns[self.column_names.index(column_name)]
            list_test = bin_search(value, column, operator)
            if list_test == 0:
                rows = []
            else:
                rows = [i for i in list_test]
        else:
            rows = [i for i in range(len(self.columns[0]))]

        # top k rows
        rows = rows[:top_k]
        # copy the old dict, but only the rows and columns of data with index in rows/columns (the indexes that we want returned)
        dict = {(key): ([[self.data[i][j] for j in return_cols]
                         for i in rows] if key == "data" else value)
                for key, value in self.__dict__.items()}

        # we need to set the new column names/types and no of columns, since we might
        # only return some columns
        dict['column_names'] = [self.column_names[i] for i in return_cols]
        dict['column_types'] = [self.column_types[i] for i in return_cols]
        dict['_no_of_columns'] = len(return_cols)

        # order by the return table if specified
        if order_by is None:
            return Table(load=dict)
        else:
            return Table(load=dict).order_by(order_by, asc)
def intersection_binsearch(arr1, arr2):
    inter = []
    larger = []
    smaller = []

    if len(arr1) > len(arr2):
        larger = arr1
        smaller = arr2
    else:
        larger = arr2
        smaller = arr1

    for i in range(len(larger)):
        if bin_search(smaller, larger[i]):
            print ("Appending {} to inter".format(larger[i]))
            inter.append(larger[i])
    return inter
Exemplo n.º 8
0
def union_binsearch(arr1, arr2):
    union = []
    larger = []
    smaller = []

    if len(arr1) > len(arr2):
        larger = arr1
        smaller = arr2
    else:
        larger = arr2
        smaller = arr1

    union = smaller

    for i in range(len(larger)):
        if not bin_search(smaller, larger[i]):
            print("Appending {} to union".format(larger[i]))
            union.append(larger[i])
    return union
 def test_binary_search(self):
     self.assertEquals(bin_search([1, 2, 3, 4, 5], 0, 4, 3), 2)
Exemplo n.º 10
0
    def _select_stack_bin(
        self,
        table_name,
        list_stack,
        return_columns,
        condition=None,
        order_by=None,
        asc=False,
    ):

        # if * return all columns, else find the column indexes for the columns specified
        if return_columns == '*':
            return_cols = [i for i in range(len(self.column_names))]
        elif isinstance(return_columns, str):
            raise Exception(
                f'Return columns should be "*" or of type list. (the second parameter is return_columns not condition)'
            )
        else:
            return_cols = [
                self.column_names.index(colname) for colname in return_columns
            ]

        # if condition is None, return all rows
        # if not, return the rows with values where condition is met for value
        if condition is not None:
            column_name, operator, value = self._parse_condition(condition)
            column = self.columns[self.column_names.index(column_name)]
            list_test = bin_search(value, column, operator)
            if list_test == 0:
                rows1 = []
            else:
                rows1 = [i for i in list_test]

            column2 = list_stack.columns[self.column_names.index(column_name)]
            list_test2 = bin_search(value, column2, operator)
            if list_test2 == 0:
                rows2 = []
            else:
                rows2 = [i for i in list_test2]
        else:
            rows2 = [i for i in range(len(list_stack.columns[0]))]
            rows1 = [i for i in range(len(self.columns[0]))]

        # copy the old dict, but only the rows and columns of data with index in rows/columns (the indexes that we want returned)
        dict = {(key): ([[self.data[i][j] for j in return_cols]
                         for i in rows1] if key == "data" else value)
                for key, value in self.__dict__.items()}
        dict_row = []
        for i in rows2:
            for j in return_cols:
                dict_row.append(list_stack.columns[j][i])
            dict['data'] = dict.get('data', []) + [dict_row]
            dict_row = []
        # we need to set the new column names/types and no of columns, since we might
        # only return some columns
        dict['column_names'] = [self.column_names[i] for i in return_cols]
        dict['column_types'] = [self.column_types[i] for i in return_cols]
        dict['_no_of_columns'] = len(return_cols)

        # order by the return table if specified
        if order_by is None:
            return Table(load=dict)
        else:
            return Table(load=dict).order_by(order_by, asc)