Skip to content

Commit 57c1673

Browse files
Merge branch 'master' into use-deque-in-BFS-shortest-path
2 parents e005b12 + 561cc38 commit 57c1673

File tree

15 files changed

+174
-117
lines changed

15 files changed

+174
-117
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
build:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- uses: actions/checkout@v4
12+
- uses: actions/checkout@v5
1313
- uses: astral-sh/setup-uv@v6
1414
with:
1515
enable-cache: true

.github/workflows/devcontainer_ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
build:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v4
15+
- uses: actions/checkout@v5
1616
- uses: devcontainers/[email protected]
1717
with:
1818
push: never

.github/workflows/directory_writer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
directory_writer:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v4
9+
- uses: actions/checkout@v5
1010
with:
1111
fetch-depth: 0
1212
- uses: actions/setup-python@v5

.github/workflows/project_euler.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
project-euler:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v4
17+
- uses: actions/checkout@v5
1818
- uses: astral-sh/setup-uv@v6
1919
- uses: actions/setup-python@v5
2020
with:
@@ -24,7 +24,7 @@ jobs:
2424
validate-solutions:
2525
runs-on: ubuntu-latest
2626
steps:
27-
- uses: actions/checkout@v4
27+
- uses: actions/checkout@v5
2828
- uses: astral-sh/setup-uv@v6
2929
- uses: actions/setup-python@v5
3030
with:

.github/workflows/ruff.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
ruff:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v4
14+
- uses: actions/checkout@v5
1515
- uses: astral-sh/setup-uv@v6
1616
- run: uvx ruff check --output-format=github .

.github/workflows/sphinx.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
build_docs:
2626
runs-on: ubuntu-24.04-arm
2727
steps:
28-
- uses: actions/checkout@v4
28+
- uses: actions/checkout@v5
2929
- uses: astral-sh/setup-uv@v6
3030
- uses: actions/setup-python@v5
3131
with:
@@ -34,7 +34,7 @@ jobs:
3434
- run: uv sync --group=docs
3535
- uses: actions/configure-pages@v5
3636
- run: uv run sphinx-build -c docs . docs/_build/html
37-
- uses: actions/upload-pages-artifact@v3
37+
- uses: actions/upload-pages-artifact@v4
3838
with:
3939
path: docs/_build/html
4040

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v5.0.0
3+
rev: v6.0.0
44
hooks:
55
- id: check-executables-have-shebangs
66
- id: check-toml
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.12.5
19+
rev: v0.12.9
2020
hooks:
2121
- id: ruff-check
2222
- id: ruff-format

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<a href="https://github.com/pre-commit/pre-commit">
2828
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" height="20" alt="pre-commit">
2929
</a>
30-
<a href="https://github.com/psf/black">
31-
<img src="https://img.shields.io/static/v1?label=code%20style&message=black&color=black&style=flat-square" height="20" alt="code style: black">
30+
<a href="https://docs.astral.sh/ruff/formatter/">
31+
<img src="https://img.shields.io/static/v1?label=code%20style&message=ruff&color=black&style=flat-square" height="20" alt="code style: black">
3232
</a>
3333
<!-- Short description: -->
3434
<h3>All algorithms implemented in Python - for education</h3>

bit_manipulation/reverse_bits.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
11
def get_reverse_bit_string(number: int) -> str:
22
"""
3-
return the bit string of an integer
3+
Return the reverse bit string of a 32 bit integer
44
55
>>> get_reverse_bit_string(9)
66
'10010000000000000000000000000000'
77
>>> get_reverse_bit_string(43)
88
'11010100000000000000000000000000'
99
>>> get_reverse_bit_string(2873)
1010
'10011100110100000000000000000000'
11+
>>> get_reverse_bit_string(2550136832)
12+
'00000000000000000000000000011001'
1113
>>> get_reverse_bit_string("this is not a number")
1214
Traceback (most recent call last):
1315
...
14-
TypeError: operation can not be conducted on a object of type str
16+
TypeError: operation can not be conducted on an object of type str
1517
"""
1618
if not isinstance(number, int):
1719
msg = (
18-
"operation can not be conducted on a object of type "
20+
"operation can not be conducted on an object of type "
1921
f"{type(number).__name__}"
2022
)
2123
raise TypeError(msg)
2224
bit_string = ""
2325
for _ in range(32):
2426
bit_string += str(number % 2)
25-
number = number >> 1
27+
number >>= 1
2628
return bit_string
2729

2830

