Skip to content

[pre-commit.ci] pre-commit autoupdate #9543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repos:
- id: black

- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
rev: v2.2.6
hooks:
- id: codespell
additional_dependencies:
Expand Down
4 changes: 2 additions & 2 deletions computer_vision/cnn_classification.py.DISABLED.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Download dataset from :
https://lhncbc.nlm.nih.gov/LHC-publications/pubs/TuberculosisChestXrayImageDataSets.html

1. Download the dataset folder and create two folder training set and test set
in the parent dataste folder
in the parent dataset folder
2. Move 30-40 image from both TB positive and TB Negative folder
in the test set folder
3. The labels of the iamges will be extracted from the folder name
3. The labels of the images will be extracted from the folder name
the image is present in.

"""
Expand Down
2 changes: 1 addition & 1 deletion computer_vision/mosaic_augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import cv2
import numpy as np

# Parrameters
# Parameters
OUTPUT_SIZE = (720, 1280) # Height, Width
SCALE_RANGE = (0.4, 0.6) # if height or width lower than this scale, drop it.
FILTER_TINY_SCALE = 1 / 100
Expand Down
11 changes: 4 additions & 7 deletions dynamic_programming/min_distance_up_bottom.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""
Author : Alexander Pantyukhin
Date : October 14, 2022
This is implementation Dynamic Programming up bottom approach
to find edit distance.
The aim is to demonstate up bottom approach for solving the task.
The implementation was tested on the
leetcode: https://leetcode.com/problems/edit-distance/
This is an implementation of the up-bottom approach to find edit distance.
The implementation was tested on Leetcode: https://leetcode.com/problems/edit-distance/

Levinstein distance
Dynamic Programming: up -> down.
Expand All @@ -30,10 +27,10 @@ def min_distance_up_bottom(word1: str, word2: str) -> int:

@functools.cache
def min_distance(index1: int, index2: int) -> int:
# if first word index is overflow - delete all from the second word
# if first word index overflows - delete all from the second word
if index1 >= len_word1:
return len_word2 - index2
# if second word index is overflow - delete all from the first word
# if second word index overflows - delete all from the first word
if index2 >= len_word2:
return len_word1 - index1
diff = int(word1[index1] != word2[index2]) # current letters not identical
Expand Down
8 changes: 4 additions & 4 deletions graphs/tests/test_min_spanning_tree_prim.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def test_prim_successful_result():
[1, 7, 11],
]

adjancency = defaultdict(list)
adjacency = defaultdict(list)
for node1, node2, cost in edges:
adjancency[node1].append([node2, cost])
adjancency[node2].append([node1, cost])
adjacency[node1].append([node2, cost])
adjacency[node2].append([node1, cost])

result = mst(adjancency)
result = mst(adjacency)

expected = [
[7, 6, 1],
Expand Down
36 changes: 19 additions & 17 deletions hashes/sha1.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
"""
Demonstrates implementation of SHA1 Hash function in a Python class and gives utilities
to find hash of string or hash of text from a file.
Implementation of the SHA1 hash function and gives utilities to find hash of string or
hash of text from a file. Also contains a Test class to verify that the generated hash
matches what is returned by the hashlib library

Usage: python sha1.py --string "Hello World!!"
python sha1.py --file "hello_world.txt"
When run without any arguments, it prints the hash of the string "Hello World!!
Welcome to Cryptography"
Also contains a Test class to verify that the generated Hash is same as that
returned by the hashlib library

SHA1 hash or SHA1 sum of a string is a cryptographic function which means it is easy
SHA1 hash or SHA1 sum of a string is a cryptographic function, which means it is easy
to calculate forwards but extremely difficult to calculate backwards. What this means
is, you can easily calculate the hash of a string, but it is extremely difficult to
know the original string if you have its hash. This property is useful to communicate
securely, send encrypted messages and is very useful in payment systems, blockchain
and cryptocurrency etc.
The Algorithm as described in the reference:
is you can easily calculate the hash of a string, but it is extremely difficult to know
the original string if you have its hash. This property is useful for communicating
securely, send encrypted messages and is very useful in payment systems, blockchain and
cryptocurrency etc.

