Skip to content

Commit 67a0fae

Browse files
authored
Merge branch 'master' into feature/algorithm/Valid-Number
2 parents e3f077c + c521243 commit 67a0fae

6 files changed

Lines changed: 48 additions & 41 deletions

File tree

‎maths/fermat_little_theorem.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@
55
# Wikipedia reference: https://en.wikipedia.org/wiki/Fermat%27s_little_theorem
66

77

8-
def binary_exponentiation(a: int, n: float, mod: int) -> int:
8+
def binary_exponentiation(a: int, n: int, mod: int) -> int:
9+
"""
10+
Calculate (a ** n) % mod using binary exponentiation, which runs in O(log n) time.
11+
12+
>>> binary_exponentiation(2, 10, 17)
13+
4
14+
>>> binary_exponentiation(3, 0, 5)
15+
1
16+
>>> binary_exponentiation(5, 3, 13)
17+
8
18+
"""
919
if n == 0:
1020
return 1
1121

1222
elif n % 2 == 1:
1323
return (binary_exponentiation(a, n - 1, mod) * a) % mod
1424

1525
else:
16-
b = binary_exponentiation(a, n / 2, mod)
26+
b = binary_exponentiation(a, n // 2, mod)
1727
return (b * b) % mod
1828

1929

‎pyproject.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ dependencies = [
2222
"numpy>=2.1.3",
2323
"pandas>=2.3.3",
2424
"pillow>=11.3",
25-
"polars>=1.44.2",
2625
"rich>=13.9.4",
2726
"scikit-learn>=1.9.1",
2827
"scipy>=1.18.1",

‎sorts/reversort.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
python reversort.py
1818
"""
1919

20-
from typing import Any
20+
from typing import Any, Protocol
2121

2222

23-
def reversort(collection: list[Any]) -> list[Any]:
23+
class Comparable(Protocol):
24+
def __lt__(self, other: Any, /) -> bool: ...
25+
26+
27+
def reversort[T: Comparable](collection: list[T]) -> list[T]:
2428
"""
2529
Sort a list using the Reversort algorithm.
2630
@@ -78,6 +82,10 @@ def reversort(collection: list[Any]) -> list[Any]:
7882
>>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
7983
>>> reversort(collection_arg) == sorted(collection_arg)
8084
True
85+
>>> reversort([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
86+
Traceback (most recent call last):
87+
...
88+
TypeError: '<' not supported between instances of 'str' and 'int'
8189
"""
8290
arr = collection[:] # Create a copy to avoid modifying the original
8391
n = len(arr)
@@ -96,7 +104,7 @@ def reversort(collection: list[Any]) -> list[Any]:
96104
return arr
97105

98106

99-
def reversort_cost(collection: list[Any]) -> int:
107+
def reversort_cost[T: Comparable](collection: list[T]) -> int:
100108
"""
101109
Calculate the cost of sorting using Reversort.
102110
@@ -119,6 +127,10 @@ def reversort_cost(collection: list[Any]) -> int:
119127
0
120128
>>> reversort_cost([])
121129
0
130+
>>> reversort_cost([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
131+
Traceback (most recent call last):
132+
...
133+
TypeError: '<' not supported between instances of 'str' and 'int'
122134
"""
123135
arr = collection[:] # Create a copy to avoid modifying the original
124136
n = len(arr)

‎tests/test_sorts.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from sorts.patience_sort import patience_sort
4242
from sorts.quick_sort import quick_sort
4343
from sorts.recursive_insertion_sort import rec_insertion_sort
44+
from sorts.reversort import reversort
4445
from sorts.selection_sort import selection_sort
4546
from sorts.shell_sort import shell_sort
4647
from sorts.shrink_shell_sort import shell_sort as shrink_shell_sort
@@ -74,6 +75,7 @@ def test_heap_sort() -> None:
7475
pancake_sort,
7576
patience_sort,
7677
quick_sort,
78+
reversort,
7779
selection_sort,
7880
shell_sort,
7981
shrink_shell_sort,
@@ -141,6 +143,7 @@ def test_rec_insertion_sort(case) -> None:
141143
merge_sort,
142144
odd_even_sort,
143145
pancake_sort,
146+
reversort,
144147
selection_sort,
145148
shrink_shell_sort,
146149
strand_sort,

‎uv.lock‎

Lines changed: 0 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎web_programming/crawl_google_scholar_citation.py‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
Get the citation from google scholar
3-
using title and year of publication, and volume and pages of journal.
2+
Get the citation from Google Scholar using the title and year of publication, and the
3+
journal volume and pages.
44
"""
55

66
# /// script
@@ -17,17 +17,29 @@
1717

1818
def get_citation(base_url: str, params: dict) -> str:
1919
"""
20-
Return the citation number.
20+
Returns the citation number for a publication based on its title, journal, volume,
21+
pages, and year of publication.
22+
23+
Parameters:
24+
- base_url: The base URL for making requests to Google Scholar.
25+
- params: A dictionary containing the publication information.
26+
27+
Returns:
28+
- A string containing the number of citations.
2129
"""
30+
# Send a GET request to the URL with the specified parameters
2231
soup = BeautifulSoup(
2332
httpx2.get(base_url, params=params, timeout=10).content, "html.parser"
2433
)
34+
# Find the div element with class 'gs_ri' that contains citation information
2535
div = soup.find("div", attrs={"class": "gs_ri"})
36+
# Find all links in the div and retrieve the third link (the citation count)
2637
anchors = div.find("div", attrs={"class": "gs_fl"}).find_all("a")
27-
return anchors[2].get_text()
38+
return anchors[2].get_text() # Return the text from the third link
2839

2940

3041
if __name__ == "__main__":
42+
# Define parameters for the publication whose citation is to be searched
3143
params = {
3244
"title": (
3345
"Precisely geometry controlled microsupercapacitors for ultrahigh areal "
@@ -37,6 +49,7 @@ def get_citation(base_url: str, params: dict) -> str:
3749
"volume": 30,
3850
"pages": "3979-3990",
3951
"year": 2018,
40-
"hl": "en",
52+
"hl": "en", # Language to be used (English)
4153
}
54+
# Call the get_citation function with the specified URL and parameters
4255
print(get_citation("https://scholar.google.com/scholar_lookup", params=params))

0 commit comments

Comments
 (0)