Skip to content

Commit 15e5292

Browse files
authored
Fix flake8 issues (#837)
1 parent 8149be5 commit 15e5292

Some content is hidden

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

44 files changed

+548
-432
lines changed

algorithms/compression/elias.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
"""
2-
Elias γ code or Elias gamma code is a universal code encoding positive integers.
3-
It is used most commonly when coding integers whose upper-bound cannot be determined beforehand.
4-
Elias δ code or Elias delta code is a universal code encoding the positive integers,
2+
Elias γ code or Elias gamma code is a universal code
3+
encoding positive integers.
4+
It is used most commonly when coding integers whose
5+
upper-bound cannot be determined beforehand.
6+
Elias δ code or Elias delta code is a universal code
7+
encoding the positive integers,
58
that includes Elias γ code when calculating.
69
Both were developed by Peter Elias.
710
811
"""
9-
from math import log,ceil
12+
from math import log
1013

11-
log2 = lambda x: log(x,2)
14+
log2 = lambda x: log(x, 2)
1215

1316
# Calculates the binary number
14-
def binary(x,l=1):
17+
def binary(x, l=1):
1518
fmt = '{0:0%db}' % l
1619
return fmt.format(x)
1720

@@ -21,20 +24,21 @@ def unary(x):
2124

2225
def elias_generic(lencoding, x):
2326
"""
24-
The compressed data is calculated in two parts.
25-
The first part is the unary number of 1 + ⌊log2(x)⌋.
27+
The compressed data is calculated in two parts.
28+
The first part is the unary number of 1 + ⌊log2(x)⌋.
2629
The second part is the binary number of x - 2^(⌊log2(x)⌋).
2730
For the final result we add these two parts.
2831
"""
29-
if x == 0: return '0'
32+
if x == 0:
33+
return '0'
3034

3135
first_part = 1 + int(log2(x))
3236

3337
a = x - 2**(int(log2(x)))
3438

3539
k = int(log2(x))
3640

37-
return lencoding(first_part) + binary(a,k)
41+
return lencoding(first_part) + binary(a, k)
3842

3943
def elias_gamma(x):
4044
"""
@@ -46,4 +50,4 @@ def elias_delta(x):
4650
"""
4751
For the first part we put the elias_g of the number.
4852
"""
49-
return elias_generic(elias_gamma,x)
53+
return elias_generic(elias_gamma, x)

algorithms/dp/coin_change.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
"""
22
Problem
3-
Given a value n, if we want to make change for N cents, and we have infinite supply of each of
4-
coins = {S1, S2, .. , Sm} valued coins, how many ways can we make the change?
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?
57
The order of coins doesn't matter.
6-
For example, for n = 4 and coins = [1, 2, 3], there are four solutions:
7-
[1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3].
8-
So output should be 4.
8+
For example, for n = 4 and coins = [1, 2, 3], there are
9+
four solutions:
10+
[1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3].
11+
So output should be 4.
912
10-
For n = 10 and coins = [2, 5, 3, 6], there are five solutions:
11-
[2, 2, 2, 2, 2], [2, 2, 3, 3], [2, 2, 6], [2, 3, 5] and [5, 5].
13+
For n = 10 and coins = [2, 5, 3, 6], there are five solutions:
14+
[2, 2, 2, 2, 2], [2, 2, 3, 3], [2, 2, 6], [2, 3, 5] and [5, 5].
1215
So the output should be 5.
1316
1417
Time complexity: O(n * m) where n is the value and m is the number of coins
1518
Space complexity: O(n)
1619
"""
1720

21+
1822
def count(coins, n):
1923
# initialize dp array and set base case as 1
2024
dp = [1] + [0] * n
21-
25+
2226
# fill dp in a bottom up manner
2327
for coin in coins:
2428
for i in range(coin, n+1):
2529
dp[i] += dp[i-coin]
2630

2731
return dp[n]
28-

algorithms/dp/edit_distance.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
1-
"""The edit distance between two words is the minimum number of letter insertions,
2-
letter deletions, and letter substitutions required to transform one word into another.
1+
"""The edit distance between two words is the minimum number
2+
of letter insertions, letter deletions, and letter substitutions
3+
required to transform one word into another.
34
4-
For example, the edit distance between FOOD and MONEY is at most four:
5+
For example, the edit distance between FOOD and MONEY is at
6+
most four:
57
68
FOOD -> MOOD -> MOND -> MONED -> MONEY
79
8-
Given two words A and B, find the minimum number of operations required to transform one string into the other.
10+
Given two words A and B, find the minimum number of operations
11+
required to transform one string into the other.
912
In other words, find the edit distance between A and B.
1013
1114
Thought process:
1215
13-
Let edit(i, j) denote the edit distance between the prefixes A[1..i] and B[1..j].
16+
Let edit(i, j) denote the edit distance between
17+
the prefixes A[1..i] and B[1..j].
1418
1519
Then, the function satifies the following recurrence:
1620
1721
edit(i, j) = i if j = 0
1822
j if i = 0
19-
min(edit(i-1, j) + 1,
23+
min(edit(i-1, j) + 1,
2024
edit(i, j-1), + 1,
2125
edit(i-1, j-1) + cost) otherwise
2226
23-
There are two base cases, both of which occur when one string is empty and the other is not.
24-
1. To convert an empty string A into a string B of length n, perform n insertions.
25-
2. To convert a string A of length m into an empty string B, perform m deletions.
27+
There are two base cases, both of which occur when one string is empty
28+
and the other is not.
29+
1. To convert an empty string A into a string B of length n,
30+
perform n insertions.
31+
2. To convert a string A of length m into an empty string B,
32+
perform m deletions.
2633
2734
Here, the cost is 1 if a substitution is required,
28-
or 0 if both chars in words A and B are the same at indexes i and j, respectively.
35+
or 0 if both chars in words A and B are the same at
36+
indexes i and j, respectively.
2937
3038
To find the edit distance between two words A and B,
31-
we need to find edit(m, n), where m is the length of A and n is the length of B.
39+
we need to find edit(m, n), where m is the length of A and n
40+
is the length of B.
3241
"""
3342

3443

algorithms/dp/egg_drop.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
"""
2-
You are given K eggs, and you have access to a building with N floors
3-
from 1 to N. Each egg is identical in function, and if an egg breaks,
4-
you cannot drop it again. You know that there exists a floor F with
5-
0 <= F <= N such that any egg dropped at a floor higher than F will
6-
break, and any egg dropped at or below floor F will not break.
7-
Each move, you may take an egg (if you have an unbroken one) and drop
8-
it from any floor X (with 1 <= X <= N). Your goal is to know with
9-
certainty what the value of F is. What is the minimum number of moves
10-
that you need to know with certainty what F is, regardless of the
2+
You are given K eggs, and you have access to a building with N floors
3+
from 1 to N. Each egg is identical in function, and if an egg breaks,
4+
you cannot drop it again. You know that there exists a floor F with
5+
0 <= F <= N such that any egg dropped at a floor higher than F will
6+
break, and any egg dropped at or below floor F will not break.
7+
Each move, you may take an egg (if you have an unbroken one) and drop
8+
it from any floor X (with 1 <= X <= N). Your goal is to know with
9+
certainty what the value of F is. What is the minimum number of moves
10+
that you need to know with certainty what F is, regardless of the
1111
initial value of F?
1212
1313
Example:
1414
Input: K = 1, N = 2
1515
Output: 2
16-
Explanation:
16+
Explanation:
1717
Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
18-
Otherwise, drop the egg from floor 2. If it breaks, we know with
18+
Otherwise, drop the egg from floor 2. If it breaks, we know with
1919
certainty that F = 1.
2020
If it didn't break, then we know with certainty F = 2.
2121
Hence, we needed 2 moves in the worst case to know what F is with certainty.
@@ -24,20 +24,21 @@
2424
# A Dynamic Programming based Python Program for the Egg Dropping Puzzle
2525
INT_MAX = 32767
2626

27+
2728
def egg_drop(n, k):
2829
# A 2D table where entery eggFloor[i][j] will represent minimum
2930
# number of trials needed for i eggs and j floors.
30-
egg_floor = [[0 for x in range(k+1)] for x in range(n+1)]
31-
31+
egg_floor = [[0 for x in range(k+1)] for x in range(n + 1)]
32+
3233
# We need one trial for one floor and 0 trials for 0 floors
3334
for i in range(1, n+1):
3435
egg_floor[i][1] = 1
3536
egg_floor[i][0] = 0
36-
37+
3738
# We always need j trials for one egg and j floors.
3839
for j in range(1, k+1):
3940
egg_floor[1][j] = j
40-
41+
4142
# Fill rest of the entries in table using optimal substructure
4243
# property
4344
for i in range(2, n+1):
@@ -47,6 +48,6 @@ def egg_drop(n, k):
4748
res = 1 + max(egg_floor[i-1][x-1], egg_floor[i][j-x])
4849
if res < egg_floor[i][j]:
4950
egg_floor[i][j] = res
50-
51+
5152
# eggFloor[n][k] holds the result
5253
return egg_floor[n][k]

algorithms/dp/fib.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
'''
2-
In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence,
3-
such that each number is the sum of the two preceding ones, starting from 0 and 1.
2+
In mathematics, the Fibonacci numbers, commonly denoted Fn,
3+
form a sequence, called the Fibonacci sequence,
4+
such that each number is the sum of the two preceding ones,
5+
starting from 0 and 1.
46
That is,
57
F0=0 , F1=1
68
and
79
Fn= F(n-1) + F(n-2)
810
The Fibonacci numbers are the numbers in the following integer sequence.
911
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….
1012
11-
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
13+
In mathematical terms, the sequence Fn of Fibonacci numbers is
14+
defined by the recurrence relation
1215
1316
Here, given a number n, print n-th Fibonacci Number.
1417
'''
18+
19+
1520
def fib_recursive(n):
1621
"""[summary]
1722
Computes the n-th fibonacci number recursive.
@@ -20,7 +25,7 @@ def fib_recursive(n):
2025
2126
Arguments:
2227
n {[int]} -- [description]
23-
28+
2429
Returns:
2530
[int] -- [description]
2631
"""
@@ -35,15 +40,16 @@ def fib_recursive(n):
3540

3641
# print(fib_recursive(35)) # => 9227465 (slow)
3742

43+
3844
def fib_list(n):
3945
"""[summary]
4046
This algorithm computes the n-th fibbonacci number
4147
very quick. approximate O(n)
4248
The algorithm use dynamic programming.
43-
49+
4450
Arguments:
4551
n {[int]} -- [description]
46-
52+
4753
Returns:
4854
[int] -- [description]
4955
"""
@@ -58,13 +64,14 @@ def fib_list(n):
5864

5965
# print(fib_list(100)) # => 354224848179261915075
6066

67+
6168
def fib_iter(n):
6269
"""[summary]
6370
Works iterative approximate O(n)
6471
6572
Arguments:
6673
n {[int]} -- [description]
67-
74+
6875
Returns:
6976
[int] -- [description]
7077
"""

algorithms/dp/hosoya_triangle.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,40 @@
88
99
For example:
1010
printHosoya( 6 ) would return:
11-
1
12-
1 1
13-
2 1 2
14-
3 2 2 3
15-
5 3 4 3 5
11+
1
12+
1 1
13+
2 1 2
14+
3 2 2 3
15+
5 3 4 3 5
1616
8 5 6 6 5 8
1717
1818
The complexity is O(n^3).
1919
2020
"""
2121

2222

23-
def hosoya(n, m):
23+
def hosoya(n, m):
2424
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+
(n == 1 and m == 1) or (n == 2 and m == 1)):
2626
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:
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:
3232
return 0
33-
34-
def print_hosoya(n):
35-
for i in range(n):
36-
for j in range(i + 1):
37-
print(hosoya(i, j) , end = " ")
38-
print ("\n", end = "")
33+
34+
35+
def print_hosoya(n):
36+
for i in range(n):
37+
for j in range(i + 1):
38+
print(hosoya(i, j), end=" ")
39+
print("\n", end="")
40+
3941

4042
def hosoya_testing(n):
4143
x = []
42-
for i in range(n):
43-
for j in range(i + 1):
44+
for i in range(n):
45+
for j in range(i + 1):
4446
x.append(hosoya(i, j))
4547
return x

0 commit comments

Comments
 (0)