The algorithm as described in the reference:
First we start with a message. The message is padded and the length of the message
is added to the end. It is then split into blocks of 512 bits or 64 bytes. The blocks
are then processed one at a time. Each block must be expanded and compressed.
The value after each compression is added to a 160bit buffer called the current hash
state. After the last block is processed the current hash state is returned as
The value after each compression is added to a 160-bit buffer called the current hash
state. After the last block is processed, the current hash state is returned as
the final hash.

Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/
"""
import argparse
Expand All @@ -30,18 +32,18 @@

class SHA1Hash:
"""
Class to contain the entire pipeline for SHA1 Hashing Algorithm
Class to contain the entire pipeline for SHA1 hashing algorithm
>>> SHA1Hash(bytes('Allan', 'utf-8')).final_hash()
'872af2d8ac3d8695387e7c804bf0e02c18df9e6e'
"""

def __init__(self, data):
"""
Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal
Initiates the variables data and h. h is a list of 5 8-digit hexadecimal
numbers corresponding to
(1732584193, 4023233417, 2562383102, 271733878, 3285377520)
respectively. We will start with this as a message digest. 0x is how you write
Hexadecimal numbers in Python
hexadecimal numbers in Python
"""
self.data = data
self.h = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
Expand Down Expand Up @@ -90,7 +92,7 @@ def final_hash(self):
For each block, the variable h that was initialized is copied to a,b,c,d,e
and these 5 variables a,b,c,d,e undergo several changes. After all the blocks
are processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1]
and so on. This h becomes our final hash which is returned.
and so on. This h becomes our final hash which is returned.
"""
self.padded_data = self.padding()
self.blocks = self.split_blocks()
Expand Down Expand Up @@ -135,7 +137,7 @@ def test_sha1_hash():
def main():
"""
Provides option 'string' or 'file' to take input and prints the calculated SHA1
hash. unittest.main() has been commented because we probably don't want to run
hash. unittest.main() has been commented out because we probably don't want to run
the test each time.
"""
# unittest.main()
Expand Down
31 changes: 12 additions & 19 deletions maths/pi_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,53 @@ def calculate_pi(limit: int) -> str:
https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
Leibniz Formula for Pi

The Leibniz formula is the special case arctan 1 = 1/4 Pi .
The Leibniz formula is the special case arctan(1) = pi / 4.
Leibniz's formula converges extremely slowly: it exhibits sublinear convergence.

