Ejemplo n.º 1
0
def _k_smallest(input_list, k):
    """
    O(n*logn) algorithm for finding the first k elemenst
    :param input_list:
    :return:
    """
    sorted_list = MergeSort.merge_sort(input_list)
    for i in range(k):
        print(sorted_list[i])
    return
def remove_duplicates(input_list):
    temp = []
    sorted_list = MergeSort.merge_sort(input_list)
    temp.append(input_list[0])
    for i in range(1, len(sorted_list)):
        if sorted_list[i] == sorted_list[i - 1]:
            continue
        else:
            temp.append(sorted_list[i])
    return temp
Ejemplo n.º 3
0
def delete_duplicates(input_list):
    sorted_list = MergeSort.merge_sort(input_list)
    i = 0
    while i < len(sorted_list) - 1:
        if sorted_list[i] == sorted_list[i + 1]:
            sorted_list[i], sorted_list[len(sorted_list) -
                                        1] = sorted_list[len(sorted_list) -
                                                         1], sorted_list[i]
            del sorted_list[len(sorted_list) - 1]
        i += 1
    return sorted_list
def check_duplicates_opt(input_list):
    """
    O(n*log(n)) algorithm - Sort the list

    :param input_list:
    :return:
    """
    sorted_list = MergeSort.merge_sort(input_list)
    for i in range(len(sorted_list)-1):
        if sorted_list[i] == sorted_list[i+1]:
            return True
    return False
Ejemplo n.º 5
0
def find_numbers(input_list, k):
    """
    O(n^2) algorithm
    :param input_list:
    :return:
    """
    sorted_list = MergeSort.merge_sort(input_list)
    a = b = 0
    pairs_list = []
    for i in range(len(input_list)):
        a, b = ConstantSum._find_numers_opt(input_list, k - input_list[i])
        pairs_list.append((a, b, input_list[i]))
    return pairs_list
Ejemplo n.º 6
0
def _find_number(input_list, k):

    sorted_list = MergeSort.merge_sort(input_list)
    left = 0
    right = len(input_list) - 1
    for i in range(len(sorted_list) - 2):
        left = i + 1
        right = len(input_list) - 1
        while left < right:
            if input_list[left] + input_list[right] + input_list[i] == k:
                return input_list[i], input_list[left], input_list[right]
            if input_list[left] + input_list[right] + input_list[i] > k:
                right -= 1
            elif input_list[left] + input_list[right] + input_list[i] < k:
                left += 1
def max_occurance_opt(input_list):
    """
    O(n*log(n)) algorithm
    :param input_list:
    :return:
    """
    sorted_list = MergeSort.merge_sort(input_list)
    max_idx = 0
    count = 1
    max_count = 0
    for i in range(len(sorted_list) - 1):
        if sorted_list[i] == sorted_list[i + 1]:
            count += 1
        else:
            count = 1
        if max_count < count:
            max_count = count
            max_idx = i
    return max_count, sorted_list[max_idx]
Ejemplo n.º 8
0
def _find_majority(input_list):
    """
    O(n*log(n)) algorithm
    :param input_list:
    :return:
    """
    sorted_list = MergeSort.merge_sort(input_list)
    max_count = 0
    count = 0
    maxcount_element_idx = 0
    for i in range(len(sorted_list) - 1):
        if input_list[i] == input_list[i + 1]:
            count += 1
        else:
            count = 1
        if max_count < count:
            max_count = count
            maxcount_element_idx = i
    if max_count > len(sorted_list) // 2:
        return sorted_list[maxcount_element_idx]
    else:
        return -1
Ejemplo n.º 9
0
def _find_numbers(input_list):
    """
    O(n*(log(n)) algorithm - Sort the input
    :param input_list:
    :return:
    """
    sorted_list = MergeSort.merge_sort(input_list)
    start_idx = 0
    end_idx = len(input_list) - 1
    min_sum = sys.maxsize
    min_left = 0
    min_right = len(input_list) - 1
    while start_idx < end_idx:
        current_sum = input_list[start_idx] + input_list[end_idx]
        if abs(current_sum) < abs(min_sum):
            min_sum = current_sum
            min_left = start_idx
            min_right = end_idx

        if current_sum < 0:
            start_idx += 1
        if current_sum > 0:
            end_idx -= 1
    return input_list[min_left], input_list[min_right]