mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
Tighten up psf/black and flake8 (#2024)
* Tighten up psf/black and flake8 * Fix some tests * Fix some E741 * Fix some E741 * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -26,9 +26,9 @@ def factorial(num):
|
||||
|
||||
# factorial of num
|
||||
# uncomment the following to see how recalculations are avoided
|
||||
##result=[-1]*10
|
||||
##result[0]=result[1]=1
|
||||
##print(factorial(5))
|
||||
# result=[-1]*10
|
||||
# result[0]=result[1]=1
|
||||
# print(factorial(5))
|
||||
# print(factorial(3))
|
||||
# print(factorial(7))
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
"""
|
||||
Author : Syed Faizan (3rd Year Student IIIT Pune)
|
||||
Author : Syed Faizan (3rd Year Student IIIT Pune)
|
||||
github : faizan2700
|
||||
You are given a bitmask m and you want to efficiently iterate through all of
|
||||
its submasks. The mask s is submask of m if only bits that were included in
|
||||
@ -33,7 +33,7 @@ def list_of_submasks(mask: int) -> List[int]:
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: mask needs to be positive integer, your input 0
|
||||
|
||||
|
||||
"""
|
||||
|
||||
fmt = "mask needs to be positive integer, your input {}"
|
||||
|
@ -76,7 +76,7 @@ if __name__ == "__main__":
|
||||
expected_subseq = "GTAB"
|
||||
|
||||
ln, subseq = longest_common_subsequence(a, b)
|
||||
## print("len =", ln, ", sub-sequence =", subseq)
|
||||
print("len =", ln, ", sub-sequence =", subseq)
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
@ -1,11 +1,14 @@
|
||||
"""
|
||||
Author : Mehdi ALAOUI
|
||||
|
||||
This is a pure Python implementation of Dynamic Programming solution to the longest increasing subsequence of a given sequence.
|
||||
This is a pure Python implementation of Dynamic Programming solution to the longest
|
||||
increasing subsequence of a given sequence.
|
||||
|
||||
The problem is :
|
||||
Given an array, to find the longest and increasing sub-array in that given array and return it.
|
||||
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return [10, 22, 33, 41, 60, 80] as output
|
||||
Given an array, to find the longest and increasing sub-array in that given array and
|
||||
return it.
|
||||
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return
|
||||
[10, 22, 33, 41, 60, 80] as output
|
||||
"""
|
||||
from typing import List
|
||||
|
||||
@ -21,11 +24,13 @@ def longest_subsequence(array: List[int]) -> List[int]: # This function is recu
|
||||
[8]
|
||||
>>> longest_subsequence([1, 1, 1])
|
||||
[1, 1, 1]
|
||||
>>> longest_subsequence([])
|
||||
[]
|
||||
"""
|
||||
array_length = len(array)
|
||||
if (
|
||||
array_length <= 1
|
||||
): # If the array contains only one element, we return it (it's the stop condition of recursion)
|
||||
# If the array contains only one element, we return it (it's the stop condition of
|
||||
# recursion)
|
||||
if array_length <= 1:
|
||||
return array
|
||||
# Else
|
||||
pivot = array[0]
|
||||
|
@ -1,19 +1,19 @@
|
||||
#############################
|
||||
# Author: Aravind Kashyap
|
||||
# File: lis.py
|
||||
# comments: This programme outputs the Longest Strictly Increasing Subsequence in O(NLogN)
|
||||
# Where N is the Number of elements in the list
|
||||
# comments: This programme outputs the Longest Strictly Increasing Subsequence in
|
||||
# O(NLogN) Where N is the Number of elements in the list
|
||||
#############################
|
||||
from typing import List
|
||||
|
||||
|
||||
def CeilIndex(v, l, r, key):
|
||||
def CeilIndex(v, l, r, key): # noqa: E741
|
||||
while r - l > 1:
|
||||
m = (l + r) // 2
|
||||
if v[m] >= key:
|
||||
r = m
|
||||
else:
|
||||
l = m
|
||||
l = m # noqa: E741
|
||||
return r
|
||||
|
||||
|
||||
@ -23,7 +23,8 @@ def LongestIncreasingSubsequenceLength(v: List[int]) -> int:
|
||||
6
|
||||
>>> LongestIncreasingSubsequenceLength([])
|
||||
0
|
||||
>>> LongestIncreasingSubsequenceLength([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15])
|
||||
>>> LongestIncreasingSubsequenceLength([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3,
|
||||
... 11, 7, 15])
|
||||
6
|
||||
>>> LongestIncreasingSubsequenceLength([5, 4, 3, 2, 1])
|
||||
1
|
||||
|
@ -44,12 +44,12 @@ def max_sub_array(nums: List[int]) -> int:
|
||||
|
||||
>>> max_sub_array([-2, 1, -3, 4, -1, 2, 1, -5, 4])
|
||||
6
|
||||
|
||||
|
||||
An empty (sub)array has sum 0.
|
||||
>>> max_sub_array([])
|
||||
0
|
||||
|
||||
If all elements are negative, the largest subarray would be the empty array,
|
||||
|
||||
If all elements are negative, the largest subarray would be the empty array,
|
||||
having the sum 0.
|
||||
>>> max_sub_array([-1, -2, -3])
|
||||
0
|
||||
|
@ -23,7 +23,7 @@ def findMin(arr):
|
||||
dp[i][j] = dp[i][j] or dp[i - 1][j - arr[i - 1]]
|
||||
|
||||
for j in range(int(s / 2), -1, -1):
|
||||
if dp[n][j] == True:
|
||||
if dp[n][j] is True:
|
||||
diff = s - 2 * j
|
||||
break
|
||||
|
||||
|
@ -40,7 +40,7 @@ class Node:
|
||||
def print_binary_search_tree(root, key, i, j, parent, is_left):
|
||||
"""
|
||||
Recursive function to print a BST from a root table.
|
||||
|
||||
|
||||
>>> key = [3, 8, 9, 10, 17, 21]
|
||||
>>> root = [[0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 3], [0, 0, 2, 3, 3, 3], \
|
||||
[0, 0, 0, 3, 3, 3], [0, 0, 0, 0, 4, 5], [0, 0, 0, 0, 0, 5]]
|
||||
@ -73,7 +73,7 @@ def find_optimal_binary_search_tree(nodes):
|
||||
The dynamic programming algorithm below runs in O(n^2) time.
|
||||
Implemented from CLRS (Introduction to Algorithms) book.
|
||||
https://en.wikipedia.org/wiki/Introduction_to_Algorithms
|
||||
|
||||
|
||||
>>> find_optimal_binary_search_tree([Node(12, 8), Node(10, 34), Node(20, 50), \
|
||||
Node(42, 3), Node(25, 40), Node(37, 30)])
|
||||
Binary search tree nodes:
|
||||
@ -104,14 +104,15 @@ def find_optimal_binary_search_tree(nodes):
|
||||
# This 2D array stores the overall tree cost (which's as minimized as possible);
|
||||
# for a single key, cost is equal to frequency of the key.
|
||||
dp = [[freqs[i] if i == j else 0 for j in range(n)] for i in range(n)]
|
||||
# sum[i][j] stores the sum of key frequencies between i and j inclusive in nodes array
|
||||
# sum[i][j] stores the sum of key frequencies between i and j inclusive in nodes
|
||||
# array
|
||||
sum = [[freqs[i] if i == j else 0 for j in range(n)] for i in range(n)]
|
||||
# stores tree roots that will be used later for constructing binary search tree
|
||||
root = [[i if i == j else 0 for j in range(n)] for i in range(n)]
|
||||
|
||||
for l in range(2, n + 1): # l is an interval length
|
||||
for i in range(n - l + 1):
|
||||
j = i + l - 1
|
||||
for interval_length in range(2, n + 1):
|
||||
for i in range(n - interval_length + 1):
|
||||
j = i + interval_length - 1
|
||||
|
||||
dp[i][j] = sys.maxsize # set the value to "infinity"
|
||||
sum[i][j] = sum[i][j - 1] + freqs[j]
|
||||
|
@ -1,12 +1,15 @@
|
||||
# Python program to print all subset combinations of n element in given set of r element.
|
||||
# arr[] ---> Input Array
|
||||
# data[] ---> Temporary array to store current combination
|
||||
# start & end ---> Staring and Ending indexes in arr[]
|
||||
# index ---> Current index in data[]
|
||||
# r ---> Size of a combination to be printed
|
||||
|
||||
|
||||
def combination_util(arr, n, r, index, data, i):
|
||||
# Current combination is ready to be printed,
|
||||
# print it
|
||||
"""
|
||||
Current combination is ready to be printed, print it
|
||||
arr[] ---> Input Array
|
||||
data[] ---> Temporary array to store current combination
|
||||
start & end ---> Staring and Ending indexes in arr[]
|
||||
index ---> Current index in data[]
|
||||
r ---> Size of a combination to be printed
|
||||
"""
|
||||
if index == r:
|
||||
for j in range(r):
|
||||
print(data[j], end=" ")
|
||||
|
Reference in New Issue
Block a user