Convergence (https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80#Convergence)

We cannot try to prove against an interrupted, uncompleted generation.
https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80#Unusual_behaviour
The errors can in fact be predicted;
but those calculations also approach infinity for accuracy.
The errors can in fact be predicted, but those calculations also approach infinity
for accuracy.

Our output will always be a string since we can defintely store all digits in there.
For simplicity' sake, let's just compare against known values and since our outpit
is a string, we need to convert to float.
Our output will be a string so that we can definitely store all digits.

>>> import math
>>> float(calculate_pi(15)) == math.pi
True

Since we cannot predict errors or interrupt any infinite alternating
series generation since they approach infinity,
or interrupt any alternating series, we are going to need math.isclose()
Since we cannot predict errors or interrupt any infinite alternating series
generation since they approach infinity, or interrupt any alternating series, we'll
need math.isclose()

>>> math.isclose(float(calculate_pi(50)), math.pi)
True

>>> math.isclose(float(calculate_pi(100)), math.pi)
True

Since math.pi-constant contains only 16 digits, here some test with preknown values:
Since math.pi contains only 16 digits, here are some tests with known values:

>>> calculate_pi(50)
'3.14159265358979323846264338327950288419716939937510'
>>> calculate_pi(80)
'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899'

To apply the Leibniz formula for calculating pi,
the variables q, r, t, k, n, and l are used for the iteration process.
"""
# Variables used for the iteration process
q = 1
r = 0
t = 1
k = 1
n = 3
l = 3

decimal = limit
counter = 0

result = ""

"""
We will avoid using yield since we otherwise get a Generator-Object,
which we can't just compare against anything. We would have to make a list out of it
after the generation, so we will just stick to plain return logic:
"""
# We can't compare against anything if we make a generator,
# so we'll stick with plain return logic
while counter != decimal + 1:
if 4 * q + r - t < n * t:
result += str(n)
Expand Down
4 changes: 2 additions & 2 deletions maths/radians.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def radians(degree: float) -> float:
"""
Coverts the given angle from degrees to radians
Converts the given angle from degrees to radians
https://en.wikipedia.org/wiki/Radian

>>> radians(180)
Expand All @@ -16,7 +16,7 @@ def radians(degree: float) -> float:
1.9167205845401725

>>> from math import radians as math_radians
>>> all(abs(radians(i)-math_radians(i)) <= 0.00000001 for i in range(-2, 361))
>>> all(abs(radians(i) - math_radians(i)) <= 1e-8 for i in range(-2, 361))
True
"""

Expand Down
7 changes: 3 additions & 4 deletions maths/square_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ def get_initial_point(a: float) -> float:


def square_root_iterative(
a: float, max_iter: int = 9999, tolerance: float = 0.00000000000001
a: float, max_iter: int = 9999, tolerance: float = 1e-14
) -> float:
"""
Square root is aproximated using Newtons method.
Square root approximated using Newton's method.
https://en.wikipedia.org/wiki/Newton%27s_method

>>> all(abs(square_root_iterative(i)-math.sqrt(i)) <= .00000000000001
... for i in range(500))
>>> all(abs(square_root_iterative(i) - math.sqrt(i)) <= 1e-14 for i in range(500))
True

>>> square_root_iterative(-1)
Expand Down
8 changes: 4 additions & 4 deletions neural_network/convolution_neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
Name - - CNN - Convolution Neural Network For Photo Recognizing
Goal - - Recognize Handing Writing Word Photo
DetailTotal 5 layers neural network
Detail: Total 5 layers neural network
* Convolution layer
* Pooling layer
* Input layer layer of BP
Expand All @@ -24,7 +24,7 @@ def __init__(
self, conv1_get, size_p1, bp_num1, bp_num2, bp_num3, rate_w=0.2, rate_t=0.2
):
"""
:param conv1_get: [a,c,d]size, number, step of convolution kernel
:param conv1_get: [a,c,d], size, number, step of convolution kernel
:param size_p1: pooling size
:param bp_num1: units number of flatten layer
:param bp_num2: units number of hidden layer
Expand Down Expand Up @@ -71,7 +71,7 @@ def save_model(self, save_path):
with open(save_path, "wb") as f:
pickle.dump(model_dic, f)

print(f"Model saved {save_path}")
print(f"Model saved: {save_path}")

@classmethod
def read_model(cls, model_path):
Expand Down Expand Up @@ -210,7 +210,7 @@ def _calculate_gradient_from_pool(
def train(
self, patterns, datas_train, datas_teach, n_repeat, error_accuracy, draw_e=bool
):
# model traning
# model training
print("----------------------Start Training-------------------------")
print((" - - Shape: Train_Data ", np.shape(datas_train)))
print((" - - Shape: Teach_Data ", np.shape(datas_teach)))
Expand Down
2 changes: 1 addition & 1 deletion neural_network/gan.py_tf
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ if __name__ == "__main__":
# G_b2 = np.random.normal(size=(784),scale=(1. / np.sqrt(784 / 2.))) *0.002
G_b7 = np.zeros(784)

# 3. For Adam Optimzier
# 3. For Adam Optimizer
v1, m1 = 0, 0
v2, m2 = 0, 0
v3, m3 = 0, 0
Expand Down
8 changes: 4 additions & 4 deletions other/graham_scan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
This is a pure Python implementation of the merge-insertion sort algorithm
This is a pure Python implementation of the Graham scan algorithm
Source: https://en.wikipedia.org/wiki/Graham_scan

For doctests run following command:
Expand Down Expand Up @@ -142,8 +142,8 @@ def graham_scan(points: list[tuple[int, int]]) -> list[tuple[int, int]]:
stack.append(sorted_points[0])
stack.append(sorted_points[1])
stack.append(sorted_points[2])
# In any ways, the first 3 points line are towards left.
# Because we sort them the angle from minx, miny.
# The first 3 points lines are towards the left because we sort them by their angle
# from minx, miny.
current_direction = Direction.left

for i in range(3, len(sorted_points)):
Expand All @@ -164,7 +164,7 @@ def graham_scan(points: list[tuple[int, int]]) -> list[tuple[int, int]]:
break
elif current_direction == Direction.right:
# If the straight line is towards right,
# every previous points on those straigh line is not convex hull.
# every previous points on that straight line is not convex hull.
stack.pop()
if next_direction == Direction.right:
stack.pop()
Expand Down
4 changes: 2 additions & 2 deletions other/linear_congruential_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class LinearCongruentialGenerator:
A pseudorandom number generator.
"""

# The default value for **seed** is the result of a function call which is not
# The default value for **seed** is the result of a function call, which is not
# normally recommended and causes ruff to raise a B008 error. However, in this case,
# it is accptable because `LinearCongruentialGenerator.__init__()` will only be
# it is acceptable because `LinearCongruentialGenerator.__init__()` will only be
# called once per instance and it ensures that each instance will generate a unique
# sequence of numbers.

Expand Down
12 changes: 5 additions & 7 deletions other/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ def random_characters(chars_incl, i):
pass # Put your code here...


# This Will Check Whether A Given Password Is Strong Or Not
# It Follows The Rule that Length Of Password Should Be At Least 8 Characters
# And At Least 1 Lower, 1 Upper, 1 Number And 1 Special Character
def is_strong_password(password: str, min_length: int = 8) -> bool:
"""
This will check whether a given password is strong or not. The password must be at
least as long as the provided minimum length, and it must contain at least 1
lowercase letter, 1 uppercase letter, 1 number and 1 special character.

>>> is_strong_password('Hwea7$2!')
True
>>> is_strong_password('Sh0r1')
Expand All @@ -81,7 +82,6 @@ def is_strong_password(password: str, min_length: int = 8) -> bool:
"""

if len(password) < min_length:
# Your Password must be at least 8 characters long
return False

upper = any(char in ascii_uppercase for char in password)
Expand All @@ -90,8 +90,6 @@ def is_strong_password(password: str, min_length: int = 8) -> bool:
spec_char = any(char in punctuation for char in password)

return upper and lower and num and spec_char
# Passwords should contain UPPERCASE, lowerase
# numbers, and special characters


def main():
Expand All @@ -104,7 +102,7 @@ def main():
"Alternative Password generated:",
alternative_password_generator(chars_incl, length),
)
print("[If you are thinking of using this passsword, You better save it.]")
print("[If you are thinking of using this password, You better save it.]")


if __name__ == "__main__":
Expand Down
Loading