mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 18:49:26 +08:00
Set the Python file maximum line length to 88 characters (#2122)
* flake8 --max-line-length=88 * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
"""
|
||||
This is a pure Python implementation of Dynamic Programming solution to the fibonacci sequence problem.
|
||||
This is a pure Python implementation of Dynamic Programming solution to the fibonacci
|
||||
sequence problem.
|
||||
"""
|
||||
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
"""
|
||||
The number of partitions of a number n into at least k parts equals the number of partitions into exactly k parts
|
||||
plus the number of partitions into at least k-1 parts. Subtracting 1 from each part of a partition of n into k parts
|
||||
gives a partition of n-k into k parts. These two facts together are used for this algorithm.
|
||||
The number of partitions of a number n into at least k parts equals the number of
|
||||
partitions into exactly k parts plus the number of partitions into at least k-1 parts.
|
||||
Subtracting 1 from each part of a partition of n into k parts gives a partition of n-k
|
||||
into k parts. These two facts together are used for this algorithm.
|
||||
"""
|
||||
|
||||
|
||||
|
@ -12,7 +12,8 @@ def list_of_submasks(mask: int) -> List[int]:
|
||||
|
||||
"""
|
||||
Args:
|
||||
mask : number which shows mask ( always integer > 0, zero does not have any submasks )
|
||||
mask : number which shows mask ( always integer > 0, zero does not have any
|
||||
submasks )
|
||||
|
||||
Returns:
|
||||
all_submasks : the list of submasks of mask (mask s is called submask of mask
|
||||
|
@ -9,8 +9,8 @@ Note that only the integer weights 0-1 knapsack problem is solvable
|
||||
|
||||
def MF_knapsack(i, wt, val, j):
|
||||
"""
|
||||
This code involves the concept of memory functions. Here we solve the subproblems which are needed
|
||||
unlike the below example
|
||||
This code involves the concept of memory functions. Here we solve the subproblems
|
||||
which are needed unlike the below example
|
||||
F is a 2D array with -1s filled up
|
||||
"""
|
||||
global F # a global dp table for knapsack
|
||||
|
@ -1,6 +1,7 @@
|
||||
"""
|
||||
LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them.
|
||||
A subsequence is a sequence that appears in the same relative order, but not necessarily continuous.
|
||||
LCS Problem Statement: Given two sequences, find the length of longest subsequence
|
||||
present in both of them. A subsequence is a sequence that appears in the same relative
|
||||
order, but not necessarily continuous.
|
||||
Example:"abc", "abg" are subsequences of "abcdefgh".
|
||||
"""
|
||||
|
||||
|
@ -1,10 +1,12 @@
|
||||
"""
|
||||
Author : Yvonne
|
||||
|
||||
This is a pure Python implementation of Dynamic Programming solution to the longest_sub_array problem.
|
||||
This is a pure Python implementation of Dynamic Programming solution to the
|
||||
longest_sub_array problem.
|
||||
|
||||
The problem is :
|
||||
Given an array, to find the longest and continuous sub array and get the max sum of the sub array in the given array.
|
||||
Given an array, to find the longest and continuous sub array and get the max sum of the
|
||||
sub array in the given array.
|
||||
"""
|
||||
|
||||
|
||||
|
@ -13,8 +13,9 @@ pieces separately or not cutting it at all if the price of it is the maximum obt
|
||||
|
||||
def naive_cut_rod_recursive(n: int, prices: list):
|
||||
"""
|
||||
Solves the rod-cutting problem via naively without using the benefit of dynamic programming.
|
||||
The results is the same sub-problems are solved several times leading to an exponential runtime
|
||||
Solves the rod-cutting problem via naively without using the benefit of dynamic
|
||||
programming. The results is the same sub-problems are solved several times
|
||||
leading to an exponential runtime
|
||||
|
||||
Runtime: O(2^n)
|
||||
|
||||
@ -26,7 +27,8 @@ def naive_cut_rod_recursive(n: int, prices: list):
|
||||
|
||||
Returns
|
||||
-------
|
||||
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
|
||||
The maximum revenue obtainable for a rod of length n given the list of prices
|
||||
for each piece.
|
||||
|
||||
Examples
|
||||
--------
|
||||
@ -50,8 +52,9 @@ def naive_cut_rod_recursive(n: int, prices: list):
|
||||
|
||||
def top_down_cut_rod(n: int, prices: list):
|
||||
"""
|
||||
Constructs a top-down dynamic programming solution for the rod-cutting problem
|
||||
via memoization. This function serves as a wrapper for _top_down_cut_rod_recursive
|
||||
Constructs a top-down dynamic programming solution for the rod-cutting
|
||||
problem via memoization. This function serves as a wrapper for
|
||||
_top_down_cut_rod_recursive
|
||||
|
||||
Runtime: O(n^2)
|
||||
|
||||
@ -63,12 +66,13 @@ def top_down_cut_rod(n: int, prices: list):
|
||||
|
||||
Note
|
||||
----
|
||||
For convenience and because Python's lists using 0-indexing, length(max_rev) = n + 1,
|
||||
to accommodate for the revenue obtainable from a rod of length 0.
|
||||
For convenience and because Python's lists using 0-indexing, length(max_rev) =
|
||||
n + 1, to accommodate for the revenue obtainable from a rod of length 0.
|
||||
|
||||
Returns
|
||||
-------
|
||||
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
|
||||
The maximum revenue obtainable for a rod of length n given the list of prices
|
||||
for each piece.
|
||||
|
||||
Examples
|
||||
-------
|
||||
@ -99,7 +103,8 @@ def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list):
|
||||
|
||||
Returns
|
||||
-------
|
||||
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
|
||||
The maximum revenue obtainable for a rod of length n given the list of prices
|
||||
for each piece.
|
||||
"""
|
||||
if max_rev[n] >= 0:
|
||||
return max_rev[n]
|
||||
@ -144,7 +149,8 @@ def bottom_up_cut_rod(n: int, prices: list):
|
||||
"""
|
||||
_enforce_args(n, prices)
|
||||
|
||||
# length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of length 0.
|
||||
# length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of
|
||||
# length 0.
|
||||
max_rev = [float("-inf") for _ in range(n + 1)]
|
||||
max_rev[0] = 0
|
||||
|
||||
@ -167,7 +173,8 @@ def _enforce_args(n: int, prices: list):
|
||||
|
||||
Throws ValueError:
|
||||
|
||||
if n is negative or there are fewer items in the price list than the length of the rod
|
||||
if n is negative or there are fewer items in the price list than the length of
|
||||
the rod
|
||||
"""
|
||||
if n < 0:
|
||||
raise ValueError(f"n must be greater than or equal to 0. Got n = {n}")
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Python program to print all subset combinations of n element in given set of r element.
|
||||
# Print all subset combinations of n element in given set of r element.
|
||||
|
||||
|
||||
def combination_util(arr, n, r, index, data, i):
|
||||
|
@ -9,7 +9,8 @@ def isSumSubset(arr, arrLen, requiredSum):
|
||||
# initially no subsets can be formed hence False/0
|
||||
subset = [[False for i in range(requiredSum + 1)] for i in range(arrLen + 1)]
|
||||
|
||||
# for each arr value, a sum of zero(0) can be formed by not taking any element hence True/1
|
||||
# for each arr value, a sum of zero(0) can be formed by not taking any element
|
||||
# hence True/1
|
||||
for i in range(arrLen + 1):
|
||||
subset[i][0] = True
|
||||
|
||||
|
Reference in New Issue
Block a user