-
Couldn't load subscription status.
- Fork 4
Create sorting script #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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() |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,121 @@ | ||||||
| def bubble_sort(arr): | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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,
Suggested change
|
||||||
| """ | ||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of |
||||||
|
|
||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code has two issues that should be addressed:
Here is a suggested refactoring that addresses both points by organizing the tests and placing them inside a 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)There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code has two issues that should be addressed:
Here is a suggested refactoring that addresses both points by organizing the tests and placing them inside a 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)There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code has two issues that should be addressed:
Here is a suggested refactoring that addresses both points by organizing the tests and placing them inside a 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)There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code has two issues that should be addressed:
Here is a suggested refactoring that addresses both points by organizing the tests and placing them inside a 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) |
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_sorttakes a list and modifies it in-place, so its return type isNone. You can use the built-inlisttype for basic hinting. For more specific types (e.g., a list of numbers), you might usefrom typing import List, Unionand annotate asList[Union[int, float]].