mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Merge branch 'TheAlgorithms:master' into master
This commit is contained in:
commit
1720083569
@ -16,7 +16,7 @@ repos:
|
||||
- id: auto-walrus
|
||||
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.0.291
|
||||
rev: v0.0.292
|
||||
hooks:
|
||||
- id: ruff
|
||||
|
||||
@ -33,7 +33,7 @@ repos:
|
||||
- tomli
|
||||
|
||||
- repo: https://github.com/tox-dev/pyproject-fmt
|
||||
rev: "1.1.0"
|
||||
rev: "1.2.0"
|
||||
hooks:
|
||||
- id: pyproject-fmt
|
||||
|
||||
|
@ -233,6 +233,7 @@
|
||||
* [Merge Two Lists](data_structures/linked_list/merge_two_lists.py)
|
||||
* [Middle Element Of Linked List](data_structures/linked_list/middle_element_of_linked_list.py)
|
||||
* [Print Reverse](data_structures/linked_list/print_reverse.py)
|
||||
* [Reverse K Group](data_structures/linked_list/reverse_k_group.py)
|
||||
* [Rotate To The Right](data_structures/linked_list/rotate_to_the_right.py)
|
||||
* [Singly Linked List](data_structures/linked_list/singly_linked_list.py)
|
||||
* [Skip List](data_structures/linked_list/skip_list.py)
|
||||
|
@ -1,69 +0,0 @@
|
||||
"""
|
||||
Minimax helps to achieve maximum score in a game by checking all possible moves.
|
||||
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
|
||||
def minimax(
|
||||
depth: int, node_index: int, is_max: bool, scores: list[int], height: float
|
||||
) -> int:
|
||||
"""
|
||||
depth is current depth in game tree.
|
||||
node_index is index of current node in scores[].
|
||||
scores[] contains the leaves of game tree.
|
||||
height is maximum height of game tree.
|
||||
|
||||
>>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]
|
||||
>>> height = math.log(len(scores), 2)
|
||||
>>> minimax(0, 0, True, scores, height)
|
||||
65
|
||||
>>> minimax(-1, 0, True, scores, height)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Depth cannot be less than 0
|
||||
>>> minimax(0, 0, True, [], 2)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Scores cannot be empty
|
||||
>>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
|
||||
>>> height = math.log(len(scores), 2)
|
||||
>>> minimax(0, 0, True, scores, height)
|
||||
12
|
||||
"""
|
||||
|
||||
if depth < 0:
|
||||
raise ValueError("Depth cannot be less than 0")
|
||||
|
||||
if not scores:
|
||||
raise ValueError("Scores cannot be empty")
|
||||
|
||||
if depth == height:
|
||||
return scores[node_index]
|
||||
|
||||
return (
|
||||
max(
|
||||
minimax(depth + 1, node_index * 2, False, scores, height),
|
||||
minimax(depth + 1, node_index * 2 + 1, False, scores, height),
|
||||
)
|
||||
if is_max
|
||||
else min(
|
||||
minimax(depth + 1, node_index * 2, True, scores, height),
|
||||
minimax(depth + 1, node_index * 2 + 1, True, scores, height),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
|
||||
height = math.log(len(scores), 2)
|
||||
print(f"Optimal value : {minimax(0, 0, True, scores, height)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
main()
|
@ -1,51 +0,0 @@
|
||||
import random
|
||||
|
||||
from .binary_exp_mod import bin_exp_mod
|
||||
|
||||
|
||||
# This is a probabilistic check to test primality, useful for big numbers!
|
||||
# if it's a prime, it will return true
|
||||
# if it's not a prime, the chance of it returning true is at most 1/4**prec
|
||||
def is_prime_big(n, prec=1000):
|
||||
"""
|
||||
>>> from maths.prime_check import is_prime
|
||||
>>> # all(is_prime_big(i) == is_prime(i) for i in range(1000)) # 3.45s
|
||||
>>> all(is_prime_big(i) == is_prime(i) for i in range(256))
|
||||
True
|
||||
"""
|
||||
if n < 2:
|
||||
return False
|
||||
|
||||
if n % 2 == 0:
|
||||
return n == 2
|
||||
|
||||
# this means n is odd
|
||||
d = n - 1
|
||||
exp = 0
|
||||
while d % 2 == 0:
|
||||
d /= 2
|
||||
exp += 1
|
||||
|
||||
# n - 1=d*(2**exp)
|
||||
count = 0
|
||||
while count < prec:
|
||||
a = random.randint(2, n - 1)
|
||||
b = bin_exp_mod(a, d, n)
|
||||
if b != 1:
|
||||
flag = True
|
||||
for _ in range(exp):
|
||||
if b == n - 1:
|
||||
flag = False
|
||||
break
|
||||
b = b * b
|
||||
b %= n
|
||||
if flag:
|
||||
return False
|
||||
count += 1
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
n = abs(int(input("Enter bound : ").strip()))
|
||||
print("Here's the list of primes:")
|
||||
print(", ".join(str(i) for i in range(n + 1) if is_prime_big(i)))
|
Loading…
x
Reference in New Issue
Block a user