Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import os
import json
import sys

def main():
github_event_name = os.getenv("GITHUB_EVENT_NAME")
github_event_path = os.getenv("GITHUB_EVENT_PATH")
def load_github_event():
event_name = os.getenv("GITHUB_EVENT_NAME", "UNKNOWN_EVENT")
event_path = os.getenv("GITHUB_EVENT_PATH")

print(f"📦 Received GitHub event: {event_name}")

print(f"Received GitHub event: {github_event_name}")
if not event_path:
print("⚠️ Environment variable GITHUB_EVENT_PATH is not set. Cannot read event data.")
sys.exit(1)

if not github_event_path:
print("GITHUB_EVENT_PATH not set, cannot read event data.")
return
if not os.path.isfile(event_path):
print(f"❌ Event file not found at: {event_path}")
sys.exit(1)

try:
with open(github_event_path, "r") as file:
event_data = json.load(file)
print("Event JSON Payload:")
print(json.dumps(event_data, indent=2))
with open(event_path, "r", encoding="utf-8") as f:
return json.load(f)
except json.JSONDecodeError as e:
print(f"❌ Failed to parse event JSON: {e}")
except Exception as e:
print(f"Error reading event data: {e}")
print(f"❌ Unexpected error reading event file: {e}")

sys.exit(1)

def main():
event_data = load_github_event()
print("✅ Event JSON Payload:")
print(json.dumps(event_data, indent=2))

if __name__ == "__main__":
main()
main()
121 changes: 121 additions & 0 deletions sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
def bubble_sort(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve code clarity, maintainability, and enable static analysis, consider adding type hints to your function signatures, as recommended by PEP 484. This should be applied to all functions in this file.

For example, bubble_sort takes a list and modifies it in-place, so its return type is None. You can use the built-in list type for basic hinting. For more specific types (e.g., a list of numbers), you might use from typing import List, Union and annotate as List[Union[int, float]].

Suggested change
def bubble_sort(arr):
def bubble_sort(arr: list) -> None:

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve code clarity, maintainability, and enable static analysis, consider adding type hints to your function signatures, as recommended by PEP 484. This should be applied to all functions in this file.

For example, bubble_sort takes a list and modifies it in-place, so its return type is None. You can use the built-in list type for basic hinting. For more specific types (e.g., a list of numbers), you might use from typing import List, Union and annotate as List[Union[int, float]].

Suggested change
def bubble_sort(arr):
def bubble_sort(arr: list) -> None:

"""
Performs in-place Bubble Sort on a list.
Stops early if no swaps occur in a pass.
"""
n = len(arr)
for end in range(n - 1, 0, -1):
swapped = False
for i in range(end):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = True
if not swapped:
break


def selection_sort(arr):
"""
Performs in-place Selection Sort.
Repeatedly selects the minimum element and puts it in place.
"""
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]


def insertion_sort(arr):
"""
Performs in-place Insertion Sort.
Builds the sorted array one item at a time.
"""
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key


def merge_sort(arr):
"""
Performs Merge Sort (not in-place).
Recursively divides the list and merges sorted halves.
"""
if len(arr) <= 1:
return arr

mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
Comment on lines +54 to +55

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of merge_sort creates new sub-lists using slicing (arr[:mid] and arr[mid:]) in each recursive call. For large lists, this can be inefficient and consume a lot of memory because Python creates a copy of the data for each slice. A more memory-efficient approach for merge_sort is to pass indices (e.g., start and end) to the recursive calls. This allows them to work on views of the original list (or a single copy) instead of creating many small, temporary copies.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of merge_sort creates new sub-lists using slicing (arr[:mid] and arr[mid:]) in each recursive call. For large lists, this can be inefficient and consume a lot of memory because Python creates a copy of the data for each slice. A more memory-efficient approach for merge_sort is to pass indices (e.g., start and end) to the recursive calls. This allows them to work on views of the original list (or a single copy) instead of creating many small, temporary copies.


return merge(left, right)


def merge(left, right):
"""
Helper function to merge two sorted lists.
"""
result = []
i = j = 0

# Merge the two halves
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1

# Append any remaining elements
result.extend(left[i:])
result.extend(right[j:])
return result


# Sample input
original = [6, 6, 2, 3, 2]

print("Original list:")
print(original)

# Bubble Sort
arr1 = original.copy()
bubble_sort(arr1)
print("\nBubble Sort result:")
print(arr1)

# Selection Sort
arr2 = original.copy()
selection_sort(arr2)
print("\nSelection Sort result:")
print(arr2)

# Insertion Sort
arr3 = original.copy()
insertion_sort(arr3)
print("\nInsertion Sort result:")
print(arr3)

# Merge Sort (not in-place)
arr4 = original.copy()
arr4_sorted = merge_sort(arr4)
print("\nMerge Sort result:")
print(arr4_sorted)

# Python built-in sort (in-place)
arr5 = original.copy()
arr5.sort()
print("\nPython .sort() result:")
print(arr5)

# Python built-in sorted() (returns new list)
arr6 = sorted(original)
print("\nPython sorted() result (non-in-place):")
print(arr6)
Comment on lines +82 to +121

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This block of code has two issues that should be addressed:

  1. Critical: The demonstration code runs in the global scope. This is a significant issue because if another module imports this file, the demonstration code will execute unexpectedly. All script execution logic should be placed within an if __name__ == "__main__": block to ensure the module is reusable.
  2. Medium: The code for testing each sorting algorithm is highly repetitive. This violates the Don't Repeat Yourself (DRY) principle and makes the code harder to maintain or extend. Refactoring this into a loop would make it much cleaner.

