Skip to content

Commit c5c5bb2

Browse files
authored
Merge branch 'TheAlgorithms:master' into time_period_simple_pendulum
2 parents 6eb3556 + c850227 commit c5c5bb2

File tree

4 files changed

+135
-8
lines changed

4 files changed

+135
-8
lines changed

Diff for: data_structures/stacks/infix_to_prefix_conversion.py

+73
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@
1616

1717

1818
def infix_2_postfix(infix):
19+
"""
20+
>>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
21+
Symbol | Stack | Postfix
22+
----------------------------
23+
a | | a
24+
+ | + | a
25+
b | + | ab
26+
^ | +^ | ab
27+
c | +^ | abc
28+
| + | abc^
29+
| | abc^+
30+
'abc^+'
31+
>>> infix_2_postfix("1*((-a)*2+b)")
32+
Traceback (most recent call last):
33+
...
34+
KeyError: '('
35+
>>> infix_2_postfix("")
36+
Symbol | Stack | Postfix
37+
----------------------------
38+
''
39+
>>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE
40+
Symbol | Stack | Postfix
41+
----------------------------
42+
( | ( |
43+
( | (( |
44+
) | ( |
45+
| | (
46+
'('
47+
>>> infix_2_postfix("())")
48+
Traceback (most recent call last):
49+
...
50+
IndexError: list index out of range
51+
"""
1952
stack = []
2053
post_fix = []
2154
priority = {
@@ -74,6 +107,42 @@ def infix_2_postfix(infix):
74107

75108

76109
def infix_2_prefix(infix):
110+
"""
111+
>>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
112+
Symbol | Stack | Postfix
113+
----------------------------
114+
c | | c
115+
^ | ^ | c
116+
b | ^ | cb
117+
+ | + | cb^
118+
a | + | cb^a
119+
| | cb^a+
120+
'+a^bc'
121+
122+
>>> infix_2_prefix("1*((-a)*2+b)")
123+
Traceback (most recent call last):
124+
...
125+
KeyError: '('
126+
127+
>>> infix_2_prefix('')
128+
Symbol | Stack | Postfix
129+
----------------------------
130+
''
131+
132+
>>> infix_2_prefix('(()')
133+
Traceback (most recent call last):
134+
...
135+
IndexError: list index out of range
136+
137+
>>> infix_2_prefix('())') # doctest: +NORMALIZE_WHITESPACE
138+
Symbol | Stack | Postfix
139+
----------------------------
140+
( | ( |
141+
( | (( |
142+
) | ( |
143+
| | (
144+
'('
145+
"""
77146
infix = list(infix[::-1]) # reverse the infix equation
78147

79148
for i in range(len(infix)):
@@ -88,6 +157,10 @@ def infix_2_prefix(infix):
88157

89158

90159
if __name__ == "__main__":
160+
from doctest import testmod
161+
162+
testmod()
163+
91164
Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation
92165
Infix = "".join(Infix.split()) # Remove spaces from the input
93166
print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")

Diff for: machine_learning/k_means_clust.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def report_generator(
235235
] # group by cluster number
236236
.agg(
237237
[
238-
("sum", np.sum),
238+
("sum", "sum"),
239239
("mean_with_zeros", lambda x: np.mean(np.nan_to_num(x))),
240240
("mean_without_zeros", lambda x: x.replace(0, np.NaN).mean()),
241241
(
@@ -248,7 +248,7 @@ def report_generator(
248248
)
249249
),
250250
),
251-
("mean_with_na", np.mean),
251+
("mean_with_na", "mean"),
252252
("min", lambda x: x.min()),
253253
("5%", lambda x: x.quantile(0.05)),
254254
("25%", lambda x: x.quantile(0.25)),

Diff for: maths/carmichael_number.py

+49-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@
1010
Examples of Carmichael Numbers: 561, 1105, ...
1111
https://en.wikipedia.org/wiki/Carmichael_number
1212
"""
13+
1314
from maths.greatest_common_divisor import greatest_common_divisor
1415

1516

1617
def power(x: int, y: int, mod: int) -> int:
18+
"""
19+
20+
Examples:
21+
>>> power(2, 15, 3)
22+
2
23+
24+
>>> power(5, 1, 30)
25+
5
26+
"""
27+
1728
if y == 0:
1829
return 1
1930
temp = power(x, y // 2, mod) % mod
@@ -24,15 +35,47 @@ def power(x: int, y: int, mod: int) -> int:
2435

2536

2637
def is_carmichael_number(n: int) -> bool:
27-
b = 2
28-
while b < n:
29-
if greatest_common_divisor(b, n) == 1 and power(b, n - 1, n) != 1:
30-
return False
31-
b += 1
32-
return True
38+
"""
39+
40+
Examples:
41+
>>> is_carmichael_number(562)
42+
False
43+
44+
>>> is_carmichael_number(561)
45+
True
46+
47+
>>> is_carmichael_number(5.1)
48+
Traceback (most recent call last):
49+
...
50+
ValueError: Number 5.1 must instead be a positive integer
51+
52+
>>> is_carmichael_number(-7)
53+
Traceback (most recent call last):
54+
...
55+
ValueError: Number -7 must instead be a positive integer
56+
57+
>>> is_carmichael_number(0)
58+
Traceback (most recent call last):
59+
...
60+
ValueError: Number 0 must instead be a positive integer
61+
"""
62+
63+
if n <= 0 or not isinstance(n, int):
64+
msg = f"Number {n} must instead be a positive integer"
65+
raise ValueError(msg)
66+
67+
return all(
68+
power(b, n - 1, n) == 1
69+
for b in range(2, n)
70+
if greatest_common_divisor(b, n) == 1
71+
)
3372

3473

3574
if __name__ == "__main__":
75+
import doctest
76+
77+
doctest.testmod()
78+
3679
number = int(input("Enter number: ").strip())
3780
if is_carmichael_number(number):
3881
print(f"{number} is a Carmichael Number.")

Diff for: maths/primelib.py

+11
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,11 @@ def fib(n):
574574
"""
575575
input: positive integer 'n'
576576
returns the n-th fibonacci term , indexing by 0
577+
578+
>>> fib(5)
579+
8
580+
>>> fib(99)
581+
354224848179261915075
577582
"""
578583

579584
# precondition
@@ -589,3 +594,9 @@ def fib(n):
589594
fib1 = tmp
590595

591596
return ans
597+
598+
599+
if __name__ == "__main__":
600+
import doctest
601+
602+
doctest.testmod()

0 commit comments

Comments
 (0)