Skip to content

[pull] master from TheAlgorithms:master #24

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 4 commits into from
Apr 3, 2024
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
4 changes: 2 additions & 2 deletions compression/huffman.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def build_tree(letters: list[Letter]) -> Letter | TreeNode:
Run through the list of Letters and build the min heap
for the Huffman Tree.
"""
response: list[Letter | TreeNode] = letters # type: ignore
response: list[Letter | TreeNode] = list(letters)
while len(response) > 1:
left = response.pop(0)
right = response.pop(0)
Expand All @@ -59,7 +59,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
if isinstance(root, Letter):
root.bitstring[root.letter] = bitstring
return [root]
treenode: TreeNode = root # type: ignore
treenode: TreeNode = root
letters = []
letters += traverse_tree(treenode.left, bitstring + "0")
letters += traverse_tree(treenode.right, bitstring + "1")
Expand Down
Empty file.
6 changes: 3 additions & 3 deletions data_structures/binary_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ def remove(self, value: int) -> None:
predecessor = self.get_max(
node.left
) # Gets the max value of the left branch
self.remove(predecessor.value) # type: ignore
self.remove(predecessor.value) # type: ignore[union-attr]
node.value = (
predecessor.value # type: ignore
predecessor.value # type: ignore[union-attr]
) # Assigns the value to the node to delete and keep tree structure

def preorder_traverse(self, node: Node | None) -> Iterable:
Expand Down Expand Up @@ -336,7 +336,7 @@ def inorder(curr_node: Node | None) -> list[Node]:
"""
node_list = []
if curr_node is not None:
node_list = inorder(curr_node.left) + [curr_node] + inorder(curr_node.right)
node_list = [*inorder(curr_node.left), curr_node, *inorder(curr_node.right)]
return node_list


Expand Down
Empty file.
2 changes: 1 addition & 1 deletion data_structures/linked_list/rotate_to_the_right.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def insert_node(head: Node | None, data: int) -> Node:
while temp_node.next_node:
temp_node = temp_node.next_node

temp_node.next_node = new_node # type: ignore
temp_node.next_node = new_node
return head


Expand Down
Empty file.
2 changes: 1 addition & 1 deletion dynamic_programming/subset_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def subset_combinations(elements: list[int], n: int) -> list:
for i in range(1, r + 1):
for j in range(i, 0, -1):
for prev_combination in dp[j - 1]:
dp[j].append(tuple(prev_combination) + (elements[i - 1],))
dp[j].append((*prev_combination, elements[i - 1]))

try:
return sorted(dp[n])
Expand Down
Empty file added electronics/__init__.py
Empty file.
3 changes: 1 addition & 2 deletions electronics/circular_convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def circular_convolution(self) -> list[float]:
using matrix method

Usage:
>>> import circular_convolution as cc
>>> convolution = cc.CircularConvolution()
>>> convolution = CircularConvolution()
>>> convolution.circular_convolution()
[10, 10, 6, 14]

Expand Down
Empty file added fractals/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion fractals/mandelbrot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import colorsys

from PIL import Image # type: ignore
from PIL import Image


def get_distance(x: float, y: float, max_step: int) -> float:
Expand Down
Empty file added geometry/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion graphics/bezier_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# https://www.tutorialspoint.com/computer_graphics/computer_graphics_curves.htm
from __future__ import annotations

from scipy.special import comb # type: ignore
from scipy.special import comb


class BezierCurve:
Expand Down
Empty file added greedy_methods/__init__.py
Empty file.
Empty file.
Empty file added linear_programming/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions maths/entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def analyze_text(text: str) -> tuple[dict, dict]:
The first dictionary stores the frequency of single character strings.
The second dictionary stores the frequency of two character strings.
"""
single_char_strings = Counter() # type: ignore
two_char_strings = Counter() # type: ignore
single_char_strings = Counter() # type: ignore[var-annotated]
two_char_strings = Counter() # type: ignore[var-annotated]
single_char_strings[text[-1]] += 1

# first case when we have space at start.
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion maths/odd_sieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def odd_sieve(num: int) -> list[int]:
0, ceil((num - i_squared) / (i << 1))
)

return [2] + list(compress(range(3, num, 2), sieve))
return [2, *list(compress(range(3, num, 2), sieve))]


if __name__ == "__main__":
Expand Down
Empty file.
4 changes: 3 additions & 1 deletion matrix/spiral_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ def spiral_traversal(matrix: list[list]) -> list[int]:
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([])
"""
if matrix:
return list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) # type: ignore
return list(matrix.pop(0)) + spiral_traversal(
[list(row) for row in zip(*matrix)][::-1]
)
else:
return []