Here is a suggested refactoring that addresses both points by organizing the tests and placing them inside a __main__ guard:

if __name__ == "__main__":
    # Sample input
    original = [6, 6, 2, 3, 2]

    print("Original list:")
    print(original)

    # Sorting functions to test: (name, function, is_in_place)
    sorting_algorithms = [
        ("Bubble Sort", bubble_sort, True),
        ("Selection Sort", selection_sort, True),
        ("Insertion Sort", insertion_sort, True),
        ("Merge Sort", merge_sort, False),
        ("Python .sort()", lambda arr: arr.sort(), True),
        ("Python sorted()", sorted, False),
    ]

    for name, sort_func, is_in_place in sorting_algorithms:
        arr_copy = original.copy()
        print(f"\n{name} result:")
        if is_in_place:
            sort_func(arr_copy)
            print(arr_copy)
        else:
            sorted_arr = sort_func(arr_copy)
            print(sorted_arr)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This block of code has two issues that should be addressed:

  1. Critical: The demonstration code runs in the global scope. This is a significant issue because if another module imports this file, the demonstration code will execute unexpectedly. All script execution logic should be placed within an if __name__ == "__main__": block to ensure the module is reusable.
  2. Medium: The code for testing each sorting algorithm is highly repetitive. This violates the Don't Repeat Yourself (DRY) principle and makes the code harder to maintain or extend. Refactoring this into a loop would make it much cleaner.

Here is a suggested refactoring that addresses both points by organizing the tests and placing them inside a __main__ guard:

if __name__ == "__main__":
    # Sample input
    original = [6, 6, 2, 3, 2]

    print("Original list:")
    print(original)

    # Sorting functions to test: (name, function, is_in_place)
    sorting_algorithms = [
        ("Bubble Sort", bubble_sort, True),
        ("Selection Sort", selection_sort, True),
        ("Insertion Sort", insertion_sort, True),
        ("Merge Sort", merge_sort, False),
        ("Python .sort()", lambda arr: arr.sort(), True),
        ("Python sorted()", sorted, False),
    ]

    for name, sort_func, is_in_place in sorting_algorithms:
        arr_copy = original.copy()
        print(f"\n{name} result:")
        if is_in_place:
            sort_func(arr_copy)
            print(arr_copy)
        else:
            sorted_arr = sort_func(arr_copy)
            print(sorted_arr)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This block of code has two issues that should be addressed:

  1. Critical: The demonstration code runs in the global scope. This is a significant issue because if another module imports this file, the demonstration code will execute unexpectedly. All script execution logic should be placed within an if __name__ == "__main__": block to ensure the module is reusable.
  2. Medium: The code for testing each sorting algorithm is highly repetitive. This violates the Don't Repeat Yourself (DRY) principle and makes the code harder to maintain or extend. Refactoring this into a loop would make it much cleaner.

Here is a suggested refactoring that addresses both points by organizing the tests and placing them inside a __main__ guard:

if __name__ == "__main__":
    # Sample input
    original = [6, 6, 2, 3, 2]

    print("Original list:")
    print(original)

    # Sorting functions to test: (name, function, is_in_place)
    sorting_algorithms = [
        ("Bubble Sort", bubble_sort, True),
        ("Selection Sort", selection_sort, True),
        ("Insertion Sort", insertion_sort, True),
        ("Merge Sort", merge_sort, False),
        ("Python .sort()", lambda arr: arr.sort(), True),
        ("Python sorted()", sorted, False),
    ]

    for name, sort_func, is_in_place in sorting_algorithms:
        arr_copy = original.copy()
        print(f"\n{name} result:")
        if is_in_place:
            sort_func(arr_copy)
            print(arr_copy)
        else:
            sorted_arr = sort_func(arr_copy)
            print(sorted_arr)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This block of code has two issues that should be addressed:

  1. Critical: The demonstration code runs in the global scope. This is a significant issue because if another module imports this file, the demonstration code will execute unexpectedly. All script execution logic should be placed within an if __name__ == "__main__": block to ensure the module is reusable.
  2. Medium: The code for testing each sorting algorithm is highly repetitive. This violates the Don't Repeat Yourself (DRY) principle and makes the code harder to maintain or extend. Refactoring this into a loop would make it much cleaner.

Here is a suggested refactoring that addresses both points by organizing the tests and placing them inside a __main__ guard:

if __name__ == "__main__":
    # Sample input
    original = [6, 6, 2, 3, 2]

    print("Original list:")
    print(original)

    # Sorting functions to test: (name, function, is_in_place)
    sorting_algorithms = [
        ("Bubble Sort", bubble_sort, True),
        ("Selection Sort", selection_sort, True),
        ("Insertion Sort", insertion_sort, True),
        ("Merge Sort", merge_sort, False),
        ("Python .sort()", lambda arr: arr.sort(), True),
        ("Python sorted()", sorted, False),
    ]

    for name, sort_func, is_in_place in sorting_algorithms:
        arr_copy = original.copy()
        print(f"\n{name} result:")
        if is_in_place:
            sort_func(arr_copy)
            print(arr_copy)
        else:
            sorted_arr = sort_func(arr_copy)
            print(sorted_arr)