提交 b838f104 编写于 作者: C Christian Clauss 提交者: John Law

Fix indentation contains tabs (flake8 E101,W191) (#1573)

上级 dbaedd4e
......@@ -5,7 +5,7 @@ before_install: pip install --upgrade pip setuptools
install: pip install -r requirements.txt
before_script:
- 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:
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
- mypy --ignore-missing-imports .
......
# -*- coding: utf-8 -*-
"""
In this problem, we want to determine all possible combinations of k
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)!)))
In this problem, we want to determine all possible combinations of k
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)!)))
"""
......
"""
In this problem, we want to determine all possible permutations
of the given sequence. We use backtracking to solve this problem.
In this problem, we want to determine all possible permutations
of the given sequence. We use backtracking to solve this problem.
Time complexity: O(n! * n),
where n denotes the length of the given sequence.
Time complexity: O(n! * n),
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):
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly len(sequence) - index children.
It terminates when it reaches the end of the given sequence.
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly len(sequence) - index children.
It terminates when it reaches the end of the given sequence.
"""
if index == len(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")
sequence = list(map(int, input().split()))
......
"""
In this problem, we want to determine all possible subsequences
of the given sequence. We use backtracking to solve this problem.
In this problem, we want to determine all possible subsequences
of the given sequence. We use backtracking to solve this problem.
Time complexity: O(2^n),
where n denotes the length of the given sequence.
Time complexity: O(2^n),
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):
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly two children.
It terminates when it reaches the end of the given sequence.
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly two children.
It terminates when it reaches the end of the given sequence.
"""
if index == len(sequence):
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")
sequence = list(map(int, input().split()))
......
"""
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.
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.
Summation of the chosen numbers must be equal to given number M and one number can
be used only once.
Summation of the chosen numbers must be equal to given number M and one number can
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):
"""
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
given below satisfy.
This algorithm follows depth-fist-search and backtracks when the node is not branchable.
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
given below satisfy.
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:
return
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")
nums = list(map(int, input().split()))
......
def compare_string(string1, string2):
"""
>>> compare_string('0010','0110')
'0_10'
>>> compare_string('0110','1101')
-1
"""
>>> compare_string('0010','0110')
'0_10'
>>> compare_string('0110','1101')
-1
"""
l1 = list(string1)
l2 = list(string2)
count = 0
......@@ -21,9 +21,9 @@ def compare_string(string1, string2):
def check(binary):
"""
>>> check(['0.00.01.5'])
['0.00.01.5']
"""
>>> check(['0.00.01.5'])
['0.00.01.5']
"""
pi = []
while 1:
check1 = ["$"] * len(binary)
......@@ -45,9 +45,9 @@ def check(binary):
def decimal_to_binary(no_of_variable, minterms):
"""
>>> decimal_to_binary(3,[1.5])
['0.00.01.5']
"""
>>> decimal_to_binary(3,[1.5])
['0.00.01.5']
"""
temp = []
s = ""
for m in minterms:
......@@ -61,12 +61,12 @@ def decimal_to_binary(no_of_variable, minterms):
def is_for_table(string1, string2, count):
"""
>>> is_for_table('__1','011',2)
True
>>> is_for_table('01_','001',1)
False
"""
>>> is_for_table('__1','011',2)
True
>>> is_for_table('01_','001',1)
False
"""
l1 = list(string1)
l2 = list(string2)
count_n = 0
......@@ -81,12 +81,12 @@ def is_for_table(string1, string2, count):
def selection(chart, prime_implicants):
"""
>>> selection([[1]],['0.00.01.5'])
['0.00.01.5']
>>> selection([[1]],['0.00.01.5'])
['0.00.01.5']
"""
>>> selection([[1]],['0.00.01.5'])
['0.00.01.5']
>>> selection([[1]],['0.00.01.5'])
['0.00.01.5']
"""
temp = []
select = [0] * len(chart)
for i in range(len(chart[0])):
......@@ -128,9 +128,9 @@ def selection(chart, prime_implicants):
def prime_implicant_chart(prime_implicants, binary):
"""
>>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5'])
[[1]]
"""
>>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5'])
[[1]]
"""
chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))]
for i in range(len(prime_implicants)):
count = prime_implicants[i].count("_")
......
"""
author: Christian Bender
date: 21.12.2017
class: XORCipher
This class implements the XOR-cipher algorithm and provides
some useful methods for encrypting and decrypting strings and
files.
Overview about methods
- encrypt : list of char
- decrypt : list of char
- encrypt_string : str
- decrypt_string : str
- encrypt_file : boolean
- decrypt_file : boolean
author: Christian Bender
date: 21.12.2017
class: XORCipher
This class implements the XOR-cipher algorithm and provides
some useful methods for encrypting and decrypting strings and
files.
Overview about methods
- encrypt : list of char
- decrypt : list of char
- encrypt_string : str
- decrypt_string : str
- encrypt_file : boolean
- decrypt_file : boolean
"""
class XORCipher(object):
def __init__(self, key=0):
"""
simple constructor that receives a key or uses
default key = 0
"""
simple constructor that receives a key or uses
default key = 0
"""
# private field
self.__key = key
def encrypt(self, content, key):
"""
input: 'content' of type string and 'key' of type int
output: encrypted string 'content' as a list of chars
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
input: 'content' of type string and 'key' of type int
output: encrypted string 'content' as a list of chars
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
# precondition
assert isinstance(key, int) and isinstance(content, str)
......@@ -55,11 +55,11 @@ class XORCipher(object):
def decrypt(self, content, key):
"""
input: 'content' of type list and 'key' of type int
output: decrypted string 'content' as a list of chars
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
input: 'content' of type list and 'key' of type int
output: decrypted string 'content' as a list of chars
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
# precondition
assert isinstance(key, int) and isinstance(content, list)
......@@ -80,11 +80,11 @@ class XORCipher(object):
def encrypt_string(self, content, key=0):
"""
input: 'content' of type string and 'key' of type int
output: encrypted string 'content'
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
input: 'content' of type string and 'key' of type int
output: encrypted string 'content'
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
# precondition
assert isinstance(key, int) and isinstance(content, str)
......@@ -105,11 +105,11 @@ class XORCipher(object):
def decrypt_string(self, content, key=0):
"""
input: 'content' of type string and 'key' of type int
output: decrypted string 'content'
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
input: 'content' of type string and 'key' of type int
output: decrypted string 'content'
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
# precondition
assert isinstance(key, int) and isinstance(content, str)
......@@ -130,12 +130,12 @@ class XORCipher(object):
def encrypt_file(self, file, key=0):
"""
input: filename (str) and a key (int)
output: returns true if encrypt process was
successful otherwise false
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
input: filename (str) and a key (int)
output: returns true if encrypt process was
successful otherwise false
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
# precondition
assert isinstance(file, str) and isinstance(key, int)
......@@ -155,12 +155,12 @@ class XORCipher(object):
def decrypt_file(self, file, key):
"""
input: filename (str) and a key (int)
output: returns true if decrypt process was
successful otherwise false
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
input: filename (str) and a key (int)
output: returns true if decrypt process was
successful otherwise false
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
# precondition
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))
# if (crypt.encrypt_file("test.txt",key)):
# print("encrypt successful")
# print("encrypt successful")
# else:
# print("encrypt unsuccessful")
# print("encrypt unsuccessful")
# if (crypt.decrypt_file("encrypt.out",key)):
# print("decrypt successful")
# print("decrypt successful")
# else:
# print("decrypt unsuccessful")
# print("decrypt unsuccessful")
"""
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/
"""
......
......@@ -11,7 +11,7 @@ Enter an Infix Equation = a + b ^c
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) |
+ | push(5+54) | 59
Result = 59
Result = 59
"""
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):
"""
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)
Runtime: O(2^n)
Arguments
-------
n: int, the length of the rod
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
price for a rod of length ``i``
Arguments
-------
n: int, the length of the rod
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
price for a rod of length ``i``
Returns
-------
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
Returns
-------
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
Examples
--------
>>> naive_cut_rod_recursive(4, [1, 5, 8, 9])
10
>>> naive_cut_rod_recursive(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
30
"""
Examples
--------
>>> naive_cut_rod_recursive(4, [1, 5, 8, 9])
10
>>> naive_cut_rod_recursive(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
30
"""
_enforce_args(n, prices)
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):
"""
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)
Arguments
--------
n: int, the length of the rod
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
price for a rod of length ``i``
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.
Returns
-------
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
Examples
-------
>>> top_down_cut_rod(4, [1, 5, 8, 9])
10
>>> top_down_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
30
"""
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)
Arguments
--------
n: int, the length of the rod
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
price for a rod of length ``i``
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.
Returns
-------
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
Examples
-------
>>> top_down_cut_rod(4, [1, 5, 8, 9])
10
>>> top_down_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
30
"""
_enforce_args(n, prices)
max_rev = [float("-inf") for _ in range(n + 1)]
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):
"""
Constructs a top-down dynamic programming solution for the rod-cutting problem
via memoization.
Runtime: O(n^2)
Arguments
--------
n: int, the length of the rod
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
price for a rod of length ``i``
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``
Returns
-------
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
"""
Constructs a top-down dynamic programming solution for the rod-cutting problem
via memoization.
Runtime: O(n^2)
Arguments
--------
n: int, the length of the rod
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
price for a rod of length ``i``
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``
Returns
-------
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]
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):
"""
Constructs a bottom-up dynamic programming solution for the rod-cutting problem
Runtime: O(n^2)
Arguments
----------
n: int, the maximum length of the rod.
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
price for a rod of length ``i``
Returns
-------
The maximum revenue obtainable from cutting a rod of length n given
the prices for each piece of rod p.
Examples
-------
>>> bottom_up_cut_rod(4, [1, 5, 8, 9])
10
>>> bottom_up_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
30
"""
Constructs a bottom-up dynamic programming solution for the rod-cutting problem
Runtime: O(n^2)
Arguments
----------
n: int, the maximum length of the rod.
prices: list, the prices for each piece of rod. ``p[i-i]`` is the
price for a rod of length ``i``
Returns
-------
The maximum revenue obtainable from cutting a rod of length n given
the prices for each piece of rod p.
Examples
-------
>>> bottom_up_cut_rod(4, [1, 5, 8, 9])
10
>>> bottom_up_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
30
"""
_enforce_args(n, prices)
# 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):
"""
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
prices: list, the price list for each piece of rod.
n: int, the length of the 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:
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)
def floyd(n):
"""
Parameters:
n : size of pattern
"""
Parameters:
n : size of pattern
"""
for i in range(0, n):
for j in range(0, n - i - 1): # printing spaces
print(" ", end="")
......@@ -17,9 +17,9 @@ def floyd(n):
# Function to print lower half of diamond (pyramid)
def reverse_floyd(n):
"""
Parameters:
n : size of pattern
"""
Parameters:
n : size of pattern
"""
for i in range(n, 0, -1):
for j in range(i, 0, -1): # printing stars
print("* ", end="")
......@@ -31,9 +31,9 @@ def reverse_floyd(n):
# Function to print complete diamond pattern of "*"
def pretty_print(n):
"""
Parameters:
n : size of pattern
"""
Parameters:
n : size of pattern
"""
if n <= 0:
print(" ... .... nothing printing :(")
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
if any of the following conditions are true:
- s is empty
- 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 is empty
- 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
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:
"""
>>> solution(1000, 1000)
-59231
>>> solution(200, 1000)
-59231
>>> solution(200, 200)
-4925
>>> solution(-1000, 1000)
0
>>> solution(-1000, -1000)
0
"""
>>> solution(1000, 1000)
-59231
>>> solution(200, 1000)
-59231
>>> solution(200, 200)
-4925
>>> solution(-1000, 1000)
0
>>> solution(-1000, -1000)
0
"""
longest = [0, 0, 0] # length, a, b
for a in range((a_limit * -1) + 1, a_limit):
for b in range(2, b_limit):
......
......@@ -113,7 +113,7 @@ if __name__ == "__main__":
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(',')]
try:
__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 = int(target_input)
"""
"""
debug = 0
if debug == 1:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册