From 4cd7bb7d18c2ed914cad821e2ece3e3ef99d7fcd Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 26 Mar 2025 01:27:35 +0000 Subject: [PATCH] reorganise directories upd chapter_01/binary.py --- README.md | 20 ++++---- chapter1/binary.py | 43 ----------------- {chapter1 => chapter_01}/README.md | 0 chapter_01/binary.py | 44 ++++++++++++++++++ {chapter2 => chapter_02}/README.md | 0 {chapter2 => chapter_02}/selectionsort.py | 0 {chapter3 => chapter_03}/README.md | 0 {chapter3 => chapter_03}/recursion.py | 0 {chapter4 => chapter_04}/README.md | 0 {chapter4 => chapter_04}/ex4.1.py | 0 {chapter4 => chapter_04}/ex4.3.py | 0 {chapter4 => chapter_04}/ex4.4.py | 0 {chapter4 => chapter_04}/quicksort.py | 0 {chapter6 => chapter_06}/README.md | 0 {chapter6 => chapter_06}/bfs1.png | Bin {chapter6 => chapter_06}/bfs2.png | Bin {chapter6 => chapter_06}/ex6.1.py | 0 {chapter6 => chapter_06}/ex6.2.py | 0 {chapter6 => chapter_06}/mango.py | 0 {chapter7 => chapter_07}/README.md | 0 {chapter7 => chapter_07}/bfs_dirtrav.py | 0 {chapter7 => chapter_07}/dfs_dirtrav.py | 0 {chapter9 => chapter_09}/README.md | 0 {chapter9 => chapter_09}/dijkstra1.png | Bin {chapter9 => chapter_09}/dijkstra2.png | Bin {chapter9 => chapter_09}/dijkstra3.png | Bin {chapter9 => chapter_09}/rama.py | 0 {chapter9 => chapter_09}/with_heap/ex171a.py | 0 {chapter9 => chapter_09}/with_heap/ex171b.py | 0 {chapter9 => chapter_09}/with_heap/ex171c.py | 0 .../with_list_function/ex171a.py | 0 .../with_list_function/ex171b.py | 0 .../with_list_function/ex171c.py | 0 {chapter10 => chapter_10}/README.md | 0 {chapter10 => chapter_10}/greedy.py | 0 {chapter11 => chapter_11}/README.md | 0 {chapter11 => chapter_11}/camping.py | 0 {chapter11 => chapter_11}/knapsack.py | 0 {chapter11 => chapter_11}/subsequence.py | 0 {chapter11 => chapter_11}/substring.py | 0 {chapter11 => chapter_11}/travel.py | 0 {chapter12 => chapter_12}/README.md | 0 {chapter12 => chapter_12}/loaves.py | 0 43 files changed, 54 insertions(+), 53 deletions(-) delete mode 100644 chapter1/binary.py rename {chapter1 => chapter_01}/README.md (100%) create mode 100644 chapter_01/binary.py rename {chapter2 => chapter_02}/README.md (100%) rename {chapter2 => chapter_02}/selectionsort.py (100%) rename {chapter3 => chapter_03}/README.md (100%) rename {chapter3 => chapter_03}/recursion.py (100%) rename {chapter4 => chapter_04}/README.md (100%) rename {chapter4 => chapter_04}/ex4.1.py (100%) rename {chapter4 => chapter_04}/ex4.3.py (100%) rename {chapter4 => chapter_04}/ex4.4.py (100%) rename {chapter4 => chapter_04}/quicksort.py (100%) rename {chapter6 => chapter_06}/README.md (100%) rename {chapter6 => chapter_06}/bfs1.png (100%) rename {chapter6 => chapter_06}/bfs2.png (100%) rename {chapter6 => chapter_06}/ex6.1.py (100%) rename {chapter6 => chapter_06}/ex6.2.py (100%) rename {chapter6 => chapter_06}/mango.py (100%) rename {chapter7 => chapter_07}/README.md (100%) rename {chapter7 => chapter_07}/bfs_dirtrav.py (100%) rename {chapter7 => chapter_07}/dfs_dirtrav.py (100%) rename {chapter9 => chapter_09}/README.md (100%) rename {chapter9 => chapter_09}/dijkstra1.png (100%) rename {chapter9 => chapter_09}/dijkstra2.png (100%) rename {chapter9 => chapter_09}/dijkstra3.png (100%) rename {chapter9 => chapter_09}/rama.py (100%) rename {chapter9 => chapter_09}/with_heap/ex171a.py (100%) rename {chapter9 => chapter_09}/with_heap/ex171b.py (100%) rename {chapter9 => chapter_09}/with_heap/ex171c.py (100%) rename {chapter9 => chapter_09}/with_list_function/ex171a.py (100%) rename {chapter9 => chapter_09}/with_list_function/ex171b.py (100%) rename {chapter9 => chapter_09}/with_list_function/ex171c.py (100%) rename {chapter10 => chapter_10}/README.md (100%) rename {chapter10 => chapter_10}/greedy.py (100%) rename {chapter11 => chapter_11}/README.md (100%) rename {chapter11 => chapter_11}/camping.py (100%) rename {chapter11 => chapter_11}/knapsack.py (100%) rename {chapter11 => chapter_11}/subsequence.py (100%) rename {chapter11 => chapter_11}/substring.py (100%) rename {chapter11 => chapter_11}/travel.py (100%) rename {chapter12 => chapter_12}/README.md (100%) rename {chapter12 => chapter_12}/loaves.py (100%) diff --git a/README.md b/README.md index 0406944..8d4da2f 100644 --- a/README.md +++ b/README.md @@ -40,13 +40,13 @@ [chapter12][knn] -[binary]: ./chapter1/ -[selection_sort]: ./chapter2/ -[recursion]: ./chapter3/ -[quick_sort]: ./chapter4/ -[bfs]: ./chapter6/ -[trees]: ./chapter7/ -[dijkstra]: ./chapter9/ -[greedy]: ./chapter10/ -[dynamic]: ./chapter11/ -[knn]: ./chapter12/ +[binary]: ./chapter_01/ +[selection_sort]: ./chapter_02/ +[recursion]: ./chapter_03/ +[quick_sort]: ./chapter_04/ +[bfs]: ./chapter_06/ +[trees]: ./chapter_07/ +[dijkstra]: ./chapter_09/ +[greedy]: ./chapter_10/ +[dynamic]: ./chapter_11/ +[knn]: ./chapter_12/ diff --git a/chapter1/binary.py b/chapter1/binary.py deleted file mode 100644 index 664a93e..0000000 --- a/chapter1/binary.py +++ /dev/null @@ -1,43 +0,0 @@ -import logging -import random - -logging.basicConfig(level=logging.DEBUG) -logger = logging.getLogger(__name__) - - -def binary_search(arr, item): - low = 0 - high = len(arr) - 1 - - while low <= high: - mid = (low + high) // 2 - guess = arr[mid] - if guess == item: - return mid - elif guess > item: - high = mid - 1 - else: - low = mid + 1 - - return None - - -LOWER = 1000 -UPPER = 1000000 -SAMPLE_SIZE = 1000 - - -numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE) -numbers.sort() - -seen = set() -count = 0 -result = None -while not result: - guess = random.randrange(LOWER, UPPER) - if guess not in seen: - count += 1 - seen.add(guess) - result = binary_search(numbers, guess) - -print(f"Found {guess} at index {result} after {count} attempts") diff --git a/chapter1/README.md b/chapter_01/README.md similarity index 100% rename from chapter1/README.md rename to chapter_01/README.md diff --git a/chapter_01/binary.py b/chapter_01/binary.py new file mode 100644 index 0000000..8e3e326 --- /dev/null +++ b/chapter_01/binary.py @@ -0,0 +1,44 @@ +import logging +import random + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + + +class BinarySearch: + def __init__(self, arr): + self._arr = arr + + def search(self, item): + low = 0 + high = len(self._arr) - 1 + + while low <= high: + mid = (low + high) // 2 + guess = self._arr[mid] + if guess == item: + return mid + elif guess > item: + high = mid - 1 + else: + low = mid + 1 + + return None + + +LOWER = 1000 +UPPER = 1000000 +SAMPLE_SIZE = 1000 + + +numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE) +numbers.extend([9000, 999999]) +numbers.sort() +print(numbers) + +search = BinarySearch(numbers) +print(search.search(42)) +print(search.search(9000)) +print(search.search(20000)) +print(search.search(60000)) +print(search.search(999999)) diff --git a/chapter2/README.md b/chapter_02/README.md similarity index 100% rename from chapter2/README.md rename to chapter_02/README.md diff --git a/chapter2/selectionsort.py b/chapter_02/selectionsort.py similarity index 100% rename from chapter2/selectionsort.py rename to chapter_02/selectionsort.py diff --git a/chapter3/README.md b/chapter_03/README.md similarity index 100% rename from chapter3/README.md rename to chapter_03/README.md diff --git a/chapter3/recursion.py b/chapter_03/recursion.py similarity index 100% rename from chapter3/recursion.py rename to chapter_03/recursion.py diff --git a/chapter4/README.md b/chapter_04/README.md similarity index 100% rename from chapter4/README.md rename to chapter_04/README.md diff --git a/chapter4/ex4.1.py b/chapter_04/ex4.1.py similarity index 100% rename from chapter4/ex4.1.py rename to chapter_04/ex4.1.py diff --git a/chapter4/ex4.3.py b/chapter_04/ex4.3.py similarity index 100% rename from chapter4/ex4.3.py rename to chapter_04/ex4.3.py diff --git a/chapter4/ex4.4.py b/chapter_04/ex4.4.py similarity index 100% rename from chapter4/ex4.4.py rename to chapter_04/ex4.4.py diff --git a/chapter4/quicksort.py b/chapter_04/quicksort.py similarity index 100% rename from chapter4/quicksort.py rename to chapter_04/quicksort.py diff --git a/chapter6/README.md b/chapter_06/README.md similarity index 100% rename from chapter6/README.md rename to chapter_06/README.md diff --git a/chapter6/bfs1.png b/chapter_06/bfs1.png similarity index 100% rename from chapter6/bfs1.png rename to chapter_06/bfs1.png diff --git a/chapter6/bfs2.png b/chapter_06/bfs2.png similarity index 100% rename from chapter6/bfs2.png rename to chapter_06/bfs2.png diff --git a/chapter6/ex6.1.py b/chapter_06/ex6.1.py similarity index 100% rename from chapter6/ex6.1.py rename to chapter_06/ex6.1.py diff --git a/chapter6/ex6.2.py b/chapter_06/ex6.2.py similarity index 100% rename from chapter6/ex6.2.py rename to chapter_06/ex6.2.py diff --git a/chapter6/mango.py b/chapter_06/mango.py similarity index 100% rename from chapter6/mango.py rename to chapter_06/mango.py diff --git a/chapter7/README.md b/chapter_07/README.md similarity index 100% rename from chapter7/README.md rename to chapter_07/README.md diff --git a/chapter7/bfs_dirtrav.py b/chapter_07/bfs_dirtrav.py similarity index 100% rename from chapter7/bfs_dirtrav.py rename to chapter_07/bfs_dirtrav.py diff --git a/chapter7/dfs_dirtrav.py b/chapter_07/dfs_dirtrav.py similarity index 100% rename from chapter7/dfs_dirtrav.py rename to chapter_07/dfs_dirtrav.py diff --git a/chapter9/README.md b/chapter_09/README.md similarity index 100% rename from chapter9/README.md rename to chapter_09/README.md diff --git a/chapter9/dijkstra1.png b/chapter_09/dijkstra1.png similarity index 100% rename from chapter9/dijkstra1.png rename to chapter_09/dijkstra1.png diff --git a/chapter9/dijkstra2.png b/chapter_09/dijkstra2.png similarity index 100% rename from chapter9/dijkstra2.png rename to chapter_09/dijkstra2.png diff --git a/chapter9/dijkstra3.png b/chapter_09/dijkstra3.png similarity index 100% rename from chapter9/dijkstra3.png rename to chapter_09/dijkstra3.png diff --git a/chapter9/rama.py b/chapter_09/rama.py similarity index 100% rename from chapter9/rama.py rename to chapter_09/rama.py diff --git a/chapter9/with_heap/ex171a.py b/chapter_09/with_heap/ex171a.py similarity index 100% rename from chapter9/with_heap/ex171a.py rename to chapter_09/with_heap/ex171a.py diff --git a/chapter9/with_heap/ex171b.py b/chapter_09/with_heap/ex171b.py similarity index 100% rename from chapter9/with_heap/ex171b.py rename to chapter_09/with_heap/ex171b.py diff --git a/chapter9/with_heap/ex171c.py b/chapter_09/with_heap/ex171c.py similarity index 100% rename from chapter9/with_heap/ex171c.py rename to chapter_09/with_heap/ex171c.py diff --git a/chapter9/with_list_function/ex171a.py b/chapter_09/with_list_function/ex171a.py similarity index 100% rename from chapter9/with_list_function/ex171a.py rename to chapter_09/with_list_function/ex171a.py diff --git a/chapter9/with_list_function/ex171b.py b/chapter_09/with_list_function/ex171b.py similarity index 100% rename from chapter9/with_list_function/ex171b.py rename to chapter_09/with_list_function/ex171b.py diff --git a/chapter9/with_list_function/ex171c.py b/chapter_09/with_list_function/ex171c.py similarity index 100% rename from chapter9/with_list_function/ex171c.py rename to chapter_09/with_list_function/ex171c.py diff --git a/chapter10/README.md b/chapter_10/README.md similarity index 100% rename from chapter10/README.md rename to chapter_10/README.md diff --git a/chapter10/greedy.py b/chapter_10/greedy.py similarity index 100% rename from chapter10/greedy.py rename to chapter_10/greedy.py diff --git a/chapter11/README.md b/chapter_11/README.md similarity index 100% rename from chapter11/README.md rename to chapter_11/README.md diff --git a/chapter11/camping.py b/chapter_11/camping.py similarity index 100% rename from chapter11/camping.py rename to chapter_11/camping.py diff --git a/chapter11/knapsack.py b/chapter_11/knapsack.py similarity index 100% rename from chapter11/knapsack.py rename to chapter_11/knapsack.py diff --git a/chapter11/subsequence.py b/chapter_11/subsequence.py similarity index 100% rename from chapter11/subsequence.py rename to chapter_11/subsequence.py diff --git a/chapter11/substring.py b/chapter_11/substring.py similarity index 100% rename from chapter11/substring.py rename to chapter_11/substring.py diff --git a/chapter11/travel.py b/chapter_11/travel.py similarity index 100% rename from chapter11/travel.py rename to chapter_11/travel.py diff --git a/chapter12/README.md b/chapter_12/README.md similarity index 100% rename from chapter12/README.md rename to chapter_12/README.md diff --git a/chapter12/loaves.py b/chapter_12/loaves.py similarity index 100% rename from chapter12/loaves.py rename to chapter_12/loaves.py