Skip to content

Commit

Permalink
update(algorithms): Revised commenting of all in-sopce modules and th…
Browse files Browse the repository at this point in the history
…e readme directory structure. (#10)
  • Loading branch information
Cre4T3Tiv3 authored Nov 3, 2023
1 parent cbeded1 commit dbc6331
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 186 deletions.
69 changes: 29 additions & 40 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# ========================
# Golang .gitignore
# ========================

# Binaries for Programs and Plugins:
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test Binary, Built with `go test -c`:
*.test

# Output of the Go Coverage Tool, Specifically When Used with LiteIDE:
*.out

# Dependency Directories (Remove the Comment Below to Include It)
# vendor/

# Go Workspace File:
go.work

# The Local Development Env-Var JSON Config File:
/configs/config.json

# Local Directory Containing Log Files:
/logs/*

# ========================
# Python .gitignore
# ========================
Expand Down Expand Up @@ -124,46 +153,6 @@ dmypy.json
# Cython debug symbols
cython_debug/

# ========================
# Bash .gitignore
# ========================

# Editor backup/swap files
*~
.*~
*.swp
.*.swp

# Compiled Binaries or scripts (if any)
bin/
*.out

# ========================
# PowerShell .gitignore
# ========================

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# JetBrains IDEs (like PyCharm)
.idea/

# ========================
# MISC .gitignore
# ========================
Expand Down
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,37 @@ For example:
```
algorithms-and-data-structures-portfolio
|-- algorithms
| |-- dynamic-programming
| | |-- python
| | | |-- max_sum_divisible_by_k.py
| |-- hashing
| | |-- quadratic-probing
| | | |-- python
| | | | |-- hash_quadratic_probing.py
| | | | |-- README.md
| |-- searching
| | |-- binary-search
| | | |-- python
| | | |-- go
| |-- hashing
| | |-- quadratic-probing
| | | | |-- binary_search_test.go
| | | | |-- binary_search.go
| | | | |-- go.mod
| | | | |-- README.md
| | | |-- python
| | | | |-- tests
| | | | | |-- test_binary_search.py
| | | | |-- README.md
| | | | |-- binary_search.py
| |-- sorting
| | |-- adjusted-counting-sort
| | | |-- python
| | | | |-- adjusted_counting.py
| | | | |-- README.md
| | | | |-- tests
| | | | | |-- test_adjusted_counting.py
| | | | |-- adjusted_counting.py
| | | | |-- README.md
|-- .gitignore
|-- LICENSE
|-- README.md
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,107 @@
class HashTable:
def __init__(self, size=10):
"""Initialize the hash table with a given size."""
# The size of the hash table.
self.size = size
# The hash table.
self.table = [None] * self.size
# The number of key-value pairs stored in the hash table.
self.count = 0

def _hash(self, key):
"""Compute the initial hash value for a key."""
# The hash value is computed as follows:
return key % self.size

def _rehash(self, old_hash, i):
"""Compute the new hash value using quadratic probing."""
# The new hash value is computed as follows:
return (old_hash + i**2) % self.size

def put(self, key, value):
"""Store the key-value pair in the hash table."""
# Compute the initial hash value.
initial_hash = self._hash(key)
# Initialize the hash value.
hash_val = initial_hash
# Initialize the counter.
i = 1

# Find an available slot or a slot with the same key to update.
# Search for an empty slot or a slot with the same key.
while self.table[hash_val] is not None and self.table[hash_val][0] != key:
# If the key already exists, update the value.
hash_val = self._rehash(initial_hash, i)
# Increment the counter.
i += 1

# If the key does not exist, add the key-value pair.
if self.table[hash_val] is None:
# Increment the counter.
self.count += 1
# Add the key-value pair.
self.table[hash_val] = (key, value)

def get(self, key):
"""Retrieve the value associated with the given key."""
# Compute the initial hash value.
initial_hash = self._hash(key)
# Initialize the hash value.
hash_val = initial_hash
# Initialize the counter.
i = 1

# Search for the key in the hash table.
while self.table[hash_val] is not None:
# Return the value if the key is found.
if self.table[hash_val][0] == key:
# Return the value.
return self.table[hash_val][1]
# Otherwise, rehash and search again.
hash_val = self._rehash(initial_hash, i)
# Increment the counter.
i += 1

# Return None if the key is not found.
return None

def __setitem__(self, key, value):
"""Support assignment using bracket notation."""
# Add the key-value pair.
self.put(key, value)

def __getitem__(self, key):
"""Support retrieval using bracket notation."""
# Retrieve the value.
return self.get(key)

def __contains__(self, key):
"""Support the 'in' keyword."""
# Check if the key exists.
return self.get(key) is not None


def test_hash_table():
"""Function to test the HashTable functionality."""
# Initialize the hash table.
hash_table = HashTable()

# Sample data for testing.
# Add some key-value pairs.
keys = [1, 2, 12, 22]
# Note: The keys are chosen to demonstrate the quadratic probing.
values = ["One", "Two", "Twelve", "Twenty Two"]
# Add the key-value pairs.
for key, value in zip(keys, values):
# Add the key-value pair.
hash_table[key] = value

# Check if the key-value pairs were added correctly.
for key, expected in zip(keys, values):
# Retrieve the value.
result = hash_table[key]
# Check if the value is correct.
assert (
result == expected
), f"Expected {expected}, but got {result} for key {key}"


# Test the HashTable class.
if __name__ == "__main__":
try:
test_hash_table()
Expand Down
41 changes: 0 additions & 41 deletions algorithms/searching/binary-search/go/.gitignore

This file was deleted.

83 changes: 0 additions & 83 deletions algorithms/searching/binary-search/python/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ def test_binary_search():
result == expected
), f"Expected {expected}, but got {result} for array {arr} and target {target}"


# Test the binary_search function.
if __name__ == "__main__":
try:
# Run the tests.
test_binary_search()
# Print a success message if all tests pass.
print("All tests passed!")
except AssertionError as e:
# If any test fails, print a detailed error message.
Expand Down
Loading

0 comments on commit dbc6331

Please sign in to comment.