mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-21 05:07:35 +00:00
Fix indentation contains tabs (flake8 E101,W191) (#1573)
This commit is contained in:
parent
dbaedd4ed7
commit
b838f1042c
@ -5,7 +5,7 @@ before_install: pip install --upgrade pip setuptools
|
|||||||
install: pip install -r requirements.txt
|
install: pip install -r requirements.txt
|
||||||
before_script:
|
before_script:
|
||||||
- black --check . || true
|
- black --check . || true
|
||||||
- flake8 . --count --select=E9,F4,F63,F7,F82 --show-source --statistics
|
- flake8 . --count --select=E101,E9,F4,F63,F7,F82,W191 --show-source --statistics
|
||||||
script:
|
script:
|
||||||
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
|
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
|
||||||
- mypy --ignore-missing-imports .
|
- mypy --ignore-missing-imports .
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
"""
|
"""
|
||||||
In this problem, we want to determine all possible combinations of k
|
In this problem, we want to determine all possible combinations of k
|
||||||
numbers out of 1 ... n. We use backtracking to solve this problem.
|
numbers out of 1 ... n. We use backtracking to solve this problem.
|
||||||
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!)))
|
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!)))
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
"""
|
"""
|
||||||
In this problem, we want to determine all possible permutations
|
In this problem, we want to determine all possible permutations
|
||||||
of the given sequence. We use backtracking to solve this problem.
|
of the given sequence. We use backtracking to solve this problem.
|
||||||
|
|
||||||
Time complexity: O(n! * n),
|
Time complexity: O(n! * n),
|
||||||
where n denotes the length of the given sequence.
|
where n denotes the length of the given sequence.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@ -13,10 +13,10 @@ def generate_all_permutations(sequence):
|
|||||||
|
|
||||||
def create_state_space_tree(sequence, current_sequence, index, index_used):
|
def create_state_space_tree(sequence, current_sequence, index, index_used):
|
||||||
"""
|
"""
|
||||||
Creates a state space tree to iterate through each branch using DFS.
|
Creates a state space tree to iterate through each branch using DFS.
|
||||||
We know that each state has exactly len(sequence) - index children.
|
We know that each state has exactly len(sequence) - index children.
|
||||||
It terminates when it reaches the end of the given sequence.
|
It terminates when it reaches the end of the given sequence.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if index == len(sequence):
|
if index == len(sequence):
|
||||||
print(current_sequence)
|
print(current_sequence)
|
||||||
@ -32,7 +32,7 @@ def create_state_space_tree(sequence, current_sequence, index, index_used):
|
|||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
remove the comment to take an input from the user
|
remove the comment to take an input from the user
|
||||||
|
|
||||||
print("Enter the elements")
|
print("Enter the elements")
|
||||||
sequence = list(map(int, input().split()))
|
sequence = list(map(int, input().split()))
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
"""
|
"""
|
||||||
In this problem, we want to determine all possible subsequences
|
In this problem, we want to determine all possible subsequences
|
||||||
of the given sequence. We use backtracking to solve this problem.
|
of the given sequence. We use backtracking to solve this problem.
|
||||||
|
|
||||||
Time complexity: O(2^n),
|
Time complexity: O(2^n),
|
||||||
where n denotes the length of the given sequence.
|
where n denotes the length of the given sequence.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@ -13,10 +13,10 @@ def generate_all_subsequences(sequence):
|
|||||||
|
|
||||||
def create_state_space_tree(sequence, current_subsequence, index):
|
def create_state_space_tree(sequence, current_subsequence, index):
|
||||||
"""
|
"""
|
||||||
Creates a state space tree to iterate through each branch using DFS.
|
Creates a state space tree to iterate through each branch using DFS.
|
||||||
We know that each state has exactly two children.
|
We know that each state has exactly two children.
|
||||||
It terminates when it reaches the end of the given sequence.
|
It terminates when it reaches the end of the given sequence.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if index == len(sequence):
|
if index == len(sequence):
|
||||||
print(current_subsequence)
|
print(current_subsequence)
|
||||||
@ -29,7 +29,7 @@ def create_state_space_tree(sequence, current_subsequence, index):
|
|||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
remove the comment to take an input from the user
|
remove the comment to take an input from the user
|
||||||
|
|
||||||
print("Enter the elements")
|
print("Enter the elements")
|
||||||
sequence = list(map(int, input().split()))
|
sequence = list(map(int, input().split()))
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
"""
|
"""
|
||||||
The sum-of-subsetsproblem states that a set of non-negative integers, and a value M,
|
The sum-of-subsetsproblem states that a set of non-negative integers, and a value M,
|
||||||
determine all possible subsets of the given set whose summation sum equal to given M.
|
determine all possible subsets of the given set whose summation sum equal to given M.
|
||||||
|
|
||||||
Summation of the chosen numbers must be equal to given number M and one number can
|
Summation of the chosen numbers must be equal to given number M and one number can
|
||||||
be used only once.
|
be used only once.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@ -18,12 +18,12 @@ def generate_sum_of_subsets_soln(nums, max_sum):
|
|||||||
|
|
||||||
def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum):
|
def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum):
|
||||||
"""
|
"""
|
||||||
Creates a state space tree to iterate through each branch using DFS.
|
Creates a state space tree to iterate through each branch using DFS.
|
||||||
It terminates the branching of a node when any of the two conditions
|
It terminates the branching of a node when any of the two conditions
|
||||||
given below satisfy.
|
given below satisfy.
|
||||||
This algorithm follows depth-fist-search and backtracks when the node is not branchable.
|
This algorithm follows depth-fist-search and backtracks when the node is not branchable.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum:
|
if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum:
|
||||||
return
|
return
|
||||||
if sum(path) == max_sum:
|
if sum(path) == max_sum:
|
||||||
@ -41,7 +41,7 @@ def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nu
|
|||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
remove the comment to take an input from the user
|
remove the comment to take an input from the user
|
||||||
|
|
||||||
print("Enter the elements")
|
print("Enter the elements")
|
||||||
nums = list(map(int, input().split()))
|
nums = list(map(int, input().split()))
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
def compare_string(string1, string2):
|
def compare_string(string1, string2):
|
||||||
"""
|
"""
|
||||||
>>> compare_string('0010','0110')
|
>>> compare_string('0010','0110')
|
||||||
'0_10'
|
'0_10'
|
||||||
|
|
||||||
>>> compare_string('0110','1101')
|
>>> compare_string('0110','1101')
|
||||||
-1
|
-1
|
||||||
"""
|
"""
|
||||||
l1 = list(string1)
|
l1 = list(string1)
|
||||||
l2 = list(string2)
|
l2 = list(string2)
|
||||||
count = 0
|
count = 0
|
||||||
@ -21,9 +21,9 @@ def compare_string(string1, string2):
|
|||||||
|
|
||||||
def check(binary):
|
def check(binary):
|
||||||
"""
|
"""
|
||||||
>>> check(['0.00.01.5'])
|
>>> check(['0.00.01.5'])
|
||||||
['0.00.01.5']
|
['0.00.01.5']
|
||||||
"""
|
"""
|
||||||
pi = []
|
pi = []
|
||||||
while 1:
|
while 1:
|
||||||
check1 = ["$"] * len(binary)
|
check1 = ["$"] * len(binary)
|
||||||
@ -45,9 +45,9 @@ def check(binary):
|
|||||||
|
|
||||||
def decimal_to_binary(no_of_variable, minterms):
|
def decimal_to_binary(no_of_variable, minterms):
|
||||||
"""
|
"""
|
||||||
>>> decimal_to_binary(3,[1.5])
|
>>> decimal_to_binary(3,[1.5])
|
||||||
['0.00.01.5']
|
['0.00.01.5']
|
||||||
"""
|
"""
|
||||||
temp = []
|
temp = []
|
||||||
s = ""
|
s = ""
|
||||||
for m in minterms:
|
for m in minterms:
|
||||||
@ -61,12 +61,12 @@ def decimal_to_binary(no_of_variable, minterms):
|
|||||||
|
|
||||||
def is_for_table(string1, string2, count):
|
def is_for_table(string1, string2, count):
|
||||||
"""
|
"""
|
||||||
>>> is_for_table('__1','011',2)
|
>>> is_for_table('__1','011',2)
|
||||||
True
|
True
|
||||||
|
|
||||||
>>> is_for_table('01_','001',1)
|
>>> is_for_table('01_','001',1)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
l1 = list(string1)
|
l1 = list(string1)
|
||||||
l2 = list(string2)
|
l2 = list(string2)
|
||||||
count_n = 0
|
count_n = 0
|
||||||
@ -81,12 +81,12 @@ def is_for_table(string1, string2, count):
|
|||||||
|
|
||||||
def selection(chart, prime_implicants):
|
def selection(chart, prime_implicants):
|
||||||
"""
|
"""
|
||||||
>>> selection([[1]],['0.00.01.5'])
|
>>> selection([[1]],['0.00.01.5'])
|
||||||
['0.00.01.5']
|
['0.00.01.5']
|
||||||
|
|
||||||
>>> selection([[1]],['0.00.01.5'])
|
>>> selection([[1]],['0.00.01.5'])
|
||||||
['0.00.01.5']
|
['0.00.01.5']
|
||||||
"""
|
"""
|
||||||
temp = []
|
temp = []
|
||||||
select = [0] * len(chart)
|
select = [0] * len(chart)
|
||||||
for i in range(len(chart[0])):
|
for i in range(len(chart[0])):
|
||||||
@ -128,9 +128,9 @@ def selection(chart, prime_implicants):
|
|||||||
|
|
||||||
def prime_implicant_chart(prime_implicants, binary):
|
def prime_implicant_chart(prime_implicants, binary):
|
||||||
"""
|
"""
|
||||||
>>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5'])
|
>>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5'])
|
||||||
[[1]]
|
[[1]]
|
||||||
"""
|
"""
|
||||||
chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))]
|
chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))]
|
||||||
for i in range(len(prime_implicants)):
|
for i in range(len(prime_implicants)):
|
||||||
count = prime_implicants[i].count("_")
|
count = prime_implicants[i].count("_")
|
||||||
|
@ -1,40 +1,40 @@
|
|||||||
"""
|
"""
|
||||||
author: Christian Bender
|
author: Christian Bender
|
||||||
date: 21.12.2017
|
date: 21.12.2017
|
||||||
class: XORCipher
|
class: XORCipher
|
||||||
|
|
||||||
This class implements the XOR-cipher algorithm and provides
|
This class implements the XOR-cipher algorithm and provides
|
||||||
some useful methods for encrypting and decrypting strings and
|
some useful methods for encrypting and decrypting strings and
|
||||||
files.
|
files.
|
||||||
|
|
||||||
Overview about methods
|
Overview about methods
|
||||||
|
|
||||||
- encrypt : list of char
|
- encrypt : list of char
|
||||||
- decrypt : list of char
|
- decrypt : list of char
|
||||||
- encrypt_string : str
|
- encrypt_string : str
|
||||||
- decrypt_string : str
|
- decrypt_string : str
|
||||||
- encrypt_file : boolean
|
- encrypt_file : boolean
|
||||||
- decrypt_file : boolean
|
- decrypt_file : boolean
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class XORCipher(object):
|
class XORCipher(object):
|
||||||
def __init__(self, key=0):
|
def __init__(self, key=0):
|
||||||
"""
|
"""
|
||||||
simple constructor that receives a key or uses
|
simple constructor that receives a key or uses
|
||||||
default key = 0
|
default key = 0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# private field
|
# private field
|
||||||
self.__key = key
|
self.__key = key
|
||||||
|
|
||||||
def encrypt(self, content, key):
|
def encrypt(self, content, key):
|
||||||
"""
|
"""
|
||||||
input: 'content' of type string and 'key' of type int
|
input: 'content' of type string and 'key' of type int
|
||||||
output: encrypted string 'content' as a list of chars
|
output: encrypted string 'content' as a list of chars
|
||||||
if key not passed the method uses the key by the constructor.
|
if key not passed the method uses the key by the constructor.
|
||||||
otherwise key = 1
|
otherwise key = 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(key, int) and isinstance(content, str)
|
assert isinstance(key, int) and isinstance(content, str)
|
||||||
@ -55,11 +55,11 @@ class XORCipher(object):
|
|||||||
|
|
||||||
def decrypt(self, content, key):
|
def decrypt(self, content, key):
|
||||||
"""
|
"""
|
||||||
input: 'content' of type list and 'key' of type int
|
input: 'content' of type list and 'key' of type int
|
||||||
output: decrypted string 'content' as a list of chars
|
output: decrypted string 'content' as a list of chars
|
||||||
if key not passed the method uses the key by the constructor.
|
if key not passed the method uses the key by the constructor.
|
||||||
otherwise key = 1
|
otherwise key = 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(key, int) and isinstance(content, list)
|
assert isinstance(key, int) and isinstance(content, list)
|
||||||
@ -80,11 +80,11 @@ class XORCipher(object):
|
|||||||
|
|
||||||
def encrypt_string(self, content, key=0):
|
def encrypt_string(self, content, key=0):
|
||||||
"""
|
"""
|
||||||
input: 'content' of type string and 'key' of type int
|
input: 'content' of type string and 'key' of type int
|
||||||
output: encrypted string 'content'
|
output: encrypted string 'content'
|
||||||
if key not passed the method uses the key by the constructor.
|
if key not passed the method uses the key by the constructor.
|
||||||
otherwise key = 1
|
otherwise key = 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(key, int) and isinstance(content, str)
|
assert isinstance(key, int) and isinstance(content, str)
|
||||||
@ -105,11 +105,11 @@ class XORCipher(object):
|
|||||||
|
|
||||||
def decrypt_string(self, content, key=0):
|
def decrypt_string(self, content, key=0):
|
||||||
"""
|
"""
|
||||||
input: 'content' of type string and 'key' of type int
|
input: 'content' of type string and 'key' of type int
|
||||||
output: decrypted string 'content'
|
output: decrypted string 'content'
|
||||||
if key not passed the method uses the key by the constructor.
|
if key not passed the method uses the key by the constructor.
|
||||||
otherwise key = 1
|
otherwise key = 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(key, int) and isinstance(content, str)
|
assert isinstance(key, int) and isinstance(content, str)
|
||||||
@ -130,12 +130,12 @@ class XORCipher(object):
|
|||||||
|
|
||||||
def encrypt_file(self, file, key=0):
|
def encrypt_file(self, file, key=0):
|
||||||
"""
|
"""
|
||||||
input: filename (str) and a key (int)
|
input: filename (str) and a key (int)
|
||||||
output: returns true if encrypt process was
|
output: returns true if encrypt process was
|
||||||
successful otherwise false
|
successful otherwise false
|
||||||
if key not passed the method uses the key by the constructor.
|
if key not passed the method uses the key by the constructor.
|
||||||
otherwise key = 1
|
otherwise key = 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(file, str) and isinstance(key, int)
|
assert isinstance(file, str) and isinstance(key, int)
|
||||||
@ -155,12 +155,12 @@ class XORCipher(object):
|
|||||||
|
|
||||||
def decrypt_file(self, file, key):
|
def decrypt_file(self, file, key):
|
||||||
"""
|
"""
|
||||||
input: filename (str) and a key (int)
|
input: filename (str) and a key (int)
|
||||||
output: returns true if decrypt process was
|
output: returns true if decrypt process was
|
||||||
successful otherwise false
|
successful otherwise false
|
||||||
if key not passed the method uses the key by the constructor.
|
if key not passed the method uses the key by the constructor.
|
||||||
otherwise key = 1
|
otherwise key = 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# precondition
|
# precondition
|
||||||
assert isinstance(file, str) and isinstance(key, int)
|
assert isinstance(file, str) and isinstance(key, int)
|
||||||
@ -195,11 +195,11 @@ class XORCipher(object):
|
|||||||
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))
|
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))
|
||||||
|
|
||||||
# if (crypt.encrypt_file("test.txt",key)):
|
# if (crypt.encrypt_file("test.txt",key)):
|
||||||
# print("encrypt successful")
|
# print("encrypt successful")
|
||||||
# else:
|
# else:
|
||||||
# print("encrypt unsuccessful")
|
# print("encrypt unsuccessful")
|
||||||
|
|
||||||
# if (crypt.decrypt_file("encrypt.out",key)):
|
# if (crypt.decrypt_file("encrypt.out",key)):
|
||||||
# print("decrypt successful")
|
# print("decrypt successful")
|
||||||
# else:
|
# else:
|
||||||
# print("decrypt unsuccessful")
|
# print("decrypt unsuccessful")
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
|
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
|
||||||
Soruce: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
|
Soruce: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ Enter an Infix Equation = a + b ^c
|
|||||||
a | + | cb^a
|
a | + | cb^a
|
||||||
| | cb^a+
|
| | cb^a+
|
||||||
|
|
||||||
a+b^c (Infix) -> +a^bc (Prefix)
|
a+b^c (Infix) -> +a^bc (Prefix)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ Enter a Postfix Equation (space separated) = 5 6 9 * +
|
|||||||
| pop(5) |
|
| pop(5) |
|
||||||
+ | push(5+54) | 59
|
+ | push(5+54) | 59
|
||||||
|
|
||||||
Result = 59
|
Result = 59
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import operator as op
|
import operator as op
|
||||||
|
@ -13,28 +13,28 @@ 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):
|
def naive_cut_rod_recursive(n: int, prices: list):
|
||||||
"""
|
"""
|
||||||
Solves the rod-cutting problem via naively without using the benefit of dynamic programming.
|
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
|
The results is the same sub-problems are solved several times leading to an exponential runtime
|
||||||
|
|
||||||
Runtime: O(2^n)
|
Runtime: O(2^n)
|
||||||
|
|
||||||
Arguments
|
Arguments
|
||||||
-------
|
-------
|
||||||
n: int, the length of the rod
|
n: int, the length of the rod
|
||||||
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
|
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
|
||||||
price for a rod of length ``i``
|
price for a rod of length ``i``
|
||||||
|
|
||||||
Returns
|
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
|
Examples
|
||||||
--------
|
--------
|
||||||
>>> naive_cut_rod_recursive(4, [1, 5, 8, 9])
|
>>> naive_cut_rod_recursive(4, [1, 5, 8, 9])
|
||||||
10
|
10
|
||||||
>>> naive_cut_rod_recursive(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
|
>>> naive_cut_rod_recursive(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
|
||||||
30
|
30
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_enforce_args(n, prices)
|
_enforce_args(n, prices)
|
||||||
if n == 0:
|
if n == 0:
|
||||||
@ -50,33 +50,33 @@ def naive_cut_rod_recursive(n: int, prices: list):
|
|||||||
|
|
||||||
def top_down_cut_rod(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
|
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
|
via memoization. This function serves as a wrapper for _top_down_cut_rod_recursive
|
||||||
|
|
||||||
Runtime: O(n^2)
|
Runtime: O(n^2)
|
||||||
|
|
||||||
Arguments
|
Arguments
|
||||||
--------
|
--------
|
||||||
n: int, the length of the rod
|
n: int, the length of the rod
|
||||||
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
|
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
|
||||||
price for a rod of length ``i``
|
price for a rod of length ``i``
|
||||||
|
|
||||||
Note
|
Note
|
||||||
----
|
----
|
||||||
For convenience and because Python's lists using 0-indexing, length(max_rev) = n + 1,
|
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.
|
to accommodate for the revenue obtainable from a rod of length 0.
|
||||||
|
|
||||||
Returns
|
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
|
Examples
|
||||||
-------
|
-------
|
||||||
>>> top_down_cut_rod(4, [1, 5, 8, 9])
|
>>> top_down_cut_rod(4, [1, 5, 8, 9])
|
||||||
10
|
10
|
||||||
>>> top_down_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
|
>>> top_down_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
|
||||||
30
|
30
|
||||||
"""
|
"""
|
||||||
_enforce_args(n, prices)
|
_enforce_args(n, prices)
|
||||||
max_rev = [float("-inf") for _ in range(n + 1)]
|
max_rev = [float("-inf") for _ in range(n + 1)]
|
||||||
return _top_down_cut_rod_recursive(n, prices, max_rev)
|
return _top_down_cut_rod_recursive(n, prices, max_rev)
|
||||||
@ -84,23 +84,23 @@ def top_down_cut_rod(n: int, prices: list):
|
|||||||
|
|
||||||
def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list):
|
def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list):
|
||||||
"""
|
"""
|
||||||
Constructs a top-down dynamic programming solution for the rod-cutting problem
|
Constructs a top-down dynamic programming solution for the rod-cutting problem
|
||||||
via memoization.
|
via memoization.
|
||||||
|
|
||||||
Runtime: O(n^2)
|
Runtime: O(n^2)
|
||||||
|
|
||||||
Arguments
|
Arguments
|
||||||
--------
|
--------
|
||||||
n: int, the length of the rod
|
n: int, the length of the rod
|
||||||
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
|
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
|
||||||
price for a rod of length ``i``
|
price for a rod of length ``i``
|
||||||
max_rev: list, the computed maximum revenue for a piece of rod.
|
max_rev: list, the computed maximum revenue for a piece of rod.
|
||||||
``max_rev[i]`` is the maximum revenue obtainable for a rod of length ``i``
|
``max_rev[i]`` is the maximum revenue obtainable for a rod of length ``i``
|
||||||
|
|
||||||
Returns
|
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:
|
if max_rev[n] >= 0:
|
||||||
return max_rev[n]
|
return max_rev[n]
|
||||||
elif n == 0:
|
elif n == 0:
|
||||||
@ -120,28 +120,28 @@ def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list):
|
|||||||
|
|
||||||
def bottom_up_cut_rod(n: int, prices: list):
|
def bottom_up_cut_rod(n: int, prices: list):
|
||||||
"""
|
"""
|
||||||
Constructs a bottom-up dynamic programming solution for the rod-cutting problem
|
Constructs a bottom-up dynamic programming solution for the rod-cutting problem
|
||||||
|
|
||||||
Runtime: O(n^2)
|
Runtime: O(n^2)
|
||||||
|
|
||||||
Arguments
|
Arguments
|
||||||
----------
|
----------
|
||||||
n: int, the maximum length of the rod.
|
n: int, the maximum length of the rod.
|
||||||
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
|
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
|
||||||
price for a rod of length ``i``
|
price for a rod of length ``i``
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
The maximum revenue obtainable from cutting a rod of length n given
|
The maximum revenue obtainable from cutting a rod of length n given
|
||||||
the prices for each piece of rod p.
|
the prices for each piece of rod p.
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
-------
|
-------
|
||||||
>>> bottom_up_cut_rod(4, [1, 5, 8, 9])
|
>>> bottom_up_cut_rod(4, [1, 5, 8, 9])
|
||||||
10
|
10
|
||||||
>>> bottom_up_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
|
>>> bottom_up_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
|
||||||
30
|
30
|
||||||
"""
|
"""
|
||||||
_enforce_args(n, prices)
|
_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.
|
||||||
@ -160,15 +160,15 @@ def bottom_up_cut_rod(n: int, prices: list):
|
|||||||
|
|
||||||
def _enforce_args(n: int, prices: list):
|
def _enforce_args(n: int, prices: list):
|
||||||
"""
|
"""
|
||||||
Basic checks on the arguments to the rod-cutting algorithms
|
Basic checks on the arguments to the rod-cutting algorithms
|
||||||
|
|
||||||
n: int, the length of the rod
|
n: int, the length of the rod
|
||||||
prices: list, the price list for each piece of rod.
|
prices: list, the price list for each piece of rod.
|
||||||
|
|
||||||
Throws ValueError:
|
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:
|
if n < 0:
|
||||||
raise ValueError(f"n must be greater than or equal to 0. Got n = {n}")
|
raise ValueError(f"n must be greater than or equal to 0. Got n = {n}")
|
||||||
|
|
||||||
|
@ -3,9 +3,9 @@
|
|||||||
# Function to print upper half of diamond (pyramid)
|
# Function to print upper half of diamond (pyramid)
|
||||||
def floyd(n):
|
def floyd(n):
|
||||||
"""
|
"""
|
||||||
Parameters:
|
Parameters:
|
||||||
n : size of pattern
|
n : size of pattern
|
||||||
"""
|
"""
|
||||||
for i in range(0, n):
|
for i in range(0, n):
|
||||||
for j in range(0, n - i - 1): # printing spaces
|
for j in range(0, n - i - 1): # printing spaces
|
||||||
print(" ", end="")
|
print(" ", end="")
|
||||||
@ -17,9 +17,9 @@ def floyd(n):
|
|||||||
# Function to print lower half of diamond (pyramid)
|
# Function to print lower half of diamond (pyramid)
|
||||||
def reverse_floyd(n):
|
def reverse_floyd(n):
|
||||||
"""
|
"""
|
||||||
Parameters:
|
Parameters:
|
||||||
n : size of pattern
|
n : size of pattern
|
||||||
"""
|
"""
|
||||||
for i in range(n, 0, -1):
|
for i in range(n, 0, -1):
|
||||||
for j in range(i, 0, -1): # printing stars
|
for j in range(i, 0, -1): # printing stars
|
||||||
print("* ", end="")
|
print("* ", end="")
|
||||||
@ -31,9 +31,9 @@ def reverse_floyd(n):
|
|||||||
# Function to print complete diamond pattern of "*"
|
# Function to print complete diamond pattern of "*"
|
||||||
def pretty_print(n):
|
def pretty_print(n):
|
||||||
"""
|
"""
|
||||||
Parameters:
|
Parameters:
|
||||||
n : size of pattern
|
n : size of pattern
|
||||||
"""
|
"""
|
||||||
if n <= 0:
|
if n <= 0:
|
||||||
print(" ... .... nothing printing :(")
|
print(" ... .... nothing printing :(")
|
||||||
return
|
return
|
||||||
|
@ -3,9 +3,9 @@ The nested brackets problem is a problem that determines if a sequence of
|
|||||||
brackets are properly nested. A sequence of brackets s is considered properly nested
|
brackets are properly nested. A sequence of brackets s is considered properly nested
|
||||||
if any of the following conditions are true:
|
if any of the following conditions are true:
|
||||||
|
|
||||||
- s is empty
|
- s is empty
|
||||||
- s has the form (U) or [U] or {U} where U is a properly nested string
|
- s has the form (U) or [U] or {U} where U is a properly nested string
|
||||||
- s has the form VW where V and W are properly nested strings
|
- s has the form VW where V and W are properly nested strings
|
||||||
|
|
||||||
For example, the string "()()[()]" is properly nested but "[(()]" is not.
|
For example, the string "()()[()]" is properly nested but "[(()]" is not.
|
||||||
|
|
||||||
|
@ -39,17 +39,17 @@ def is_prime(k: int) -> bool:
|
|||||||
|
|
||||||
def solution(a_limit: int, b_limit: int) -> int:
|
def solution(a_limit: int, b_limit: int) -> int:
|
||||||
"""
|
"""
|
||||||
>>> solution(1000, 1000)
|
>>> solution(1000, 1000)
|
||||||
-59231
|
-59231
|
||||||
>>> solution(200, 1000)
|
>>> solution(200, 1000)
|
||||||
-59231
|
-59231
|
||||||
>>> solution(200, 200)
|
>>> solution(200, 200)
|
||||||
-4925
|
-4925
|
||||||
>>> solution(-1000, 1000)
|
>>> solution(-1000, 1000)
|
||||||
0
|
0
|
||||||
>>> solution(-1000, -1000)
|
>>> solution(-1000, -1000)
|
||||||
0
|
0
|
||||||
"""
|
"""
|
||||||
longest = [0, 0, 0] # length, a, b
|
longest = [0, 0, 0] # length, a, b
|
||||||
for a in range((a_limit * -1) + 1, a_limit):
|
for a in range((a_limit * -1) + 1, a_limit):
|
||||||
for b in range(2, b_limit):
|
for b in range(2, b_limit):
|
||||||
|
@ -113,7 +113,7 @@ if __name__ == "__main__":
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
"""
|
"""
|
||||||
user_input = input('Enter numbers separated by comma:\n').strip()
|
user_input = input('Enter numbers separated by comma:\n').strip()
|
||||||
collection = [int(item) for item in user_input.split(',')]
|
collection = [int(item) for item in user_input.split(',')]
|
||||||
try:
|
try:
|
||||||
__assert_sorted(collection)
|
__assert_sorted(collection)
|
||||||
@ -122,7 +122,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
target_input = input('Enter a single number to be found in the list:\n')
|
target_input = input('Enter a single number to be found in the list:\n')
|
||||||
target = int(target_input)
|
target = int(target_input)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
debug = 0
|
debug = 0
|
||||||
if debug == 1:
|
if debug == 1:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user