def test_nothing_is_found_when_the_left_and_right_bounds_cross(self):

        with self.assertRaises(ValueError) as err:
            find([1, 2], 0)

        self.assertEqual(type(err.exception), ValueError)
        self.assertEqual(err.exception.args[0], "value not in array")
    def test_a_value_larger_than_the_array_s_largest_value_is_not_found(self):

        with self.assertRaises(ValueError) as err:
            find([1, 3, 4, 6, 8, 9, 11], 13)

        self.assertEqual(type(err.exception), ValueError)
        self.assertEqual(err.exception.args[0], "value not in array")
    def test_nothing_is_found_in_an_empty_array(self):

        with self.assertRaises(ValueError) as err:
            find([], 1)

        self.assertEqual(type(err.exception), ValueError)
        self.assertEqual(err.exception.args[0], "value not in array")
    def test_identifies_that_a_value_is_not_included_in_the_array(self):

        with self.assertRaises(ValueError) as err:
            find([1, 3, 4, 6, 8, 9, 11], 7)

        self.assertEqual(type(err.exception), ValueError)
        self.assertEqual(err.exception.args[0], "value not in array")
Exemplo n.º 5
0
 def test_in_order(self):
     "A test method, which must start with test_"
     # You can have many test vectors.
     # Each one contains at least the input that you
     # feed to your function, or whatever code you want to test.
     # You can group a lot of them together,
     # if you're testing the same feature.
     # Here we have two test vectors,
     # inside one test method, which must start with "test_"
     in_order = [([1, 2, 3, 4], 3), ([-4, 3, 6, 19], 2)]
     # It's always a good idea to have a different technique
     # for testing the result, rather than recalculating
     # the same answer with the same technique.
     # Sometimes, that means manually creating the answers,
     # and include it in the test vector,
     # but other times, you can have simpler, but slower ways,
     # of getting the same results.
     # Or it's easier to check the result than to create it.
     for collection, target in in_order:
         index = find(collection, target)
         if (index >= 0):
             # For example, with finding,
             # you can check if the element is there.
             # For testing, you'll use "assert*"
             # methods of the class a lot.
             # They check if a condition is met.
             # For most variations, like here,
             # they check if a boolean or relationship
             # is True.
             self.assertEqual(target, collection[index])
         else:
             # Or if it really can't be found
             self.assertNotIn(target, collection)
Exemplo n.º 6
0
 def test_bad_order(self):
     # You can also test if your code reports errors correctly,
     # by using assertRaises.
     out_of_order = [([1, 3, 2, 4], 3), ([19, -4, 3, 6], 2)]
     for collection, target in out_of_order:
         with self.assertRaises(UnorderedException):
             index = find(collection, target)
Exemplo n.º 7
0
 def test_value_larger_than_arrays_maximum(self):
     with self.assertRaisesWithMessage(ValueError):
         find([1, 3, 4, 6, 8, 9, 11], 13)
Exemplo n.º 8
0
 def test_nothing_is_found_when_the_left_and_right_bounds_cross(self):
     with self.assertRaisesWithMessage(ValueError):
         find([1, 2], 0)
Exemplo n.º 9
0
 def test_a_value_larger_than_the_array_s_largest_value_is_not_found(self):
     with self.assertRaisesWithMessage(ValueError):
         find([1, 3, 4, 6, 8, 9, 11], 13)
Exemplo n.º 10
0
 def test_1(self):
     self.assertEqual(binary_search.find([0, 1, 21, 33, 45, 45, 61, 71, 72, 73], 33), 3)
Exemplo n.º 11
0
 def test_finds_value_in_array_with_one_element(self):
     self.assertEqual(find([6], 6), 0)
Exemplo n.º 12
0
 def test_empty_array(self):
     with self.assertRaisesWithMessage(ValueError):
         find([], 1)
Exemplo n.º 13
0
 def test_value_smaller_than_arrays_minimum(self):
     with self.assertRaisesWithMessage(ValueError):
         find([1, 3, 4, 6, 8, 9, 11], 0)
Exemplo n.º 14
0
def test_binary_search_returns_first_occurence_of_item_from_longer_list():
    assert_that(
        find('m', [
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l', 'm', 'm',
            'o', 'p'
        ]), equal_to(11))
Exemplo n.º 15
0
def test_binary_search_returns_first_occurence_of_item():
    assert_that(find('b', ['a', 'b', 'b']), equal_to(1))
Exemplo n.º 16
0
def test_binary_search_returns_negative_if_item_not_present_in_longer_list():
    assert_that(find('e', ['a', 'b', 'c', 'd']), equal_to(-1))
