Compare commits

..

No commits in common. "1d22356ce52fc385f4fce906056017fd5e6e3257" and "133502e1adce578afce94600aa3225143580663f" have entirely different histories.

16 changed files with 60 additions and 59 deletions

2
.gitignore vendored
View File

@ -160,5 +160,3 @@ cython_debug/
#.idea/
words_alpha.txt
tests/

View File

@ -1,5 +0,0 @@
# Binary Search
Repeatedly split the array checking if value is greater or less than the mid point. Stop when the exact value is found.
It takes log N steps to reduce an array of size N to an array of size 1. Time complexity for this algorithm is `O(log N)`.

View File

@ -30,14 +30,11 @@ SAMPLE_SIZE = 1000
numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
numbers.sort()
seen = set()
count = 0
result = None
while not result:
while result is None:
guess = random.randrange(LOWER, UPPER)
if guess not in seen:
count += 1
seen.add(guess)
logger.debug(f"guess: {guess}")
result = binary_search(numbers, guess)
print(f"Found {guess} at index {result} after {count} attempts")
print(f"Found {guess} at index {result}.")

7
chapter1/ex1.1.py Normal file
View File

@ -0,0 +1,7 @@
import math
num_steps = int(math.log2(128))
print(
f"A binary search would take maximum {num_steps} steps "
"to search a list of 128 items."
)

7
chapter1/ex1.2.py Normal file
View File

@ -0,0 +1,7 @@
import math
num_steps = int(math.log2(128*2))
print(
f"A binary search would take maximum {num_steps} steps "
"to search a list of 256 items."
)

View File

@ -1,7 +0,0 @@
# Selection Sort
We have to perform N swaps a total of N times. This takes N^N steps, therefore:
This algorithm has time complexity `O(N^2)`
Technically (`n 1, n - 2 ... 2, 1` ~= N/2) swaps are performed but in BigO the constants are dropped.

View File

@ -1,10 +0,0 @@
# Recursion
Recursive functions must have both:
- one or more base cases
- a recursive case
The base cases are required to ensure the recursion stops when meeting a condition
The recursive case adds functions onto the call stack and completes each one top down.

View File

@ -1,9 +0,0 @@
# Quicksort
Similar to the previous recursive function, quicksort uses divide and conquer.
The base case occurs for an array size 0 or 1 (doesn't need to be sorted).
The recursive case works by partitioning the array around a chosen pivot repeatedly until the base case is met and then combining all sorted sub-arrays.
Note. Quicksort should be implemented using a random pivot to ensure average runtimes.

View File

@ -32,14 +32,10 @@ SAMPLE_SIZE = 1000
numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
numbers.sort()
seen = set()
count = 0
result = None
while result is None:
guess = random.randrange(LOWER, UPPER)
if guess not in seen:
count += 1
seen.add(guess)
logger.debug(f"guess: {guess}")
result = binary_search(numbers, 0, len(numbers) - 1, guess)
print(f"Found {guess} at index {result} after {count} attempts.")
print(f"Found {guess} at index {result}.")

View File

@ -1,9 +0,0 @@
# Breadth-First Search
Can tell you if there's a path between A and B and will find the shortest.
In these examples, 1st degree Mango sellers are found before 2nd degree, 2nd before 3rd and so on.
Visted nodes should be stored in a set to ensure no infinite loops.
Running time for BFS on a directed graph: `O(V + E`) where V = vertices, E = edges.

View File

@ -2,4 +2,4 @@
- Dijkstra's algorithm works when all weights are non-negative
- If there are negative weights use Bellman-Ford.
- The book demonstrates a function that operates on a list. Priority queue + min heap added for completeness.
- Priority queue + min heap is optimal when compared to a function that operates on a list.

View File

@ -0,0 +1,10 @@
import heapq
customers = []
heapq.heappush(customers, (2, "Harry"))
heapq.heappush(customers, (3, "Charles"))
heapq.heappush(customers, (1, "Riya"))
heapq.heappush(customers, (4, "Stacy"))
while customers:
print(heapq.heappop(customers))

14
chapter9/examples/list.py Normal file
View File

@ -0,0 +1,14 @@
customers = []
customers.append((2, "Harry")) # no sort needed here because 1 item.
customers.append((3, "Charles"))
customers.sort(reverse=True)
# Need to sort to maintain order
customers.append((1, "Riya"))
customers.sort(reverse=True)
# Need to sort to maintain order
customers.append((4, "Stacy"))
customers.sort(reverse=True)
while customers:
print(customers.pop(0))
# Will print names in the order: Stacy, Charles, Harry, Riya.

View File

@ -0,0 +1,12 @@
from queue import PriorityQueue
customers = (
PriorityQueue()
) # we initialise the PQ class instead of using a function to operate upon a list.
customers.put((2, "Harry"))
customers.put((3, "Charles"))
customers.put((1, "Riya"))
customers.put((4, "Stacy"))
while customers:
print(customers.get())