def test_binary_search():
    print("TESTING: binary_search()")

    # Test random arrays a given number of times
    for i in range(100):
        # Create a list of random integers
        list_len = 1024
        my_list = [randint(0, list_len) for i in range(list_len)]

        # Create an arbitrary value to search for and insert into list
        arb_val = randint(0, list_len)
        my_list[randint(0, list_len)] = arb_val

        # Sort the list
        my_list.sort()

        # Search the list for the arbitrary integer
        resulting_idx = binary_search(my_list, arb_val)

        assert my_list[resulting_idx] == arb_val

    # Test specific values at known positions
    my_list = [0, 1, 1, 2, 4, 4, 4, 4, 8, 9, 10, 15, 16, 19, 20]
    assert binary_search(my_list, 0) == 0
    assert binary_search(my_list, 20) == 14
    assert binary_search(my_list, 4) == 7
    assert binary_search(my_list, 8) == 8

    print("PASSED: binary_search()")
Esempio n. 2
0
def show_logout():
    curs.execute(
        "SELECT username FROM timelogs_tbl INNER JOIN timelogsout_tbl ON timelogs_tbl.timelogs_id = "
        "timelogsout_tbl.timelogs_id WHERE timelogs_tbl.day='{}'".format(
            track_time_functions.get_day()))
    got_list = curs.fetchall()
    users_logged_out = [row[0] for row in got_list]
    new_list = []

    curs.execute("SELECT username FROM users_tbl;")
    names = [row[0] for row in curs.fetchall()]

    for username in users_logged_out:
        curs.execute("SELECT fname FROM users_tbl WHERE username={}".format(
            search_algorithms.binary_search(names, username)))
        name_in = [row[0] for row in curs.fetchall()]
        new_list.append(name_in)

    display_user = [user[0] for user in new_list]

    try:
        # writes to a file all logged out users
        with open("LogOut.txt", "w+") as outFile:
            outFile.writelines("Users currently logged in\n")
            for users_name in display_user:
                outFile.writelines("_________\n")
                outFile.writelines(users_name + "\n")

    except EXCEPTION:
        exit("Something went wrong when writing to file. Contact admin")
    finally:
        outFile.close()
Esempio n. 3
0
 def test_search(self):
     self.assertEqual(search_algorithms.binary_search([1, 2, 3, 4, 5], 1),
                      1)
Esempio n. 4
0
import sort_algorithms as Sort
import search_algorithms as Search

if __name__ == "__main__":
    data = [5, 2, 3, 7, 8, 4, 1, 9, 6]

    Sort.insertion_sort(data)

    print(Search.binary_search(data, 2))
    def test_simple_case(self):
        result = binary_search([4, 23, 32, 53, 54, 100, 92, 219, 392], 53)

        self.assertEqual(result, 3)
    def test_no_result(self):
        result = binary_search([4, 23, 32, 53, 54, 100, 92, 219, 392], 414)

        self.assertEqual(result, -1)
    def test_highest_iteration_left_first(self):
        result = binary_search([4, 23, 32, 53, 54, 100, 92, 219, 392], 4)

        self.assertEqual(result, 0)
    def test_value_in_the_middle(self):
        result = binary_search([4, 23, 32, 53, 54, 100, 92, 219, 392], 54)

        self.assertEqual(result, 4)
def test_binary_search_found_mid_position(ordered_list):
    output = binary_search(ordered_list, 56)
    assert output == 1
def test_binary_search_found_last_position(ordered_list):
    output = binary_search(ordered_list, 1000)
    assert output == 4
def test_binary_search_found_first_position(ordered_list):
    output = binary_search(ordered_list, 3)
    assert output == 3
def test_binary_search_not_found(ordered_list):
    output = binary_search(ordered_list, 875)
    assert output == 4