Exemplo n.º 1
0
 def test_binary_search(self):
     self.assertEqual(binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73], 33), 3)
     self.assertEqual(binarySearch([1, 5, 23, 111],111),3)
     self.assertEqual(binarySearch([1, 5, 23, 111],5),1)
     self.assertEqual(binarySearch([5, 23, 111],3),-1)
     self.assertEqual(binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73],72),8)
     self.assertEqual(binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73],70),-1)
     self.assertEqual(binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73, 355],355),10)
     self.assertEqual(binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73, 355],354),-1)
     self.assertEqual(binarySearch([1, 5, 23, 111],120),-1)
 def test_binarySearch(self):
     self.assertEqual(binarySearch([1, 3, 5, 7, 9, 11], 3), 1)
     self.assertEqual(binarySearch([1, 3, 5, 7, 9, 11], 4), -1)
     self.assertEqual(binarySearch([1, 3, 5, 7, 9, 11], 7), 3)
     self.assertEqual(binarySearch([-5, -3, 1, 3, 5, 7], 7), 5)
     self.assertEqual(binarySearch([-5, -3, 1, 3, 5, 7], -5), 0)
     self.assertEqual(binarySearch([-5, -3, 1, 3, 5, 7], 9), -1)
Exemplo n.º 3
0
#timing linear search
for i in range(n, n * 11, n):
    sizeArray.append(i)
    randomvalues = random.sample(range(i), i)
    startTime = time()
    linearSearch(randomvalues, randomvalues[i - 1])
    endTime = time()
    totalTime = endTime - startTime
    timeArrayLinear.append(totalTime)
    print(totalTime, "for size", i)

n = 100000
i = 0

# timing binary search
for i in range(n, n * 11, n):
    randomvalues = random.sample(range(i), i)
    startTime = time()
    binarySearch(randomvalues, randomvalues[i - 1])
    endTime = time()
    totalTime = endTime - startTime
    timeArrayBinary.append(totalTime)
    print(totalTime, "for size", i)

# draw graph for time vs size for both algorithms
fig, ax = plt.subplots(1, 1)
ax.plot(sizeArray, timeArrayLinear, label='Linear Search')
ax.plot(sizeArray, timeArrayBinary, label='Binary Search')
legend = ax.legend(loc='upper center', shadow=True, fontsize='large')
plt.show()
Exemplo n.º 4
0
 def test_case_10(self):
     self.assertEqual(
         program.binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73, 355],
                              45), 5)
Exemplo n.º 5
0
 def test_case_2(self):
     self.assertEqual(program.binarySearch([1, 5, 23, 111], 5), 1)
Exemplo n.º 6
0
 def test_case_7(self):
     self.assertEqual(
         program.binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73], 70),
         -1)
Exemplo n.º 7
0
from binary_search import binarySearch
from book_handler import load_books

file = "test_dataset.csv"


def is_sorted(lst, key=lambda x: x):
    for i, el in enumerate(lst[1:]):
        if key(el) < key(lst[i]):  # i is the index of the previous element
            return False
    return True


get_id = int(input("Please provide a book id to search: "))
book_list = load_books(file)
ids = [int(book_list[i][0]) for i in range(len(book_list)) if i != 0]
if is_sorted(ids):
    if binarySearch(ids, get_id):
        print(",".join(book_list[get_id + 1]))
Exemplo n.º 8
0
 def test_1(self):
     target = 4
     arr = [0, 1, 2, 3, 4, 5]
     res = binary_search.binarySearch(arr, target)
     self.assertEqual(res, 4)
Exemplo n.º 9
0
 def test_4(self):
     target = 2
     arr = [0, 1]
     res = binary_search.binarySearch(arr, target)
     self.assertEqual(res, -1)
Exemplo n.º 10
0
 def testBinarySearch(self):
     values = [1, 2, 3, 4, 5]
     self.assertEqual(binarySearch(values, 1), 0)
     self.assertEqual(binarySearch(values, 4), 3)
     self.assertEqual(binarySearch(values, 7), -1)