diff --git a/.github/workflows/autopep.yml b/.github/workflows/autopep.yml new file mode 100644 index 0000000..6d1fec5 --- /dev/null +++ b/.github/workflows/autopep.yml @@ -0,0 +1,22 @@ +name: Autopep 8 + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.12"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.12" + - name: Install autopep8 + run: | + pip install autopep8 + - name: Run autopep8 + run: | + autopep8 --diff --exit-code --recursive . diff --git a/.github/workflows/hw-2-workflow.yml b/.github/workflows/hw-2-workflow.yml deleted file mode 100644 index fb9b458..0000000 --- a/.github/workflows/hw-2-workflow.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: HW 2 workflow -> unit tests - -on: [push] - -jobs: - build: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.12"] - - steps: - - uses: actions/checkout@v3 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Lint with Ruff - run: | - pip install ruff - ruff --format=github --target-version=py310 . - continue-on-error: true - - name: Test with pytest - run: | - coverage run -m pytest -v -s - - name: Generate Coverage Report - run: | - coverage report -m diff --git a/.github/workflows/pyflake.yml b/.github/workflows/pyflake.yml new file mode 100644 index 0000000..03b40b8 --- /dev/null +++ b/.github/workflows/pyflake.yml @@ -0,0 +1,19 @@ +name: Python PyFlakes + +on: [push, pull_request] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.x" + - name: Install dependencies + run: | + pip install pyflakes + - name: Run linters + run: | + pyflakes $(find . -name "*.py") > post_traces/pyflakes_report.txt diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml new file mode 100644 index 0000000..10e37c8 --- /dev/null +++ b/.github/workflows/pylint.yml @@ -0,0 +1,19 @@ +name: Python PyLint + +on: [push, pull_request] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.x" + - name: Install dependencies + run: | + pip install pylint + - name: Run linters + run: | + pylint $(find . -name "*.py") diff --git a/.github/workflows/unit-test-cases.yml b/.github/workflows/unit-test-cases.yml new file mode 100644 index 0000000..2d64777 --- /dev/null +++ b/.github/workflows/unit-test-cases.yml @@ -0,0 +1,26 @@ +name: Unit Test Cases + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.12"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.x" + - name: Install Dependencies and pytest + run: | + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + pip install pytest coverage + - name: Run Unit Tests + run: | + coverage run -m pytest + - name: Generate Coverage Report + run: | + coverage report -m diff --git a/README.md b/README.md index 1575cdb..45f3cdd 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ +Group 38 - SE Homwork 2 + [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) -![Language](https://img.shields.io/badge/Python-FFD43B?style=for-the-badge&logo=python&logoColor=blue) -![Platform](https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black) -![Unit tests](https://github.com/GradHackersGuild/se-homework/actions/workflows/git-commit-workflow.yml/badge.svg) \ No newline at end of file +[![Language](https://img.shields.io/badge/Python-FFD43B?style=for-the-badge&logo=python&logoColor=blue)](https://www.python.org/) +[![Platform](https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black)](https://www.linux.org/) +[![Unit tests](https://github.com/GradHackersGuild/se-homework-2/actions/workflows/unit-test-cases.yml/badge.svg)](https://github.com/GradHackersGuild/se-homework-2/actions/workflows/unit-test-cases.yml) +[![PEP8 Check](https://github.com/GradHackersGuild/se-homework-2/actions/workflows/autopep.yml/badge.svg)](https://github.com/GradHackersGuild/se-homework-2/actions/workflows/autopep.yml) +[![PyLint Check](https://github.com/GradHackersGuild/se-homework-2/actions/workflows/pylint.yml/badge.svg)](https://github.com/GradHackersGuild/se-homework-2/actions/workflows/pylint.yml) +[![PyFlake Check](https://github.com/GradHackersGuild/se-homework-2/actions/workflows/pyflake.yml/badge.svg)](https://github.com/GradHackersGuild/se-homework-2/actions/workflows/pyflake.yml) diff --git a/__pycache__/rand.cpython-312.pyc b/__pycache__/rand.cpython-312.pyc index 6e863d0..e69de29 100644 Binary files a/__pycache__/rand.cpython-312.pyc and b/__pycache__/rand.cpython-312.pyc differ diff --git a/__pycache__/rand.cpython-39.pyc b/__pycache__/rand.cpython-39.pyc deleted file mode 100644 index c3a6ec1..0000000 Binary files a/__pycache__/rand.cpython-39.pyc and /dev/null differ diff --git a/hw2_debugging.py b/hw2_debugging.py index 89ea0ed..eae3590 100644 --- a/hw2_debugging.py +++ b/hw2_debugging.py @@ -1,34 +1,51 @@ +""" +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 + half = len(arr) // 2 + return recombine(merge_sort(arr[:half]), merge_sort(arr[half:])) + + +def recombine(left_arr, right_arr): + """ + Merges two sorted arrays into a single sorted array. + + :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: - mergeArr[leftIndex + rightIndex] = rightArr[rightIndex] - rightIndex += 1 + merge_arr[left_index + right_index] = right_arr[right_index] + right_index += 1 - for i in range(rightIndex, len(rightArr)): - mergeArr[leftIndex + i] = rightArr[i] - - for i in range(leftIndex, len(leftArr)): - mergeArr[i + rightIndex] = leftArr[i] + 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 - return mergeArr -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/autopep8_output_test_merge_sort.txt b/post_traces/autopep8_output_test_merge_sort.txt new file mode 100644 index 0000000..e69de29 diff --git a/post_traces/pyflakes_output_hw2_debugging.txt b/post_traces/pyflakes_output_hw2_debugging.txt new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/post_traces/pyflakes_output_hw2_debugging.txt @@ -0,0 +1 @@ + diff --git a/post_traces/pyflakes_output_rand.txt b/post_traces/pyflakes_output_rand.txt new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/post_traces/pyflakes_output_rand.txt @@ -0,0 +1 @@ + diff --git a/post_traces/pyflakes_output_test_merge_sort.txt b/post_traces/pyflakes_output_test_merge_sort.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..d7495ee --- /dev/null +++ b/post_traces/pylint_output_rand.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_test_merge_sort.txt b/post_traces/pylint_output_test_merge_sort.txt new file mode 100644 index 0000000..3837ed3 --- /dev/null +++ b/post_traces/pylint_output_test_merge_sort.txt @@ -0,0 +1,4 @@ + +------------------------------------------------------------------- +Your code has been rated at 10.00/10 (previous run: 8.18/10, +1.82) + diff --git a/rand.py b/rand.py index 207bb9e..d67224c 100644 --- a/rand.py +++ b/rand.py @@ -1,7 +1,19 @@ +""" +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) + +def random_array(arr) -> list[int]: + """ + 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 diff --git a/test_mergesort.py b/test_mergesort.py index f18b85f..331fd3d 100644 --- a/test_mergesort.py +++ b/test_mergesort.py @@ -1,13 +1,28 @@ +""" +Module implementing the unit test cases for Merge Sort algorithm. +""" import hw2_debugging as OP + def test_mergesort1(): + """ + Takes array of random integers and checks whether it is sorted or not + """ arr = [10, 12, 2, 4, 1, 14, 10, 15, 16, 7, 9, 10] - assert OP.mergeSort(arr) == [1, 2, 4, 7, 9, 10, 10, 10, 12, 14, 15, 16] + assert OP.merge_sort(arr) == [1, 2, 4, 7, 9, 10, 10, 10, 12, 14, 15, 16] + def test_mergesort2(): + """ + Takes array of random integers and checks whether it is sorted or not + """ arr = [8, 12, 2, 1, 4, 10, 14, 15, 20, 7, 9, 4] - assert OP.mergeSort(arr) == [1, 2, 4, 4, 7, 8, 9, 10, 12, 14, 15, 20] + assert OP.merge_sort(arr) == [1, 2, 4, 4, 7, 8, 9, 10, 12, 14, 15, 20] + def test_mergesort3(): - arr = [8, 12, 2, 4, 1, 14, 10, 15, 16, 7, 9, 10] - assert OP.mergeSort(arr) == [1, 2, 4, 7, 9, 8, 10, 10, 12, 14, 15, 16, 17] \ No newline at end of file + """ + Takes array of random integers and checks whether it is sorted or not + """ + arr = [-10, -17, -5, -5, -3, 1, -1000, 10] + assert OP.merge_sort(arr) == [-1000, -17, -10, -5, -5, -3, 1, 10]