def test_binary_search(self):
        arr1 = [-9, -8, -6, -4, -3, -2, 0, 1, 2, 3, 5, 7, 8, 9]
        arr2 = []

        self.assertEqual(binary_search(arr1, -8, 0, len(arr1)-1), 1)
        self.assertEqual(binary_search(arr1, 0, 0, len(arr1)-1), 6)
        self.assertEqual(binary_search(arr2, 6, 0, len(arr2)-1), -1)
        self.assertEqual(binary_search(arr2, 0, 0, len(arr2)-1), -1)
示例#2
0
    def test_binary_search(self):

        for test_array in self.test_arrays:

            array = test_array()

            for test_search_item in self.test_search_items:

                search_item = test_search_item()
                self.assertEqual(
                    binary_search(array, search_item),
                    self.index_of(array, search_item),
                )
示例#3
0
 def test_search(self):
     array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     testData = [{
         'element': 1,
         'index': 0
     }, {
         'element': 10,
         'index': 9
     }, {
         'element': 4,
         'index': 3
     }, {
         'element': 100,
         'index': -1
     }]
     for data in testData:
         self.assertEqual(data['index'],
                          linear_search(array, data['element']))
         self.assertEqual(data['index'],
                          binary_search(array, data['element']))
         self.assertEqual(data['index'],
                          interpolation_search(array, data['element']))
from searching import binary_search

list1 = []

print(binary_search(list1, -8, 0, len(list1) - 1))
示例#5
0
 def test_binary_search_not_in_list(self):
     search_result = binary_search(self.test_list, 25)
     self.assertEqual(search_result, -1)
#     if tree.contains(name_2):
#         duplicates.append(name_2)

# STRETCH SOLUTION
# Basically I want to search names_1 for each name_2, and add to
# "duplicates" if I find it.
# The most efficient search with arrays is a Binary Search,
# so I'll import my code for that.
import sys
sys.path.append('../../Sorting/src/searching')
from searching import binary_search
# To do a binary search, you first have to sort the search array.
names_1.sort()
for name_2 in names_2:
    # binary_search returns a -1 when the item isn't found,
    # so append to duplicates whenever the result isn't -1.
    if binary_search(names_1, name_2) != -1:
        duplicates.append(name_2)

# The runtime for this ended up being about 0.8 seconds on my computer--
# better than the Binary Search Tree!

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.
示例#7
0
 def test_binary_search_in_list(self):
     search_result = binary_search(self.test_list, 15)
     self.assertEqual(search_result, 4)
示例#8
0
def test_binary_search_first(arr):
    assert binary_search(arr, 2) == 0
    assert binary_search_recursive(arr, 2) == 0
 def test_binary_search(self):
     self.assertEqual(searching.binary_search(self.list_even, 2), 2)
     self.assertEqual(searching.binary_search(self.list_even, 5), 5)
     self.assertEqual(searching.binary_search(self.list_even, 4), 4)
     self.assertEqual(searching.binary_search(self.list_even, 7), 7)
     self.assertEqual(searching.binary_search(self.list_even, 9), None)
     self.assertEqual(searching.binary_search(self.list_odd, 2), 2)
     self.assertEqual(searching.binary_search(self.list_odd, 5), 5)
     self.assertEqual(searching.binary_search(self.list_odd, 7), 7)
     self.assertEqual(searching.binary_search(self.list_odd, 9), None)
     self.assertEqual(searching.binary_search(self.list_one, 1), 1)
     self.assertEqual(searching.binary_search(self.list_one, 2), None)
示例#10
0
def test_binary_search_smaller_than_first(arr):
    assert binary_search(arr, -3) == -1
    assert binary_search_recursive(arr, -3) == -1
示例#11
0
def test_binary_search_greater_than_last(arr):
    assert binary_search(arr, 60) == -1
    assert binary_search_recursive(arr, 60) == -1
示例#12
0
def test_binary_search_end(arr):
    assert binary_search(arr, 57) == 5
    assert binary_search_recursive(arr, 57) == 5
示例#13
0
def test_binary_search_mid(arr):
    assert binary_search(arr, 20) == 3
    assert binary_search_recursive(arr, 20) == 3
示例#14
0
 def test_binary2(self):
     assert searching.binary_search(searching.arr, 11) == "Not-Found"
示例#15
0
 def test_binary1(self):
     assert searching.binary_search(searching.arr, 5) == 5
示例#16
0
# search_lab.py
# In this lab, you will be using two searching algorithms we covered in class to
# search for a word in dictionary. Compare the performance for each algorithm.
# You will have to output the number of steps for both algorithms when used for
# searching for the same word. (case-insensitive)
# Your output should look like this.

# Demo output
# Search word: {"orange"}
# Linear Search: found at {0}, took {0} steps
# Binary Search: found at {0}, took {0} steps

from searching.linear_search import *
from searching.binary_search import *

with open("words") as f:  # open the file (words)
    lines = f.readlines()  # read all lines as a list of lines

    # strip off the newline character for each word in the list
    # "list comprehension" (syntax) - python specific
    # stripped = []
    # for line in lines:
    #     stripped.append(line.strip().lower())
    lines = [line.strip().lower() for line in lines]

    target = input("Search word: ").lower()
    li = linear_search(lines, target)
    bi = binary_search(lines, target)

    print(f"Linear Search: found at {li[0]}, took {li[1]} steps")
    print(f"Binary Search: found at {bi[0]}, took {bi[1]} steps")