29-
def reverse_bit(number: int) -> str:
31+
def reverse_bit(number: int) -> int:
3032
"""
31-
Take in an 32 bit integer, reverse its bits,
32-
return a string of reverse bits
33-
34-
result of a reverse_bit and operation on the integer provided.
33+
Take in a 32 bit integer, reverse its bits, return a 32 bit integer result
3534
3635
>>> reverse_bit(25)
37-
'00000000000000000000000000011001'
36+
2550136832
3837
>>> reverse_bit(37)
39-
'00000000000000000000000000100101'
38+
2751463424
4039
>>> reverse_bit(21)
41-
'00000000000000000000000000010101'
40+
2818572288
4241
>>> reverse_bit(58)
43-
'00000000000000000000000000111010'
42+
1543503872
4443
>>> reverse_bit(0)
45-
'00000000000000000000000000000000'
44+
0
4645
>>> reverse_bit(256)
47-
'00000000000000000000000100000000'
46+
8388608
47+
>>> reverse_bit(2550136832)
48+
25
4849
>>> reverse_bit(-1)
4950
Traceback (most recent call last):
5051
...
51-
ValueError: the value of input must be positive
52+
ValueError: The value of input must be non-negative
5253
5354
>>> reverse_bit(1.1)
5455
Traceback (most recent call last):
5556
...
56-
TypeError: Input value must be a 'int' type
57+
TypeError: Input value must be an 'int' type
5758
5859
>>> reverse_bit("0")
5960
Traceback (most recent call last):
6061
...
61-
TypeError: '<' not supported between instances of 'str' and 'int'
62+
TypeError: Input value must be an 'int' type
6263
"""
64+
if not isinstance(number, int):
65+
raise TypeError("Input value must be an 'int' type")
6366
if number < 0:
64-
raise ValueError("the value of input must be positive")
65-
elif isinstance(number, float):
66-
raise TypeError("Input value must be a 'int' type")
67-
elif isinstance(number, str):
68-
raise TypeError("'<' not supported between instances of 'str' and 'int'")
67+
raise ValueError("The value of input must be non-negative")
68+
6969
result = 0
70-
# iterator over [1 to 32],since we are dealing with 32 bit integer
71-
for _ in range(1, 33):
70+
# iterator over [0 to 31], since we are dealing with a 32 bit integer
71+
for _ in range(32):
7272
# left shift the bits by unity
73-
result = result << 1
73+
result <<= 1
7474
# get the end bit
75-
end_bit = number % 2
75+
end_bit = number & 1
7676
# right shift the bits by unity
77-
number = number >> 1
78-
# add that bit to our ans
79-
result = result | end_bit
80-
return get_reverse_bit_string(result)
77+
number >>= 1
78+
# add that bit to our answer
79+
result |= end_bit
80+
return result
8181

8282

8383
if __name__ == "__main__":

boolean_algebra/imply_gate.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,58 @@ def imply_gate(input_1: int, input_2: int) -> int:
3333
return int(input_1 == 0 or input_2 == 1)
3434

3535

36+
def recursive_imply_list(input_list: list[int]) -> int:
37+
"""
38+
Recursively calculates the implication of a list.
39+
Strictly the implication is applied consecutively left to right:
40+
( (a -> b) -> c ) -> d ...
41+
42+
>>> recursive_imply_list([])
43+
Traceback (most recent call last):
44+
...
45+
ValueError: Input list must contain at least two elements
46+
>>> recursive_imply_list([0])
47+
Traceback (most recent call last):
48+
...
49+
ValueError: Input list must contain at least two elements
50+
>>> recursive_imply_list([1])
51+
Traceback (most recent call last):
52+
...
53+
ValueError: Input list must contain at least two elements
54+
>>> recursive_imply_list([0, 0])
55+
1
56+
>>> recursive_imply_list([0, 1])
57+
1
58+
>>> recursive_imply_list([1, 0])
59+
0
60+
>>> recursive_imply_list([1, 1])
61+
1
62+
>>> recursive_imply_list([0, 0, 0])
63+
0
64+
>>> recursive_imply_list([0, 0, 1])
65+
1
66+
>>> recursive_imply_list([0, 1, 0])
67+
0
68+
>>> recursive_imply_list([0, 1, 1])
69+
1
70+
>>> recursive_imply_list([1, 0, 0])
71+
1
72+
>>> recursive_imply_list([1, 0, 1])
73+
1
74+
>>> recursive_imply_list([1, 1, 0])
75+
0
76+
>>> recursive_imply_list([1, 1, 1])
77+
1
78+
"""
79+
if len(input_list) < 2:
80+
raise ValueError("Input list must contain at least two elements")
81+
first_implication = imply_gate(input_list[0], input_list[1])
82+
if len(input_list) == 2:
83+
return first_implication
84+
new_list = [first_implication, *input_list[2:]]
85+
return recursive_imply_list(new_list)
86+
87+
3688
if __name__ == "__main__":
3789
import doctest
3890

0 commit comments

Comments
 (0)