def standardize_entry(entry, NUM_FIELDS=5):
    """Standarizes the multiple possible entry formats into unified format.

    Takes an list of user data and standardizes the format by removing
    removing characters, splitting text into a list and standarizign names 
    format.

    Args:
        entry: a list of user data.
        NUM_FIELDS: a constant specifying how many data fields an entry should
        have.

    Returns:
        A list of standarized user information. For example:

        ['Josh', 'Newman', 'blue', '08321', '1112223333']
    """

    SPECIAL_CHARS = '()-\n '
    #Removes chars from phone numbers and names
    entry = entry.translate(None, SPECIAL_CHARS)
    entry = entry.split(',')
    #Firstname, Lastname weren't comma seperated
    if (len(entry) < NUM_FIELDS):
        entry = standardize_names(entry)

    #Check if format is in (First, Last) order
    name_flag = in_first_last_name_order(entry)
    if (name_flag == False):
        helpers.swap(0, 1, entry)
    return entry
def standardize_entry(entry, NUM_FIELDS=5):
    """Standarizes the multiple possible entry formats into unified format.

    Takes an list of user data and standardizes the format by removing
    removing characters, splitting text into a list and standarizign names 
    format.

    Args:
        entry: a list of user data.
        NUM_FIELDS: a constant specifying how many data fields an entry should
        have.

    Returns:
        A list of standarized user information. For example:

        ['Josh', 'Newman', 'blue', '08321', '1112223333']
    """

    SPECIAL_CHARS = '()-\n '
    #Removes chars from phone numbers and names
    entry = entry.translate(None, SPECIAL_CHARS)
    entry = entry.split(',')
    #Firstname, Lastname weren't comma seperated
    if(len(entry) < NUM_FIELDS):
        entry = standardize_names(entry)

    #Check if format is in (First, Last) order
    name_flag = in_first_last_name_order(entry)
    if (name_flag == False):
      helpers.swap(0, 1, entry)
    return entry
Пример #3
0
def heapsort(A):
    build_max_heap(A)

    for i in range(0, len(A)):
        swap(A, 0, len(A)-i-1)
        max_heapify(A, 0, len(A)-i-1)
    return A
Пример #4
0
def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            swap(A, i, j)

    swap(A, r, i+1)
    return i+1
Пример #5
0
def insertion(A):
    # sorted_index = 1

    for i in range(1, len(A)):
        for j in range(i, -1, -1):
            if A[j] < A[j - 1]:
                swap(A, j - 1, j)
            else:
                break
    print(A)
    return A
Пример #6
0
def _partition(nums, lo, hi):
    # Get the pivot
    pivot = get_pivot(nums, lo, hi)
    low = lo
    high = hi
    while True:
        while nums[low] < pivot['value']:
            low += 1
        while nums[high] > pivot['value']:
            high -= 1
        if low >= high:
            return {'index': high, 'comparisons': 0, 'accesses': 0}
        helpers.swap(nums, low, high)
Пример #7
0
def max_heapify(A, i, leng):
    length = leng
    l = left(i)
    r = right(i)
    if l < length and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r < length and A[largest] < A[r]:
        largest = r
    if largest != i:
        swap(A, largest, i)
        max_heapify(A, largest, length)
Пример #8
0
def concave_sort (nums):
    nums_length = len(nums)
    # Append a new element if an odd number exists to avoid excluding an element from the comparisons
    odd = nums_length%2 != 0
    if odd:
        nums.append(0)
        nums_length+=1
    if config('VERBOSE'):
        cid = random.randint(0,9999)
        print(str(cid)+" | In "+str(nums))
    comps, access, half_size = 0, 0, int(math.floor(nums_length/2))
    # Loop through all possible offsets to ensure all elements are compared
    for offset in range(half_size):
        # Loop through half the elements to compare with the other half
        for i in range(half_size):
            # Get other_index
            o_i = nums_length-i-1-offset
            if o_i < half_size:
                o_i+=half_size
            # If the left value is greater than the right --> swap them
            if (nums[i] > nums[o_i]):
                helpers.swap(nums, i, o_i)
                access+=4
            comps+=1
            access+=2
    # Recursion
    if nums_length > 3:
        if config('VERBOSE'):
            print(str(cid)+" | Bal "+str(nums))
            print(str(cid)+" | Is balanced? "+str(helpers.is_balanced(nums)))
        left_sorted = concave_sort(nums[:half_size])
        right_sorted = concave_sort(nums[half_size:])
        sorted_list = left_sorted['sorted_list'] + right_sorted['sorted_list']
        comps += left_sorted['comparisons'] + right_sorted['comparisons']
        access  += left_sorted['accesses'] + right_sorted['accesses']
    else:
        sorted_list = nums
    if odd:
        sorted_list.remove(0)
    if config('VERBOSE'):
        print(str(cid)+" | Out "+str(sorted_list))
    return {"sorted_list": sorted_list, "comparisons": comps, "accesses": access}
Пример #9
0
 def test_swap(self):
     entry = ['first', 'last', 'red', '08743', '9999999999']
     swapped_entry = ['last', 'first', 'red', '08743', '9999999999']
     helpers.swap(0, 1, entry)
     self.assertEqual(entry, swapped_entry)
Пример #10
0
 def test_swap(self):
     entry = ['first', 'last', 'red', '08743', '9999999999']
     swapped_entry = ['last', 'first', 'red', '08743','9999999999']
     helpers.swap(0, 1, entry)
     self.assertEqual(entry, swapped_entry)
Пример #11
0
def selection_with_crappy_list_error(X):
    for i in range(len(X)):
        minimum = X[i:].index(min(X[i:]))
        swap(X, minimum, i)
    return X
Пример #12
0
def selection(X):
    for i in range(len(X)):
        minimum = X[i:].index(min(X[i:])) + i
        swap(X, minimum, i)
    return X