Skip to content

Commit e688227

Browse files
brunohadlichstokhos
authored andcommitted
Fixes for issue "Fix the LGTM issues TheAlgorithms#1024" (TheAlgorithms#1034)
* Added doctest and more explanation about Dijkstra execution. * tests were not passing with python2 due to missing __init__.py file at number_theory folder * Removed the dot at the beginning of the imported modules names because 'python3 -m doctest -v data_structures/hashing/*.py' and 'python3 -m doctest -v data_structures/stacks/*.py' were failing not finding hash_table.py and stack.py modules. * Moved global code to main scope and added doctest for project euler problems 1 to 14. * Added test case for negative input. * Changed N variable to do not use end of line scape because in case there is a space after it the script will break making it much more error prone. * Added problems description and doctests to the ones that were missing. Limited line length to 79 and executed python black over all scripts. * Changed the way files are loaded to support pytest call. * Added __init__.py to problems to make them modules and allow pytest execution. * Added project_euler folder to test units execution * Changed 'os.path.split(os.path.realpath(__file__))' to 'os.path.dirname()' * Added Burrows-Wheeler transform algorithm. * Added changes suggested by cclauss * Fixes for issue 'Fix the LGTM issues TheAlgorithms#1024'. * Added doctest for different parameter types and negative values. * Fixed doctest issue added at last commit.
1 parent f7df8e8 commit e688227

File tree

8 files changed

+125
-10
lines changed

8 files changed

+125
-10
lines changed

Diff for: project_euler/problem_02/sol4.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212
from __future__ import print_function
1313
import math
14-
from decimal import *
14+
from decimal import Decimal, getcontext
1515

1616
try:
1717
raw_input # Python 2
@@ -33,7 +33,31 @@ def solution(n):
3333
0
3434
>>> solution(34)
3535
44
36+
>>> solution(3.4)
37+
2
38+
>>> solution(0)
39+
Traceback (most recent call last):
40+
...
41+
ValueError: Parameter n must be greater or equal to one.
42+
>>> solution(-17)
43+
Traceback (most recent call last):
44+
...
45+
ValueError: Parameter n must be greater or equal to one.
46+
>>> solution([])
47+
Traceback (most recent call last):
48+
...
49+
TypeError: Parameter n must be int or passive of cast to int.
50+
>>> solution("asd")
51+
Traceback (most recent call last):
52+
...
53+
TypeError: Parameter n must be int or passive of cast to int.
3654
"""
55+
try:
56+
n = int(n)
57+
except (TypeError, ValueError) as e:
58+
raise TypeError("Parameter n must be int or passive of cast to int.")
59+
if n <= 0:
60+
raise ValueError("Parameter n must be greater or equal to one.")
3761
getcontext().prec = 100
3862
phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2)
3963

Diff for: project_euler/problem_03/sol1.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,38 @@ def isprime(no):
2828

2929
def solution(n):
3030
"""Returns the largest prime factor of a given number n.
31-
31+
3232
>>> solution(13195)
3333
29
3434
>>> solution(10)
3535
5
3636
>>> solution(17)
3737
17
38+
>>> solution(3.4)
39+
3
40+
>>> solution(0)
41+
Traceback (most recent call last):
42+
...
43+
ValueError: Parameter n must be greater or equal to one.
44+
>>> solution(-17)
45+
Traceback (most recent call last):
46+
...
47+
ValueError: Parameter n must be greater or equal to one.
48+
>>> solution([])
49+
Traceback (most recent call last):
50+
...
51+
TypeError: Parameter n must be int or passive of cast to int.
52+
>>> solution("asd")
53+
Traceback (most recent call last):
54+
...
55+
TypeError: Parameter n must be int or passive of cast to int.
3856
"""
57+
try:
58+
n = int(n)
59+
except (TypeError, ValueError) as e:
60+
raise TypeError("Parameter n must be int or passive of cast to int.")
61+
if n <= 0:
62+
raise ValueError("Parameter n must be greater or equal to one.")
3963
maxNumber = 0
4064
if isprime(n):
4165
return n
@@ -54,7 +78,6 @@ def solution(n):
5478
elif isprime(i):
5579
maxNumber = i
5680
return maxNumber
57-
return int(sum)
5881

5982

6083
if __name__ == "__main__":

Diff for: project_euler/problem_03/sol2.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
77
"""
88
from __future__ import print_function, division
9-
import math
109

1110
try:
1211
raw_input # Python 2
@@ -16,14 +15,38 @@
1615

1716
def solution(n):
1817
"""Returns the largest prime factor of a given number n.
19-
18+
2019
>>> solution(13195)
2120
29
2221
>>> solution(10)
2322
5
2423
>>> solution(17)
2524
17
25+
>>> solution(3.4)
26+
3
27+
>>> solution(0)
28+
Traceback (most recent call last):
29+
...
30+
ValueError: Parameter n must be greater or equal to one.
31+
>>> solution(-17)
32+
Traceback (most recent call last):
33+
...
34+
ValueError: Parameter n must be greater or equal to one.
35+
>>> solution([])
36+
Traceback (most recent call last):
37+
...
38+
TypeError: Parameter n must be int or passive of cast to int.
39+
>>> solution("asd")
40+
Traceback (most recent call last):
41+
...
42+
TypeError: Parameter n must be int or passive of cast to int.
2643
"""
44+
try:
45+
n = int(n)
46+
except (TypeError, ValueError) as e:
47+
raise TypeError("Parameter n must be int or passive of cast to int.")
48+
if n <= 0:
49+
raise ValueError("Parameter n must be greater or equal to one.")
2750
prime = 1
2851
i = 2
2952
while i * i <= n:

Diff for: project_euler/problem_05/sol1.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
def solution(n):
1818
"""Returns the smallest positive number that is evenly divisible(divisible
1919
with no remainder) by all of the numbers from 1 to n.
20-
20+
2121
>>> solution(10)
2222
2520
2323
>>> solution(15)
@@ -26,7 +26,31 @@ def solution(n):
2626
232792560
2727
>>> solution(22)
2828
232792560
29+
>>> solution(3.4)
30+
6
31+
>>> solution(0)
32+
Traceback (most recent call last):
33+
...
34+
ValueError: Parameter n must be greater or equal to one.
35+
>>> solution(-17)
36+
Traceback (most recent call last):
37+
...
38+
ValueError: Parameter n must be greater or equal to one.
39+
>>> solution([])
40+
Traceback (most recent call last):
41+
...
42+
TypeError: Parameter n must be int or passive of cast to int.
43+
>>> solution("asd")
44+
Traceback (most recent call last):
45+
...
46+
TypeError: Parameter n must be int or passive of cast to int.
2947
"""
48+
try:
49+
n = int(n)
50+
except (TypeError, ValueError) as e:
51+
raise TypeError("Parameter n must be int or passive of cast to int.")
52+
if n <= 0:
53+
raise ValueError("Parameter n must be greater or equal to one.")
3054
i = 0
3155
while 1:
3256
i += n * (n - 1)
@@ -39,7 +63,6 @@ def solution(n):
3963
if i == 0:
4064
i = 1
4165
return i
42-
break
4366

4467

4568
if __name__ == "__main__":

Diff for: project_euler/problem_07/sol2.py

+24
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,31 @@ def solution(n):
3636
229
3737
>>> solution(100)
3838
541
39+
>>> solution(3.4)
40+
5
41+
>>> solution(0)
42+
Traceback (most recent call last):
43+
...
44+
ValueError: Parameter n must be greater or equal to one.
45+
>>> solution(-17)
46+
Traceback (most recent call last):
47+
...
48+
ValueError: Parameter n must be greater or equal to one.
49+
>>> solution([])
50+
Traceback (most recent call last):
51+
...
52+
TypeError: Parameter n must be int or passive of cast to int.
53+
>>> solution("asd")
54+
Traceback (most recent call last):
55+
...
56+
TypeError: Parameter n must be int or passive of cast to int.
3957
"""
58+
try:
59+
n = int(n)
60+
except (TypeError, ValueError) as e:
61+
raise TypeError("Parameter n must be int or passive of cast to int.")
62+
if n <= 0:
63+
raise ValueError("Parameter n must be greater or equal to one.")
4064
primes = []
4165
num = 2
4266
while len(primes) < n:

Diff for: project_euler/problem_09/sol1.py

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def solution():
2828
if (a ** 2) + (b ** 2) == (c ** 2):
2929
if (a + b + c) == 1000:
3030
return a * b * c
31-
break
3231

3332

3433
if __name__ == "__main__":

Diff for: project_euler/problem_19/sol1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ def solution():
6161

6262

6363
if __name__ == "__main__":
64-
print(solution(171))
64+
print(solution())

Diff for: project_euler/problem_234/sol1.py

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def solution(n):
4040
semidivisible = []
4141
for x in range(n):
4242
l=[i for i in input().split()]
43-
c1=0
4443
c2=1
4544
while(1):
4645
if len(fib(l[0],l[1],c2))<int(l[2]):

0 commit comments

Comments
 (0)