Exemplo n.º 17
0
def test_binary_search_returns_negative_if_item_not_present():
    assert_that(find('a', ['b']), equal_to(-1))
Exemplo n.º 18
0
 def test_nothing_found_when_value_is_outside_the_range_given_by_array(self):
     ary =  [1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634]
     self.assertEqual(find(ary, 1000, throw=False), 0)
     self.assertEqual(find(ary, -10, throw=False), 0)
Exemplo n.º 19
0
 def test_a_value_smaller_than_the_array_s_smallest_value_is_not_found_no_execp(self):
     self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 0, throw=False), 0)
Exemplo n.º 20
0
 def test_identifies_that_a_value_is_not_included_in_the_array_no_execp(self):
     self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 7, throw=False), 0)
Exemplo n.º 21
0
 def test_finds_value_in_array_of_even_length(self):
     self.assertEqual(
         find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21),
         5)
Exemplo n.º 22
0
 def test_identifies_value_missing(self):
     with self.assertRaisesWithMessage(ValueError):
         find([1, 3, 4, 6, 8, 9, 11], 7)
Exemplo n.º 23
0
 def test_finds_a_value_in_the_middle_of_an_array(self):
     self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 6), 3)
Exemplo n.º 24
0
 def test_value_larger_than_arrays_maximum(self):
     with self.assertRaisesWithMessage(ValueError):
         find([1, 3, 4, 6, 8, 9, 11], 13)
Exemplo n.º 25
0
 def test_finds_a_value_at_the_end_of_an_array(self):
     self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 11), 6)
Exemplo n.º 26
0
 def test_nothing_is_found_when_left_and_right_bounds_cross(self):
     with self.assertRaisesWithMessage(ValueError):
         find([1, 2], 0)
Exemplo n.º 27
0
 def test_finds_a_value_in_an_array_of_even_length(self):
     self.assertEqual(
         find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21), 5)
Exemplo n.º 28
0
def test_binary_search_finds_items_d():
    assert_that(find('d', ['a', 'b', 'c', 'd']), equal_to(3))
Exemplo n.º 29
0
 def test_empty_array(self):
     with self.assertRaisesWithMessage(ValueError):
         find([], 1)
Exemplo n.º 30
0
from read_csv import read_csv
from operator import attrgetter
from binary_search import find

# setting variable to chess player's attribute
by_lname = attrgetter('lname')

arr = read_csv('chess-players.csv')
arr.sort(key=by_lname)

# Invoke find method from binary_search.py
result = find(arr, value='Zhao', key=by_lname)

if (result):
    print("Player found in the list")
    print(
        f'First Name: {result.fname}, Last Name: {result.lname}, Country: {result.country}, Born: {result.born}, '
        f'Died: {result.died}')

else:
    print("Not found Player")
Exemplo n.º 31
0
def test_binary_search_returns_negative_on_empty_collection():
    assert_that(find('a', []), equal_to(-1))
Exemplo n.º 32
0
 def test_finds_a_value_at_the_beginning_of_an_array(self):
     self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 1), 0)
Exemplo n.º 33
0
from read_csv import read_csv
from chess_player import Chessplayer
from operator import attrgetter
from binary_search import find

# setting variable to chess player's attribute
by_lname = attrgetter('lname')

arr = read_csv('chess-players.csv')
arr.sort(key=by_lname)

result = find(arr, value='Šulskis', key=by_lname)

if (result):
        print("Player found in the list")
        print(f'{result}')
    
else: 
    print("Not found Player") 
Exemplo n.º 34
0
 def test_finds_a_value_in_an_array_of_odd_length(self):
     self.assertEqual(
         find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), 9)
Exemplo n.º 35
0
 def test_finds_value_in_middle_of_array(self):
     self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 6), 3)
Exemplo n.º 36
0
 def test_identifies_that_a_value_is_not_included_in_the_array(self):
     with self.assertRaisesWithMessage(ValueError):
         find([1, 3, 4, 6, 8, 9, 11], 7)
Exemplo n.º 37
0
 def test_finds_value_at_beginning_of_array(self):
     self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 1), 0)
Exemplo n.º 38
0
 def test_nothing_is_found_in_an_empty_array(self):
     with self.assertRaisesWithMessage(ValueError):
         find([], 1)
Exemplo n.º 39
0
 def test_finds_value_at_end_of_array(self):
     self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 11), 6)
Exemplo n.º 40
0
 def test_finds_a_value_in_an_array_with_one_element(self):
     self.assertEqual(find([6], 6), 0)
Exemplo n.º 41
0
 def test_finds_value_in_array_of_odd_length(self):
     self.assertEqual(
         find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), 9)