Skip to content
Merged
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ A repo for storing Python code and tracking learning progress. All programs are
.
├── README.md
├── algorithms
│ ├── binary_search.py # interactive
| ├── bubble_sort.py # visual demo
│ └── quicksort.py
│ ├── binary_search.py
| ├── bubble_sort.py
| ├── quicksort.py
│ └── selection_sort.py
├── asynchronous_programming
│ ├── dependency_scheduler.py
│ ├── taskgroup_error_handling.py
│ └── worker_pipeline.py
└── data_structures
├── doubly_linked_list.py # interactive
└── singly_linked_list.py # interactive
├── doubly_linked_list.py
└── singly_linked_list.py
```

### GitHub Actions
Expand Down
27 changes: 7 additions & 20 deletions algorithms/binary_search.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
"""An interactive binary search implementation."""


def binary_search(numbers: list[int], target: int) -> int | None:
"""Return the index of target in a sorted list, or None if not found."""
left = 0
right = len(numbers) - 1
while left <= right:
Expand All @@ -16,25 +12,16 @@ def binary_search(numbers: list[int], target: int) -> int | None:
return None


def get_integer_input(prompt: str) -> int:
while True:
try:
return int(input(prompt))
except ValueError:
print("Please enter a valid integer.")


def main() -> None:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("Sorted list:", *numbers)

target = get_integer_input("Enter item to find: ")
index_found = binary_search(numbers, target)

if index_found is not None:
print(f"Item was found at index {index_found}.")
target = 7
print("Sorted list: ", numbers)
print("Target item: ", target)
found = binary_search(numbers, target)
if found is not None:
print(f"- Item {target} was found at index {found}.")
else:
print("Item was not found in the list.")
print(f"- Item {target} was not found.")


if __name__ == "__main__":
Expand Down
28 changes: 3 additions & 25 deletions algorithms/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
"""A bubble sort implementation with an animated demo."""


import os
import time


def clear_screen() -> None:
os.system("cls" if os.name == "nt" else "clear")


def show_step(numbers: list[int], index: int, should_swap: bool) -> None:
clear_screen()
spacing = " " * (3 * index)
print(numbers)
print(spacing + " ^ ^")
print(spacing + " swap" if should_swap else "")
time.sleep(0.5)


def bubble_sort(numbers: list[int]) -> None:
swapped = True
while swapped:
Expand All @@ -27,15 +7,13 @@ def bubble_sort(numbers: list[int]) -> None:
if should_swap:
numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]
swapped = True
show_step(numbers, i, should_swap)
clear_screen()
print(numbers)
print("List sorted!\n")
return numbers


def main() -> None:
numbers = [4, 2, 7, 1, 9, 6, 5, 8, 3]
bubble_sort(numbers)
print("Unsorted list:", numbers)
print("Sorted list: ", bubble_sort(numbers))


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions algorithms/quicksort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def _partition(numbers: list[int], low: int, high: int) -> int:
pivot = numbers[high]
i = low
for j in range(low, high):
if numbers[j] <= pivot:
if numbers[j] < pivot:
numbers[i], numbers[j] = numbers[j], numbers[i]
i += 1
numbers[i], numbers[high] = numbers[high], numbers[i]
Expand All @@ -22,8 +22,8 @@ def _partition(numbers: list[int], low: int, high: int) -> int:

def main() -> None:
numbers = [4, 2, 7, 1, 9, 6, 5, 8, 3]
print("Unsorted:", numbers)
print("Sorted: ", quicksort(numbers))
print("Unsorted list:", numbers)
print("Sorted list: ", quicksort(numbers))


if __name__ == "__main__":
Expand Down
18 changes: 18 additions & 0 deletions algorithms/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def selection_sort(numbers: list[int]) -> list[int]:
for i in range(len(numbers) - 1):
min_index = i
for j in range(i + 1, len(numbers)):
if numbers[j] < numbers[min_index]:
min_index = j
numbers[i], numbers[min_index] = numbers[min_index], numbers[i]
return numbers


def main() -> None:
numbers = [4, 2, 7, 1, 9, 6, 5, 8, 3]
print("Unsorted list:", numbers)
print("Sorted list: ", selection_sort(numbers))


if __name__ == "__main__":
main()
Loading