Expand Down
2 changes: 1 addition & 1 deletion matrix/tests/test_matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys

import numpy as np
import pytest # type: ignore
import pytest

# Custom/local libraries
from matrix import matrix_operation as matop
Expand Down
Empty file.
3 changes: 2 additions & 1 deletion neural_network/activation_functions/mish.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"""

import numpy as np
from softplus import softplus

from .softplus import softplus


def mish(vector: np.ndarray) -> np.ndarray:
Expand Down
2 changes: 1 addition & 1 deletion project_euler/problem_092/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def chain(number: int) -> bool:
"""

if CHAINS[number - 1] is not None:
return CHAINS[number - 1] # type: ignore
return CHAINS[number - 1] # type: ignore[return-value]

number_chain = chain(next_number(number))
CHAINS[number - 1] = number_chain
Expand Down
2 changes: 1 addition & 1 deletion project_euler/problem_104/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import sys

sys.set_int_max_str_digits(0) # type: ignore
sys.set_int_max_str_digits(0)


def check(number: int) -> bool:
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ lint.ignore = [ # `ruff rule S101` for a description of that rule
"EM101", # Exception must not use a string literal, assign to variable first
"EXE001", # Shebang is present but file is not executable" -- FIX ME
"G004", # Logging statement uses f-string
"INP001", # File `x/y/z.py` is part of an implicit namespace package. Add an `__init__.py`. -- FIX ME
"PGH003", # Use specific rule codes when ignoring type issues -- FIX ME
"PLC1901", # `{}` can be simplified to `{}` as an empty string is falsey
"PLW060", # Using global for `{name}` but no assignment is done -- DO NOT FIX
"PLW2901", # PLW2901: Redefined loop variable -- FIX ME
"PT011", # `pytest.raises(Exception)` is too broad, set the `match` parameter or use a more specific exception
"PT018", # Assertion should be broken down into multiple parts
"RUF00", # Ambiguous unicode character and other rules
"RUF001", # String contains ambiguous {}. Did you mean {}?
"RUF002", # Docstring contains ambiguous {}. Did you mean {}?
"RUF003", # Comment contains ambiguous {}. Did you mean {}?
"RUF007", # Prefer itertools.pairwise() over zip() when iterating over successive pairs
"S101", # Use of `assert` detected -- DO NOT FIX
"S105", # Possible hardcoded password: 'password'
"S113", # Probable use of requests call without timeout -- FIX ME
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes -- FIX ME
"SLF001", # Private member accessed: `_Iterator` -- FIX ME
Expand Down
2 changes: 1 addition & 1 deletion scripts/validate_filenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
try:
from .build_directory_md import good_file_paths
except ImportError:
from build_directory_md import good_file_paths # type: ignore
from build_directory_md import good_file_paths # type: ignore[no-redef]

filepaths = list(good_file_paths())
assert filepaths, "good_file_paths() failed!"
Expand Down
6 changes: 3 additions & 3 deletions scripts/validate_solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
def convert_path_to_module(file_path: pathlib.Path) -> ModuleType:
"""Converts a file path to a Python module"""
spec = importlib.util.spec_from_file_location(file_path.name, str(file_path))
module = importlib.util.module_from_spec(spec) # type: ignore
spec.loader.exec_module(module) # type: ignore
module = importlib.util.module_from_spec(spec) # type: ignore[arg-type]
spec.loader.exec_module(module) # type: ignore[union-attr]
return module


Expand Down Expand Up @@ -92,7 +92,7 @@ def test_project_euler(solution_path: pathlib.Path) -> None:
problem_number: str = solution_path.parent.name[8:].zfill(3)
expected: str = PROBLEM_ANSWERS[problem_number]
solution_module = convert_path_to_module(solution_path)
answer = str(solution_module.solution()) # type: ignore
answer = str(solution_module.solution())
answer = hashlib.sha256(answer.encode()).hexdigest()
assert (
answer == expected
Expand Down
2 changes: 1 addition & 1 deletion web_programming/covid_stats_via_xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import NamedTuple

import requests
from lxml import html # type: ignore
from lxml import html


class CovidData(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion web_programming/recaptcha_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

def login_using_recaptcha(request):
# Enter your recaptcha secret key here
secret_key = "secretKey"
secret_key = "secretKey" # noqa: S105
url = "https://www.google.com/recaptcha/api/siteverify"

# when method is not POST, direct user to login page
Expand Down