Skip to content
Open
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
54 changes: 54 additions & 0 deletions randomized_algorithms/miller_rabin_primality test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import random

Check failure on line 1 in randomized_algorithms/miller_rabin_primality test.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/miller_rabin_primality test.py:1:1: INP001 File `randomized_algorithms/miller_rabin_primality test.py` is part of an implicit namespace package. Add an `__init__.py`.

Check failure on line 1 in randomized_algorithms/miller_rabin_primality test.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/miller_rabin_primality test.py:1:1: INP001 File `randomized_algorithms/miller_rabin_primality test.py` is part of an implicit namespace package. Add an `__init__.py`.

Check failure on line 1 in randomized_algorithms/miller_rabin_primality test.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/miller_rabin_primality test.py:1:1: INP001 File `randomized_algorithms/miller_rabin_primality test.py` is part of an implicit namespace package. Add an `__init__.py`.


def miller_rabin_primality_test(n: int, k: int = 5) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Please provide descriptive name for the parameter: k

"""
Probabilistic primality test using Miller-Rabin algorithm.

Args:
n (int): Number to test for primality.
k (int): Number of iterations for accuracy.

Returns:
bool: True if n is probably prime, False if composite.

Examples:
>>> miller_rabin_primality_test(2)
True
>>> miller_rabin_primality_test(15)
False
>>> miller_rabin_primality_test(17)
True
"""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0:
return False

# Write n-1 as 2^r * d
d = n - 1
r = 0
while d % 2 == 0:
d //= 2
r += 1

for _ in range(k):
a = random.randint(2, n - 2)
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = pow(x, 2, n)
if x == n - 1:
break
else:
return False
return True


if __name__ == "__main__":
import doctest

doctest.testmod()
33 changes: 33 additions & 0 deletions randomized_algorithms/randomized_quicksort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import random

Check failure on line 1 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/randomized_quicksort.py:1:1: INP001 File `randomized_algorithms/randomized_quicksort.py` is part of an implicit namespace package. Add an `__init__.py`.

Check failure on line 1 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/randomized_quicksort.py:1:1: INP001 File `randomized_algorithms/randomized_quicksort.py` is part of an implicit namespace package. Add an `__init__.py`.

Check failure on line 1 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/randomized_quicksort.py:1:1: INP001 File `randomized_algorithms/randomized_quicksort.py` is part of an implicit namespace package. Add an `__init__.py`.
from typing import List

Check failure on line 2 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/randomized_quicksort.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 2 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/randomized_quicksort.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 2 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/randomized_quicksort.py:2:1: UP035 `typing.List` is deprecated, use `list` instead


def randomized_quicksort(arr: List[int]) -> List[int]:

Check failure on line 5 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

randomized_algorithms/randomized_quicksort.py:5:45: UP006 Use `list` instead of `List` for type annotation

Check failure on line 5 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

randomized_algorithms/randomized_quicksort.py:5:31: UP006 Use `list` instead of `List` for type annotation

Check failure on line 5 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

randomized_algorithms/randomized_quicksort.py:5:45: UP006 Use `list` instead of `List` for type annotation

Check failure on line 5 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

randomized_algorithms/randomized_quicksort.py:5:31: UP006 Use `list` instead of `List` for type annotation

Check failure on line 5 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

randomized_algorithms/randomized_quicksort.py:5:45: UP006 Use `list` instead of `List` for type annotation

Check failure on line 5 in randomized_algorithms/randomized_quicksort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

randomized_algorithms/randomized_quicksort.py:5:31: UP006 Use `list` instead of `List` for type annotation
"""
Sorts a list of integers using randomized QuickSort algorithm.

Args:
arr (List[int]): List of integers to sort.

Returns:
List[int]: Sorted list of integers.

Examples:
>>> randomized_quicksort([3, 6, 1, 8, 4])
[1, 3, 4, 6, 8]
>>> randomized_quicksort([])
[]
"""
if len(arr) <= 1:
return arr
pivot = random.choice(arr)
less = [x for x in arr if x < pivot]
equal = [x for x in arr if x == pivot]
greater = [x for x in arr if x > pivot]
return randomized_quicksort(less) + equal + randomized_quicksort(greater)


if __name__ == "__main__":
import doctest

doctest.testmod()
37 changes: 37 additions & 0 deletions randomized_algorithms/reservoir_sampling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import random

Check failure on line 1 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/reservoir_sampling.py:1:1: INP001 File `randomized_algorithms/reservoir_sampling.py` is part of an implicit namespace package. Add an `__init__.py`.

Check failure on line 1 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/reservoir_sampling.py:1:1: INP001 File `randomized_algorithms/reservoir_sampling.py` is part of an implicit namespace package. Add an `__init__.py`.

Check failure on line 1 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

randomized_algorithms/reservoir_sampling.py:1:1: INP001 File `randomized_algorithms/reservoir_sampling.py` is part of an implicit namespace package. Add an `__init__.py`.
from typing import List, Iterator, TypeVar

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/reservoir_sampling.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/reservoir_sampling.py:2:1: UP035 Import from `collections.abc` instead: `Iterator`

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

randomized_algorithms/reservoir_sampling.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/reservoir_sampling.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/reservoir_sampling.py:2:1: UP035 Import from `collections.abc` instead: `Iterator`

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

randomized_algorithms/reservoir_sampling.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/reservoir_sampling.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

randomized_algorithms/reservoir_sampling.py:2:1: UP035 Import from `collections.abc` instead: `Iterator`

Check failure on line 2 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

randomized_algorithms/reservoir_sampling.py:1:1: I001 Import block is un-sorted or un-formatted

T = TypeVar("T")


def reservoir_sampling(stream: Iterator[T], k: int) -> List[T]:

Check failure on line 7 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP047)

randomized_algorithms/reservoir_sampling.py:7:5: UP047 Generic function `reservoir_sampling` should use type parameters

Check failure on line 7 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP047)

randomized_algorithms/reservoir_sampling.py:7:5: UP047 Generic function `reservoir_sampling` should use type parameters

Check failure on line 7 in randomized_algorithms/reservoir_sampling.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP047)

randomized_algorithms/reservoir_sampling.py:7:5: UP047 Generic function `reservoir_sampling` should use type parameters

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: k

"""
Randomly select k items from a stream of unknown length using reservoir sampling.

Args:
stream (Iterator[T]): Input data stream.
k (int): Number of items to sample.

Returns:
List[T]: List of k randomly sampled items.

Examples:
>>> stream = iter(range(1, 11))
>>> len(reservoir_sampling(stream, 5))
5
"""
reservoir: List[T] = []
for i, item in enumerate(stream):
if i < k:
reservoir.append(item)
else:
j = random.randint(0, i)
if j < k:
reservoir[j] = item
return reservoir


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading