Compare commits

...

31 Commits

Author SHA1 Message Date
DIVYASREE S
07e2ea5652
Merge 9adea0af2e into e3bd7721c8 2024-11-18 17:13:04 +05:30
Christian Clauss
e3bd7721c8
validate_filenames.py Shebang python for Windows (#12371) 2024-11-15 14:59:14 +01:00
pre-commit-ci[bot]
e3f3d668be
[pre-commit.ci] pre-commit autoupdate (#12370)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.2 → v0.7.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.2...v0.7.3)
- [github.com/abravalheri/validate-pyproject: v0.22 → v0.23](https://github.com/abravalheri/validate-pyproject/compare/v0.22...v0.23)

* Update sudoku_solver.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2024-11-11 21:05:50 +01:00
pre-commit-ci[bot]
3e9ca92ca9
[pre-commit.ci] pre-commit autoupdate (#12349)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.1 → v0.7.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.1...v0.7.2)
- [github.com/tox-dev/pyproject-fmt: v2.4.3 → v2.5.0](https://github.com/tox-dev/pyproject-fmt/compare/v2.4.3...v2.5.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-04 21:09:03 +01:00
pre-commit-ci[bot]
9adea0af2e [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 17:16:47 +00:00
DIVYASREE S
9403de6894
Update proof_of_work.py 2024-10-10 22:46:12 +05:30
pre-commit-ci[bot]
5d87a84ca0 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 17:01:05 +00:00
DIVYASREE S
38904a32f7
Update proof_of_work.py 2024-10-10 22:30:29 +05:30
DIVYASREE S
1e3b382c5d
Update proof_of_work.py 2024-10-10 22:28:24 +05:30
pre-commit-ci[bot]
6ee3d9e1e6 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:50:29 +00:00
DIVYASREE S
d6ab438d90
Updated proof_of_work.py 2024-10-10 22:20:05 +05:30
pre-commit-ci[bot]
5e70415ced [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:45:32 +00:00
DIVYASREE S
cd42352303
Added proof_of_work.py 2024-10-10 22:14:01 +05:30
DIVYASREE S
8ad73811e5
Delete blockchain/proof_of_work.py 2024-10-10 22:04:33 +05:30
pre-commit-ci[bot]
44cb9a30f7 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:33:40 +00:00
DIVYASREE S
f55f9aba89
Updated proof_of_work.py 2024-10-10 22:03:33 +05:30
DIVYASREE S
9aa315441b
Updated proof_of_work.py 2024-10-10 22:01:48 +05:30
DIVYASREE S
390cd7e12e
Updated proof_of_work.py 2024-10-10 22:00:17 +05:30
pre-commit-ci[bot]
bbe09d7c4c [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:22:37 +00:00
DIVYASREE S
5a7b2ffbba
Updated proof_of_work.py 2024-10-10 21:52:14 +05:30
pre-commit-ci[bot]
bd89e9705f [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:20:58 +00:00
DIVYASREE S
374cbc2bf9
Created proof_of_work.py 2024-10-10 21:50:01 +05:30
DIVYASREE S
e14f6d5122
Updated proof_of_stake.py 2024-10-10 21:46:12 +05:30
pre-commit-ci[bot]
8b9ff8d4f3 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:12:09 +00:00
DIVYASREE S
df4c444095
Created and updated proof_of_stake.py 2024-10-10 21:40:33 +05:30
DIVYASREE S
b1c7d37812
Delete blockchain/proof_of_stake.py 2024-10-10 21:39:40 +05:30
pre-commit-ci[bot]
2046ea45bc [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 16:03:51 +00:00
DIVYASREE S
82064e3a23
Changes to proof_of_stake.py 2024-10-10 21:32:04 +05:30
pre-commit-ci[bot]
d770fa5bf5 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-10 15:54:31 +00:00
DIVYASREE S
28df0f3c9b
Updated proof_of_stake.py 2024-10-10 21:17:13 +05:30
DIVYASREE S
f467cec98a
Created proof_of_stake.py 2024-10-10 21:12:51 +05:30
5 changed files with 74 additions and 5 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus - id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.1 rev: v0.7.3
hooks: hooks:
- id: ruff - id: ruff
- id: ruff-format - id: ruff-format
@ -29,7 +29,7 @@ repos:
- tomli - tomli
- repo: https://github.com/tox-dev/pyproject-fmt - repo: https://github.com/tox-dev/pyproject-fmt
rev: "v2.4.3" rev: "v2.5.0"
hooks: hooks:
- id: pyproject-fmt - id: pyproject-fmt
@ -42,7 +42,7 @@ repos:
pass_filenames: false pass_filenames: false
- repo: https://github.com/abravalheri/validate-pyproject - repo: https://github.com/abravalheri/validate-pyproject
rev: v0.22 rev: v0.23
hooks: hooks:
- id: validate-pyproject - id: validate-pyproject

View File

@ -0,0 +1,40 @@
import random
class Validator:
def __init__(self, name: str, stake: int) -> None:
"""
Initializes a new validator with a given name and stake.
Args:
name (str): The name of the validator.
stake (int): The amount of stake the validator has.
"""
self.name = name
self.stake = stake
def choose_validator(validators: list[Validator]) -> Validator:
"""
Selects a validator to create the next block based on the weight of their stake.
The higher the stake, the greater the chance to be selected.
Args:
validators (list[Validator]): A list of Validator objects.
Returns:
Validator: The selected validator based on weighted random selection.
Example:
>>> validators = [Validator("Alice", 50), Validator("Bob", 30)]
>>> chosen = choose_validator(validators)
>>> isinstance(chosen, Validator)
True
"""
total_stake = sum(v.stake for v in validators)
weighted_validators = [(v, v.stake / total_stake) for v in validators]
selected = random.choices(
[v[0] for v in weighted_validators], weights=[v[1] for v in weighted_validators]
)
return selected[0]

View File

@ -0,0 +1,29 @@
import hashlib
def proof_of_work(difficulty: int) -> int:
"""
Simulates a Proof of Work mining process.
The miner must find a nonce such that the hash of the nonce starts
with a specific number of leading zeros (difficulty).
Args:
difficulty (int): The number of leading zeros required in the hash.
Returns:
int: The nonce value that solves the puzzle.
Example:
>>> result = proof_of_work(2) # Difficulty of 2 should be fast
>>> isinstance(result, int)
True
"""
prefix = "0" * difficulty
nonce = 0
while True:
hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest()
if hash_result.startswith(prefix):
return nonce
nonce += 1

View File

@ -172,7 +172,7 @@ def solved(values):
def from_file(filename, sep="\n"): def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep." "Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115 return open(filename).read().strip().split(sep)
def random_puzzle(assignments=17): def random_puzzle(assignments=17):

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!python
import os import os
try: try: