Skip to content

Commit 3259a07

Browse files
nolandercekorre1001Philip SalqvistpsalqvistKubha99
authored
Pylint refactor (#851)
* refactor(pylint): unionfind/count_islands.py improves pylint score from 3.71 to 10.00 fixes KTH-Software-Engineering-DD2480#2 * refactor(pylint): algorithms/search/*.py improves pylint score from 5.83 to 10.00. fixes KTH-Software-Engineering-DD2480#7 * feat: improving pylint score * refactor dp.combination_sum: lint score from 5.42 to 9.13 * refactor(pylint): algorithms/graph/*.py Improves pylint score from 5.51 to 9.96. Score is lower than 10 due to duplication between maximum_flow_bfs.py and maximum_flow_dfs.py. However, due to the educational nature of this repo, keeping it as is will probably be benefitial as it reduces complexity while reading (having to jump between files). fixes KTH-Software-Engineering-DD2480#9 * refactor egg_drop, hosoya_triangle, min_cost_path and planting_trees * feat: improving pylint score of one_sparse_recovery * refactor: move tests from regex_matching to tests folder * refactor: lint score from 4.27 to 9.46 a reason that lint score isn't 10.00 is that the module is called dp, which in turn makes pylint raise invalid name for all files in dp. I leave that as it is, since I don't want to mess with the current folder structure. fixes #3 * Fix: Fixed lint error, lint value now aboe 8 * refactor: add docstring to misra, score 10 * fix (misra): add a newline * refactor: add docstring for one_sparse, score 9.33 * wip: refactor (pylint): algorithms/maths/ wip: #4 * Fix: pylint is above 8 for tree * refactor (pylint): algorithms/maths/ Finished improving pylint score for maths folder. fixes: #4 * Fixed: comments * fix: small intendation fix Co-authored-by: ekorre1001 <[email protected]> Co-authored-by: Philip Salqvist <[email protected]> Co-authored-by: psalqvist <[email protected]> Co-authored-by: Kubha99 <[email protected]> Co-authored-by: mantaur <[email protected]>
1 parent 15e5292 commit 3259a07

File tree

102 files changed

+2235
-1735
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2235
-1735
lines changed

algorithms/dp/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
from .word_break import *
2121
from .int_divide import *
2222
from .k_factor import *
23-
from .planting_trees import *
23+
from .planting_trees import *

algorithms/dp/climbing_stairs.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
"""
22
You are climbing a stair case.
3-
It takes n steps to reach to the top.
3+
It takes `steps` number of steps to reach to the top.
44
55
Each time you can either climb 1 or 2 steps.
66
In how many distinct ways can you climb to the top?
77
8-
Note: Given n will be a positive integer.
8+
Note: Given argument `steps` will be a positive integer.
99
"""
1010

1111

1212
# O(n) space
1313

14-
def climb_stairs(n):
14+
def climb_stairs(steps):
1515
"""
16-
:type n: int
16+
:type steps: int
1717
:rtype: int
1818
"""
1919
arr = [1, 1]
20-
for _ in range(1, n):
20+
for _ in range(1, steps):
2121
arr.append(arr[-1] + arr[-2])
2222
return arr[-1]
2323

2424

2525
# the above function can be optimized as:
2626
# O(1) space
2727

28-
def climb_stairs_optimized(n):
29-
a = b = 1
30-
for _ in range(n):
31-
a, b = b, a + b
32-
return a
28+
def climb_stairs_optimized(steps):
29+
"""
30+
:type steps: int
31+
:rtype: int
32+
"""
33+
a_steps = b_steps = 1
34+
for _ in range(steps):
35+
a_steps, b_steps = b_steps, a_steps + b_steps
36+
return a_steps

algorithms/dp/coin_change.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
"""
22
Problem
3-
Given a value n, if we want to make change for N cents,
4-
and we have infinite supply of each of
5-
coins = {S1, S2, .. , Sm} valued coins, how many ways
6-
can we make the change?
7-
The order of coins doesn't matter.
8-
For example, for n = 4 and coins = [1, 2, 3], there are
9-
four solutions:
3+
Given a value `value`, if we want to make change for `value` cents, and we have infinite
4+
supply of each of coins = {S1, S2, .. , Sm} valued `coins`, how many ways can we make the change?
5+
The order of `coins` doesn't matter.
6+
For example, for `value` = 4 and `coins` = [1, 2, 3], there are four solutions:
107
[1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3].
118
So output should be 4.
129
13-
For n = 10 and coins = [2, 5, 3, 6], there are five solutions:
10+
For `value` = 10 and `coins` = [2, 5, 3, 6], there are five solutions:
11+
1412
[2, 2, 2, 2, 2], [2, 2, 3, 3], [2, 2, 6], [2, 3, 5] and [5, 5].
1513
So the output should be 5.
1614
17-
Time complexity: O(n * m) where n is the value and m is the number of coins
15+
Time complexity: O(n * m) where n is the `value` and m is the number of `coins`
1816
Space complexity: O(n)
1917
"""
2018

19+
def count(coins, value):
20+
""" Find number of combination of `coins` that adds upp to `value`
2121
22-
def count(coins, n):
22+
Keyword arguments:
23+
coins -- int[]
24+
value -- int
25+
"""
2326
# initialize dp array and set base case as 1
24-
dp = [1] + [0] * n
27+
dp_array = [1] + [0] * value
2528

2629
# fill dp in a bottom up manner
2730
for coin in coins:
28-
for i in range(coin, n+1):
29-
dp[i] += dp[i-coin]
31+
for i in range(coin, value+1):
32+
dp_array[i] += dp_array[i-coin]
3033

31-
return dp[n]
34+
return dp_array[value]

algorithms/dp/combination_sum.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,48 @@
2727
2828
"""
2929

30-
dp = None
31-
30+
DP = None
3231

3332
def helper_topdown(nums, target):
34-
global dp
35-
if dp[target] != -1:
36-
return dp[target]
33+
"""Generates DP and finds result.
34+
35+
Keyword arguments:
36+
nums -- positive integer array without duplicates
37+
target -- integer describing what a valid combination should add to
38+
"""
39+
if DP[target] != -1:
40+
return DP[target]
3741
res = 0
38-
for i in range(0, len(nums)):
39-
if target >= nums[i]:
40-
res += helper_topdown(nums, target - nums[i])
41-
dp[target] = res
42+
for num in nums:
43+
if target >= num:
44+
res += helper_topdown(nums, target - num)
45+
DP[target] = res
4246
return res
4347

4448

4549
def combination_sum_topdown(nums, target):
46-
global dp
47-
dp = [-1] * (target + 1)
48-
dp[0] = 1
49-
return helper_topdown(nums, target)
50+
"""Find number of possible combinations in nums that add up to target, in top-down manner.
5051
52+
Keyword arguments:
53+
nums -- positive integer array without duplicates
54+
target -- integer describing what a valid combination should add to
55+
"""
56+
global DP
57+
DP = [-1] * (target + 1)
58+
DP[0] = 1
59+
return helper_topdown(nums, target)
5160

52-
# EDIT: The above solution is top-down. How about a bottom-up one?
5361
def combination_sum_bottom_up(nums, target):
54-
comb = [0] * (target + 1)
55-
comb[0] = 1
56-
for i in range(0, len(comb)):
57-
for j in range(len(nums)):
58-
if i - nums[j] >= 0:
59-
comb[i] += comb[i - nums[j]]
60-
return comb[target]
62+
"""Find number of possible combinations in nums that add up to target, in bottom-up manner.
63+
64+
Keyword arguments:
65+
nums -- positive integer array without duplicates
66+
target -- integer describing what a valid combination should add to
67+
"""
68+
combs = [0] * (target + 1)
69+
combs[0] = 1
70+
for i in range(0, len(combs)):
71+
for num in nums:
72+
if i - num >= 0:
73+
combs[i] += combs[i - num]
74+
return combs[target]

algorithms/dp/edit_distance.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,34 @@
3636
indexes i and j, respectively.
3737
3838
To find the edit distance between two words A and B,
39-
we need to find edit(m, n), where m is the length of A and n
40-
is the length of B.
39+
we need to find edit(length_a, length_b).
40+
41+
Time: O(length_a*length_b)
42+
Space: O(length_a*length_b)
4143
"""
4244

4345

44-
def edit_distance(A, B):
45-
# Time: O(m*n)
46-
# Space: O(m*n)
46+
def edit_distance(word_a, word_b):
47+
"""Finds edit distance between word_a and word_b
48+
49+
Kwyword arguments:
50+
word_a -- string
51+
word_b -- string
52+
"""
4753

48-
m, n = len(A) + 1, len(B) + 1
54+
length_a, length_b = len(word_a) + 1, len(word_b) + 1
4955

50-
edit = [[0 for _ in range(n)] for _ in range(m)]
56+
edit = [[0 for _ in range(length_b)] for _ in range(length_a)]
5157

52-
for i in range(1, m):
58+
for i in range(1, length_a):
5359
edit[i][0] = i
5460

55-
for j in range(1, n):
61+
for j in range(1, length_b):
5662
edit[0][j] = j
5763

58-
for i in range(1, m):
59-
for j in range(1, n):
60-
cost = 0 if A[i - 1] == B[j - 1] else 1
64+
for i in range(1, length_a):
65+
for j in range(1, length_b):
66+
cost = 0 if word_a[i - 1] == word_b[j - 1] else 1
6167
edit[i][j] = min(edit[i - 1][j] + 1, edit[i][j - 1] + 1, edit[i - 1][j - 1] + cost)
6268

63-
return edit[-1][-1] # this is the same as edit[m][n]
69+
return edit[-1][-1] # this is the same as edit[length_a][length_b]

algorithms/dp/egg_drop.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@
2626

2727

2828
def egg_drop(n, k):
29+
"""
30+
Keyword arguments:
31+
n -- number of floors
32+
k -- number of eggs
33+
"""
2934
# A 2D table where entery eggFloor[i][j] will represent minimum
3035
# number of trials needed for i eggs and j floors.
31-
egg_floor = [[0 for x in range(k+1)] for x in range(n + 1)]
36+
egg_floor = [[0 for _ in range(k + 1)] for _ in range(n + 1)]
3237

3338
# We need one trial for one floor and 0 trials for 0 floors
3439
for i in range(1, n+1):

algorithms/dp/fib.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ def fib_recursive(n):
3535

3636
if n <= 1:
3737
return n
38-
else:
39-
return fib_recursive(n-1) + fib_recursive(n-2)
38+
return fib_recursive(n-1) + fib_recursive(n-2)
4039

4140
# print(fib_recursive(35)) # => 9227465 (slow)
4241

@@ -81,13 +80,13 @@ def fib_iter(n):
8180

8281
fib_1 = 0
8382
fib_2 = 1
84-
sum = 0
83+
res = 0
8584
if n <= 1:
8685
return n
8786
for _ in range(n-1):
88-
sum = fib_1 + fib_2
87+
res = fib_1 + fib_2
8988
fib_1 = fib_2
90-
fib_2 = sum
91-
return sum
89+
fib_2 = res
90+
return res
9291

9392
# print(fib_iter(100)) # => 354224848179261915075

algorithms/dp/hosoya_triangle.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,38 @@
1919
2020
"""
2121

22+
def hosoya(height, width):
23+
""" Calculates the hosoya triangle
2224
23-
def hosoya(n, m):
24-
if ((n == 0 and m == 0) or (n == 1 and m == 0) or
25-
(n == 1 and m == 1) or (n == 2 and m == 1)):
25+
height -- height of the triangle
26+
"""
27+
if (width == 0) and (height in (0,1)):
2628
return 1
27-
if n > m:
28-
return hosoya(n - 1, m) + hosoya(n - 2, m)
29-
elif m == n:
30-
return hosoya(n - 1, m - 1) + hosoya(n - 2, m - 2)
31-
else:
32-
return 0
33-
34-
35-
def print_hosoya(n):
36-
for i in range(n):
29+
if (width == 1) and (height in (1,2)):
30+
return 1
31+
if height > width:
32+
return hosoya(height - 1, width) + hosoya(height - 2, width)
33+
if width == height:
34+
return hosoya(height - 1, width - 1) + hosoya(height - 2, width - 2)
35+
return 0
36+
37+
def print_hosoya(height):
38+
"""Prints the hosoya triangle
39+
40+
height -- height of the triangle
41+
"""
42+
for i in range(height):
3743
for j in range(i + 1):
38-
print(hosoya(i, j), end=" ")
39-
print("\n", end="")
44+
print(hosoya(i, j) , end = " ")
45+
print ("\n", end = "")
4046

47+
def hosoya_testing(height):
48+
"""Test hosoya function
4149
42-
def hosoya_testing(n):
43-
x = []
44-
for i in range(n):
50+
height -- height of the triangle
51+
"""
52+
res = []
53+
for i in range(height):
4554
for j in range(i + 1):
46-
x.append(hosoya(i, j))
47-
return x
55+
res.append(hosoya(i, j))
56+
return res

algorithms/dp/int_divide.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
2-
Given positive integer n, find an algorithm to find the number of non-negative number division, or descomposition.
2+
Given positive integer decompose, find an algorithm to find the number of
3+
non-negative number division, or decomposition.
34
45
The complexity is O(n^2).
56
@@ -36,15 +37,19 @@
3637
"""
3738

3839

39-
def int_divide(n):
40-
arr = [[0 for i in range(n + 1)] for j in range(n + 1)]
40+
def int_divide(decompose):
41+
"""Find number of decompositions from `decompose`
42+
43+
decompose -- integer
44+
"""
45+
arr = [[0 for i in range(decompose + 1)] for j in range(decompose + 1)]
4146
arr[1][1] = 1
42-
for i in range(1, n + 1):
43-
for j in range(1, n + 1):
47+
for i in range(1, decompose + 1):
48+
for j in range(1, decompose + 1):
4449
if i < j:
4550
arr[i][j] = arr[i][i]
4651
elif i == j:
4752
arr[i][j] = 1 + arr[i][j - 1]
4853
else:
4954
arr[i][j] = arr[i][j - 1] + arr[i - j][j]
50-
return arr[n][n]
55+
return arr[decompose][decompose]

0 commit comments

Comments
 (0)