def rotated_array_search(input_list, number):
    """
    Find the index by searching in a rotated sorted array

    Args:
       input_list(array), number(int): Input array to search and the target
    Returns:
       int: Index or -1
    """
    if not input_list:
        return -1

    pivot_idx = find_pivot(input_list, 0, len(input_list) - 1)  # O(logn)
    if pivot_idx == len(input_list) - 1:
        return binary_search(input_list, 0,
                             len(input_list) - 1, number)  # O(logn)
    elif input_list[0] <= number <= input_list[pivot_idx]:
        input_list = input_list[:pivot_idx + 1]
        return binary_search(input_list, 0,
                             len(input_list) - 1, number)  # O(logn)
    elif number > input_list[-1]:
        return -1
    else:
        input_list = input_list[pivot_idx + 1:]
        return pivot_idx + 1 + binary_search(input_list, 0,
                                             len(input_list) - 1,
                                             number)  # O(logn)
Beispiel #2
0
    def test_binary_search(self):
        item_in_list = 738
        item_not_in_list = -30

        test = algorithms.binary_search(self.big_sorted, item_in_list)
        self.assertTrue(test)

        test = algorithms.binary_search(self.big_sorted, item_not_in_list)
        self.assertFalse(test)
Beispiel #3
0
    def test_binary_search(self):
        item_in_list = 738
        item_not_in_list = -30

        test = algorithms.binary_search(self.big_sorted, item_in_list)
        self.assertTrue(test)

        test = algorithms.binary_search(self.big_sorted, item_not_in_list)
        self.assertFalse(test)
Beispiel #4
0
def rotated_array_search(input_list, number):

    if not input_list:
        return -1

    pivot_idx = find_pivot(input_list, 0, len(input_list) - 1)
    if pivot_idx == len(input_list) - 1:
        return binary_search(input_list, 0, len(input_list) - 1, number)
    elif input_list[0] <= number <= input_list[pivot_idx]:
        input_list = input_list[:pivot_idx + 1]
        return binary_search(input_list, 0, len(input_list) - 1, number)
    elif number > input_list[-1]:
        return -1
    else:
        input_list = input_list[pivot_idx + 1:]
        return pivot_idx + 1 + binary_search(input_list, 0,
                                             len(input_list) - 1, number)
Beispiel #5
0
def main(input_file):
    # Load input as events in sorted order (Event timestamp)
    events = []
    with open(input_file) as file:
        for line in file:
            # TODO: Binary insert based off datetime instead of append
            e = Event(line)
            if (len(events) > 0):
                binary_insert(events, e)
            else:
                events.append(e)

    # Setup guards with shifts, and those shifts with events, in sorted order (Guard ID)
    guards = []
    current_guard_id = 0
    current_shift = Shift()
    for e in events:
        if e.type == 0:
            tmp_guard = Guard(e.GetGuardID())
            if len(guards) > 0:
                guards[current_guard_id].add_shift(current_shift)
                current_shift = Shift()
                
                index = binary_search(guards, tmp_guard)
                if index == -1:
                    current_guard_id = binary_insert(guards, tmp_guard)
                else:
                    current_guard_id = index
            else:
                guards.append(tmp_guard)
        current_shift.add_event(e)
    
    # Find guard who slept most
    result_index = index_of_max([sum(g.minutes_asleep) for g in guards])

    # Most slept minute
    msm = guards[result_index].get_most_slept_minute()

    # Part 1 Answer
    print("Guard #", guards[result_index].id, sep='')
    print("Slept most at minute", msm)
    print("Part 1 Answer:", guards[result_index].id * msm)

    print()

    # Part 2 Answer
    msm_count = [g.minutes_asleep[g.get_most_slept_minute()] for g in guards]
    result_index = index_of_max(msm_count)
    msm = guards[result_index].get_most_slept_minute()
    
    print("Guard #", guards[result_index].id, sep='')
    print("Slept most at minute ", msm, ', ', msm_count[result_index], ' times', sep='')
    print("Part 2 Answer:", guards[result_index].id * msm)
def test_target_absent_array_identical(identical_array):
    assert not binary_search(identical_array, 2)
def test_target_present_array_identical(identical_array):
    assert binary_search(identical_array, 1)
def test_target_absent_array_size_two():
    assert not binary_search([1, 2], 3)
def test_target_present_array_size_two():
    assert binary_search([1, 2], 2)
def test_target_absent_array_size_one():
    assert not binary_search([1], 2)
Beispiel #11
0
 def searchBookUsingBinarySearch(self, prefix: str):
     s = SortableBook.SortableBook(0, prefix, "", 0, None)
     j = algorithms.binary_search(self.bookSortedCatalog,
                                  self.bookSortedCatalog.size(), s)
     print(self.bookSortedCatalog[j])
def test_empty_array():
    assert not binary_search([], 5)
def test_target_is_max(ordered_array):
    assert binary_search(ordered_array, 25)
def test_target_absent_but_in_range(ordered_array):
    assert not binary_search(ordered_array, 6)
def test_target_too_big(ordered_array):
    assert not binary_search(ordered_array, 26)
def test_target_too_small(ordered_array):
    assert not binary_search(ordered_array, 0)
def test_target_present_array_size_one():
    assert binary_search([1], 1)
Beispiel #18
0
 def test_binary_search(self):
     self.assertEqual(algorithms.binary_search([4, 4, 5, 8], 8), 3)
     self.assertRaises(algorithms.UnsortedException,
                       algorithms.binary_search, [4, 1, 5, 8], 8)