diff --git a/hw2_debugging.py b/hw2_debugging.py index 89ea0ed..55e2a3e 100644 --- a/hw2_debugging.py +++ b/hw2_debugging.py @@ -1,34 +1,48 @@ +""" +Module implementing the Merge Sort algorithm. + +This script contains functions to perform merge sort on a given list and merge two sorted arrays. +""" import rand -def mergeSort(arr): - if (len(arr) == 1): +def merge_sort(arr): + """ + Recursively divides and sorts an array using the merge sort algorithm. + + :param array: List of integers to be sorted. + :return: Sorted list of integers. + """ + if len(arr) == 1: return arr half = len(arr)//2 - return recombine(mergeSort(arr[:half]), mergeSort(arr[half:])) - -def recombine(leftArr, rightArr): - leftIndex = 0 - rightIndex = 0 - mergeArr = [None] * (len(leftArr) + len(rightArr)) - while leftIndex < len(leftArr) and rightIndex < len(rightArr): - if leftArr[leftIndex] < rightArr[rightIndex]: - mergeArr[leftIndex + rightIndex] = leftArr[leftIndex] - leftIndex += 1 - else: - mergeArr[leftIndex + rightIndex] = rightArr[rightIndex] - rightIndex += 1 + return recombine(merge_sort(arr[:half]), merge_sort(arr[half:])) - for i in range(rightIndex, len(rightArr)): - mergeArr[leftIndex + i] = rightArr[i] +def recombine(left_arr, right_arr): + """ + Merges two sorted arrays into a single sorted array. - for i in range(leftIndex, len(leftArr)): - mergeArr[i + rightIndex] = leftArr[i] + :param left_arr: Left half sorted array. + :param right_arr: Right half sorted array. + :return: Merged sorted array. + """ + left_index = 0 + right_index = 0 + merge_arr = [None] * (len(left_arr) + len(right_arr)) + while left_index < len(left_arr) and right_index < len(right_arr): + if left_arr[left_index] < right_arr[right_index]: + merge_arr[left_index + right_index] = left_arr[left_index] + left_index += 1 + else: + merge_arr[left_index + right_index] = right_arr[right_index] + right_index += 1 - return mergeArr + for i in range(right_index, len(right_arr)): + merge_arr[left_index + i] = right_arr[i] + for i in range(left_index, len(left_arr)): + merge_arr[i + right_index] = left_arr[i] + return merge_arr -arr = rand.random_array([int] * 20) -arr_out = mergeSort(arr) +array = rand.random_array([int] * 20) +arr_out = merge_sort(array) print(arr_out) - - diff --git a/post_traces/formatted_code.py b/post_traces/formatted_code.py new file mode 100644 index 0000000..b1fa36a --- /dev/null +++ b/post_traces/formatted_code.py @@ -0,0 +1,35 @@ +import rand + + +def merge_sort(arr): + if len(arr) == 1: + return arr + half = len(arr) // 2 + return recombine(merge_sort(arr[:half]), merge_sort(arr[half:])) + + +def recombine(left_arr, right_arr): + left_index = 0 + right_index = 0 + merge_arr = [None] * (len(left_arr) + len(right_arr)) + while left_index < len(left_arr) and right_index < len(right_arr): + if left_arr[left_index] < right_arr[right_index]: + merge_arr[left_index + right_index] = left_arr[left_index] + left_index += 1 + else: + merge_arr[left_index + right_index] = right_arr[right_index] + right_index += 1 + + for i in range(right_index, len(right_arr)): + merge_arr[left_index + i] = right_arr[i] + for i in range(left_index, len(left_arr)): + merge_arr[i + right_index] = left_arr[i] + return merge_arr + ldfg + + + +arr = rand.random_array([int] * 20) +arr_out = merge_sort(arr) + +print(arr_out) diff --git a/post_traces/pyflakes_output_hw2_debugging.txt b/post_traces/pyflakes_output_hw2_debugging.txt new file mode 100644 index 0000000..e69de29 diff --git a/post_traces/pyflakes_output_rand.txt b/post_traces/pyflakes_output_rand.txt new file mode 100644 index 0000000..e69de29 diff --git a/post_traces/pylint_output_hw2_debugging.txt b/post_traces/pylint_output_hw2_debugging.txt new file mode 100644 index 0000000..d7495ee --- /dev/null +++ b/post_traces/pylint_output_hw2_debugging.txt @@ -0,0 +1,4 @@ + +-------------------------------------------------------------------- +Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00) + diff --git a/post_traces/pylint_output_rand.txt b/post_traces/pylint_output_rand.txt new file mode 100644 index 0000000..7005971 --- /dev/null +++ b/post_traces/pylint_output_rand.txt @@ -0,0 +1,4 @@ + +------------------------------------------------------------------- +Your code has been rated at 10.00/10 (previous run: 6.67/10, +3.33) + diff --git a/rand.py b/rand.py index 207bb9e..d7f4c0c 100644 --- a/rand.py +++ b/rand.py @@ -1,7 +1,18 @@ +""" +Module for generating random arrays. + +This module contains a function to create a list of random integers. +""" + import random def random_array(arr)-> list[int]: - shuffled_num = None - for i in range(len(arr)): - arr[i] = random.randint(1,20) + """ + Fill the provided list with random integers between 1 and 20. + + :param arr: List of integers to be filled with random values. + :return: The list filled with random integers. + """ + for i, _ in enumerate(arr): + arr[i] = random.randint(1, 20) return arr