Skip to content

Commit

Permalink
Fix flake8 issues (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankit167 committed Mar 2, 2022
1 parent 8149be5 commit 15e5292
Show file tree
Hide file tree
Showing 44 changed files with 548 additions and 432 deletions.
26 changes: 15 additions & 11 deletions algorithms/compression/elias.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
"""
Elias γ code or Elias gamma code is a universal code encoding positive integers.
It is used most commonly when coding integers whose upper-bound cannot be determined beforehand.
Elias δ code or Elias delta code is a universal code encoding the positive integers,
Elias γ code or Elias gamma code is a universal code
encoding positive integers.
It is used most commonly when coding integers whose
upper-bound cannot be determined beforehand.
Elias δ code or Elias delta code is a universal code
encoding the positive integers,
that includes Elias γ code when calculating.
Both were developed by Peter Elias.
"""
from math import log,ceil
from math import log

log2 = lambda x: log(x,2)
log2 = lambda x: log(x, 2)

# Calculates the binary number
def binary(x,l=1):
def binary(x, l=1):
fmt = '{0:0%db}' % l
return fmt.format(x)

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

def elias_generic(lencoding, x):
"""
The compressed data is calculated in two parts.
The first part is the unary number of 1 + ⌊log2(x)⌋.
The compressed data is calculated in two parts.
The first part is the unary number of 1 + ⌊log2(x)⌋.
The second part is the binary number of x - 2^(⌊log2(x)⌋).
For the final result we add these two parts.
"""
if x == 0: return '0'
if x == 0:
return '0'

first_part = 1 + int(log2(x))

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

k = int(log2(x))

return lencoding(first_part) + binary(a,k)
return lencoding(first_part) + binary(a, k)

def elias_gamma(x):
"""
Expand All @@ -46,4 +50,4 @@ def elias_delta(x):
"""
For the first part we put the elias_g of the number.
"""
return elias_generic(elias_gamma,x)
return elias_generic(elias_gamma, x)
21 changes: 12 additions & 9 deletions algorithms/dp/coin_change.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
"""
Problem
Given a value n, if we want to make change for N cents, and we have infinite supply of each of
coins = {S1, S2, .. , Sm} valued coins, how many ways can we make the change?
Given a value n, if we want to make change for N cents,
and we have infinite supply of each of
coins = {S1, S2, .. , Sm} valued coins, how many ways
can we make the change?
The order of coins doesn't matter.
For example, for n = 4 and coins = [1, 2, 3], there are four solutions:
[1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3].
So output should be 4.
For example, for n = 4 and coins = [1, 2, 3], there are
four solutions:
[1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3].
So output should be 4.
For n = 10 and coins = [2, 5, 3, 6], there are five solutions:
[2, 2, 2, 2, 2], [2, 2, 3, 3], [2, 2, 6], [2, 3, 5] and [5, 5].
For n = 10 and coins = [2, 5, 3, 6], there are five solutions:
[2, 2, 2, 2, 2], [2, 2, 3, 3], [2, 2, 6], [2, 3, 5] and [5, 5].
So the output should be 5.
Time complexity: O(n * m) where n is the value and m is the number of coins
Space complexity: O(n)
"""


def count(coins, n):
# initialize dp array and set base case as 1
dp = [1] + [0] * n

# fill dp in a bottom up manner
for coin in coins:
for i in range(coin, n+1):
dp[i] += dp[i-coin]

return dp[n]

31 changes: 20 additions & 11 deletions algorithms/dp/edit_distance.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
"""The edit distance between two words is the minimum number of letter insertions,
letter deletions, and letter substitutions required to transform one word into another.
"""The edit distance between two words is the minimum number
of letter insertions, letter deletions, and letter substitutions
required to transform one word into another.
For example, the edit distance between FOOD and MONEY is at most four:
For example, the edit distance between FOOD and MONEY is at
most four:
FOOD -> MOOD -> MOND -> MONED -> MONEY
Given two words A and B, find the minimum number of operations required to transform one string into the other.
Given two words A and B, find the minimum number of operations
required to transform one string into the other.
In other words, find the edit distance between A and B.
Thought process:
Let edit(i, j) denote the edit distance between the prefixes A[1..i] and B[1..j].
Let edit(i, j) denote the edit distance between
the prefixes A[1..i] and B[1..j].
Then, the function satifies the following recurrence:
edit(i, j) = i if j = 0
j if i = 0
min(edit(i-1, j) + 1,
min(edit(i-1, j) + 1,
edit(i, j-1), + 1,
edit(i-1, j-1) + cost) otherwise
There are two base cases, both of which occur when one string is empty and the other is not.
1. To convert an empty string A into a string B of length n, perform n insertions.
2. To convert a string A of length m into an empty string B, perform m deletions.
There are two base cases, both of which occur when one string is empty
and the other is not.
1. To convert an empty string A into a string B of length n,
perform n insertions.
2. To convert a string A of length m into an empty string B,
perform m deletions.
Here, the cost is 1 if a substitution is required,
or 0 if both chars in words A and B are the same at indexes i and j, respectively.
or 0 if both chars in words A and B are the same at
indexes i and j, respectively.
To find the edit distance between two words A and B,
we need to find edit(m, n), where m is the length of A and n is the length of B.
we need to find edit(m, n), where m is the length of A and n
is the length of B.
"""


Expand Down
33 changes: 17 additions & 16 deletions algorithms/dp/egg_drop.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
"""
You are given K eggs, and you have access to a building with N floors
from 1 to N. Each egg is identical in function, and if an egg breaks,
you cannot drop it again. You know that there exists a floor F with
0 <= F <= N such that any egg dropped at a floor higher than F will
break, and any egg dropped at or below floor F will not break.
Each move, you may take an egg (if you have an unbroken one) and drop
it from any floor X (with 1 <= X <= N). Your goal is to know with
certainty what the value of F is. What is the minimum number of moves
that you need to know with certainty what F is, regardless of the
You are given K eggs, and you have access to a building with N floors
from 1 to N. Each egg is identical in function, and if an egg breaks,
you cannot drop it again. You know that there exists a floor F with
0 <= F <= N such that any egg dropped at a floor higher than F will
break, and any egg dropped at or below floor F will not break.
Each move, you may take an egg (if you have an unbroken one) and drop
it from any floor X (with 1 <= X <= N). Your goal is to know with
certainty what the value of F is. What is the minimum number of moves
that you need to know with certainty what F is, regardless of the
initial value of F?
Example:
Input: K = 1, N = 2
Output: 2
Explanation:
Explanation:
Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
Otherwise, drop the egg from floor 2. If it breaks, we know with
Otherwise, drop the egg from floor 2. If it breaks, we know with
certainty that F = 1.
If it didn't break, then we know with certainty F = 2.
Hence, we needed 2 moves in the worst case to know what F is with certainty.
Expand All @@ -24,20 +24,21 @@
# A Dynamic Programming based Python Program for the Egg Dropping Puzzle
INT_MAX = 32767


def egg_drop(n, k):
# A 2D table where entery eggFloor[i][j] will represent minimum
# number of trials needed for i eggs and j floors.
egg_floor = [[0 for x in range(k+1)] for x in range(n+1)]
egg_floor = [[0 for x in range(k+1)] for x in range(n + 1)]

# We need one trial for one floor and 0 trials for 0 floors
for i in range(1, n+1):
egg_floor[i][1] = 1
egg_floor[i][0] = 0

# We always need j trials for one egg and j floors.
for j in range(1, k+1):
egg_floor[1][j] = j

# Fill rest of the entries in table using optimal substructure
# property
for i in range(2, n+1):
Expand All @@ -47,6 +48,6 @@ def egg_drop(n, k):
res = 1 + max(egg_floor[i-1][x-1], egg_floor[i][j-x])
if res < egg_floor[i][j]:
egg_floor[i][j] = res

# eggFloor[n][k] holds the result
return egg_floor[n][k]
21 changes: 14 additions & 7 deletions algorithms/dp/fib.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
'''
In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence,
such that each number is the sum of the two preceding ones, starting from 0 and 1.
In mathematics, the Fibonacci numbers, commonly denoted Fn,
form a sequence, called the Fibonacci sequence,
such that each number is the sum of the two preceding ones,
starting from 0 and 1.
That is,
F0=0 , F1=1
and
Fn= F(n-1) + F(n-2)
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
In mathematical terms, the sequence Fn of Fibonacci numbers is
defined by the recurrence relation
Here, given a number n, print n-th Fibonacci Number.
'''


def fib_recursive(n):
"""[summary]
Computes the n-th fibonacci number recursive.
Expand All @@ -20,7 +25,7 @@ def fib_recursive(n):
Arguments:
n {[int]} -- [description]
Returns:
[int] -- [description]
"""
Expand All @@ -35,15 +40,16 @@ def fib_recursive(n):

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


def fib_list(n):
"""[summary]
This algorithm computes the n-th fibbonacci number
very quick. approximate O(n)
The algorithm use dynamic programming.
Arguments:
n {[int]} -- [description]
Returns:
[int] -- [description]
"""
Expand All @@ -58,13 +64,14 @@ def fib_list(n):

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


def fib_iter(n):
"""[summary]
Works iterative approximate O(n)
Arguments:
n {[int]} -- [description]
Returns:
[int] -- [description]
"""
Expand Down
42 changes: 22 additions & 20 deletions algorithms/dp/hosoya_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,40 @@
For example:
printHosoya( 6 ) would return:
1
1 1
2 1 2
3 2 2 3
5 3 4 3 5
1
1 1
2 1 2
3 2 2 3
5 3 4 3 5
8 5 6 6 5 8
The complexity is O(n^3).
"""


def hosoya(n, m):
def hosoya(n, m):
if ((n == 0 and m == 0) or (n == 1 and m == 0) or
(n == 1 and m == 1) or (n == 2 and m == 1)):
(n == 1 and m == 1) or (n == 2 and m == 1)):
return 1
if n > m:
return hosoya(n - 1, m) + hosoya(n - 2, m)
elif m == n:
return hosoya(n - 1, m - 1) + hosoya(n - 2, m - 2)
else:
if n > m:
return hosoya(n - 1, m) + hosoya(n - 2, m)
elif m == n:
return hosoya(n - 1, m - 1) + hosoya(n - 2, m - 2)
else:
return 0

def print_hosoya(n):
for i in range(n):
for j in range(i + 1):
print(hosoya(i, j) , end = " ")
print ("\n", end = "")


def print_hosoya(n):
for i in range(n):
for j in range(i + 1):
print(hosoya(i, j), end=" ")
print("\n", end="")


def hosoya_testing(n):
x = []
for i in range(n):
for j in range(i + 1):
for i in range(n):
for j in range(i + 1):
x.append(hosoya(i, j))
return x

0 comments on commit 15e5292

Please sign in to comment.