mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-28 13:33:11 +08:00
Fix ty invalid assignment (#15222)
* Fix ty invalid assignment diagnostics * updating DIRECTORY.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix unused typing import * Fix gradient accumulation type handling * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix automatic differentiation gradient dtype handling --------- Co-authored-by: kadubhumika <kadubhumika@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
kadubhumika
pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
parent
62d049a60f
commit
636dd57af7
@@ -78,6 +78,7 @@ def generate_images(cells: list[list[int]], frames: int) -> list[Image.Image]:
|
||||
# Create output image
|
||||
img = Image.new("RGB", (len(cells[0]), len(cells)))
|
||||
pixels = img.load()
|
||||
assert pixels is not None
|
||||
|
||||
# Save cells to image
|
||||
for x in range(len(cells)):
|
||||
|
||||
@@ -55,6 +55,7 @@ def generate_image(cells: list[list[int]]) -> Image.Image:
|
||||
# Create the output image
|
||||
img = Image.new("RGB", (len(cells[0]), len(cells)))
|
||||
pixels = img.load()
|
||||
assert pixels is not None
|
||||
# Generates image
|
||||
for w in range(img.width):
|
||||
for h in range(img.height):
|
||||
|
||||
@@ -39,7 +39,7 @@ https://www.geeksforgeeks.org/segment-tree-efficient-implementation/
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from typing import Any, TypeVar
|
||||
from typing import TypeVar, cast
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
@@ -57,10 +57,9 @@ class SegmentTree[T]:
|
||||
... lambda a, b: (a[0] + b[0], a[1] + b[1])).query(0, 2)
|
||||
(6, 9)
|
||||
"""
|
||||
any_type: Any | T = None
|
||||
|
||||
self.N: int = len(arr)
|
||||
self.st: list[T] = [any_type for _ in range(self.N)] + arr
|
||||
self.st: list[T] = [cast(T, None) for _ in range(self.N)] + arr
|
||||
self.fn = fnc
|
||||
self.build()
|
||||
|
||||
|
||||
@@ -222,6 +222,7 @@ class BinomialHeap:
|
||||
if val < self.min_node.val:
|
||||
self.min_node = new_node
|
||||
# Put new_node as a bottom_root in heap
|
||||
assert self.bottom_root is not None
|
||||
self.bottom_root.left = new_node
|
||||
new_node.parent = self.bottom_root
|
||||
self.bottom_root = new_node
|
||||
@@ -283,6 +284,7 @@ class BinomialHeap:
|
||||
|
||||
# Update bottom root
|
||||
self.bottom_root = self.bottom_root.parent
|
||||
assert self.bottom_root is not None
|
||||
self.bottom_root.left = None
|
||||
|
||||
# Update min_node
|
||||
|
||||
@@ -2,12 +2,16 @@
|
||||
https://en.wikipedia.org/wiki/Doubly_linked_list
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, data):
|
||||
def __init__(self, data: Any):
|
||||
self.data = data
|
||||
self.previous = None
|
||||
self.next = None
|
||||
self.previous: Node | None = None
|
||||
self.next: Node | None = None
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.data}"
|
||||
@@ -15,8 +19,8 @@ class Node:
|
||||
|
||||
class DoublyLinkedList:
|
||||
def __init__(self):
|
||||
self.head = None
|
||||
self.tail = None
|
||||
self.head: Node | None = None
|
||||
self.tail: Node | None = None
|
||||
|
||||
def __iter__(self):
|
||||
"""
|
||||
@@ -93,13 +97,18 @@ class DoublyLinkedList:
|
||||
new_node.next = self.head
|
||||
self.head = new_node
|
||||
elif index == length:
|
||||
assert self.tail is not None
|
||||
self.tail.next = new_node
|
||||
assert self.tail is not None
|
||||
new_node.previous = self.tail
|
||||
self.tail = new_node
|
||||
else:
|
||||
temp = self.head
|
||||
assert temp is not None
|
||||
for _ in range(index):
|
||||
temp = temp.next
|
||||
assert temp is not None
|
||||
assert temp.previous is not None
|
||||
temp.previous.next = new_node
|
||||
new_node.previous = temp.previous
|
||||
new_node.next = temp
|
||||
@@ -141,23 +150,32 @@ class DoublyLinkedList:
|
||||
if length == 1:
|
||||
self.head = self.tail = None
|
||||
elif index == 0:
|
||||
assert self.head is not None
|
||||
self.head = self.head.next
|
||||
assert self.head is not None
|
||||
self.head.previous = None
|
||||
elif index == length - 1:
|
||||
assert self.tail is not None
|
||||
delete_node = self.tail
|
||||
self.tail = self.tail.previous
|
||||
assert self.tail is not None
|
||||
self.tail.next = None
|
||||
else:
|
||||
temp = self.head
|
||||
assert temp is not None
|
||||
for _ in range(index):
|
||||
temp = temp.next
|
||||
assert temp is not None
|
||||
delete_node = temp
|
||||
assert temp.next is not None
|
||||
assert temp.previous is not None
|
||||
temp.next.previous = temp.previous
|
||||
temp.previous.next = temp.next
|
||||
return delete_node.data
|
||||
|
||||
def delete(self, data) -> str:
|
||||
current = self.head
|
||||
assert current is not None
|
||||
|
||||
while current.data != data: # Find the position to delete
|
||||
if current.next:
|
||||
@@ -172,6 +190,8 @@ class DoublyLinkedList:
|
||||
self.delete_tail()
|
||||
|
||||
else: # Before: 1 <--> 2(current) <--> 3
|
||||
assert current.previous is not None
|
||||
assert current.next is not None
|
||||
current.previous.next = current.next # 1 --> 3
|
||||
current.next.previous = current.previous # 1 <--> 3
|
||||
return data
|
||||
|
||||
@@ -45,7 +45,7 @@ class LinkedList:
|
||||
>>> linked_list.head is None
|
||||
True
|
||||
"""
|
||||
self.head = None
|
||||
self.head: Node | None = None
|
||||
|
||||
def __iter__(self) -> Iterator[Any]:
|
||||
"""
|
||||
@@ -153,8 +153,10 @@ class LinkedList:
|
||||
if not 0 <= index < len(self):
|
||||
raise ValueError("list index out of range.")
|
||||
current = self.head
|
||||
assert current is not None
|
||||
for _ in range(index):
|
||||
current = current.next_node
|
||||
assert current is not None
|
||||
current.data = data
|
||||
|
||||
def insert_tail(self, data: Any) -> None:
|
||||
@@ -215,8 +217,10 @@ class LinkedList:
|
||||
self.head = new_node
|
||||
else:
|
||||
temp = self.head
|
||||
assert temp is not None
|
||||
for _ in range(index - 1):
|
||||
temp = temp.next_node
|
||||
assert temp is not None
|
||||
new_node.next_node = temp.next_node
|
||||
temp.next_node = new_node
|
||||
|
||||
@@ -316,10 +320,13 @@ class LinkedList:
|
||||
self.head = self.head.next_node
|
||||
else:
|
||||
temp = self.head
|
||||
assert temp is not None
|
||||
for _ in range(index - 1):
|
||||
temp = temp.next_node
|
||||
assert temp is not None
|
||||
delete_node = temp.next_node
|
||||
temp.next_node = temp.next_node.next_node
|
||||
assert delete_node is not None
|
||||
temp.next_node = delete_node.next_node
|
||||
return delete_node.data
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
|
||||
@@ -109,6 +109,7 @@ def get_image(
|
||||
"""
|
||||
img = Image.new("RGB", (image_width, image_height))
|
||||
pixels = img.load()
|
||||
assert pixels is not None
|
||||
|
||||
# loop through the image-coordinates
|
||||
for image_x in range(image_width):
|
||||
|
||||
@@ -9,7 +9,6 @@ Email: smrtpoojan@gmail.com
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from enum import Enum
|
||||
from types import TracebackType
|
||||
from typing import Any, Self
|
||||
@@ -258,7 +257,7 @@ class GradientTracker:
|
||||
"""
|
||||
|
||||
# partial derivatives with respect to target
|
||||
partial_deriv = defaultdict(lambda: 0)
|
||||
partial_deriv: dict[Variable, np.ndarray] = {}
|
||||
partial_deriv[target] = np.ones_like(target.to_ndarray())
|
||||
|
||||
# iterating through each operations in the computation graph
|
||||
@@ -270,7 +269,10 @@ class GradientTracker:
|
||||
# of variables with respect to the target
|
||||
dparam_doutput = self.derivative(param, operation)
|
||||
dparam_dtarget = dparam_doutput * partial_deriv[operation.output]
|
||||
partial_deriv[param] += dparam_dtarget
|
||||
partial_deriv[param] = (
|
||||
partial_deriv.get(param, np.zeros_like(dparam_dtarget))
|
||||
+ dparam_dtarget
|
||||
)
|
||||
|
||||
if param.result_of and param.result_of != OpType.NOOP:
|
||||
operation_queue.append(param.result_of)
|
||||
|
||||
@@ -71,7 +71,7 @@ def mincut(graph: list[list[int]], source: int, sink: int) -> list[tuple[int, in
|
||||
parent = [-1] * (len(residual))
|
||||
res = []
|
||||
while bfs(residual, source, sink, parent):
|
||||
path_flow = float("inf")
|
||||
path_flow = max(max(row) for row in residual)
|
||||
s = sink
|
||||
|
||||
while s != source:
|
||||
|
||||
@@ -21,6 +21,7 @@ import gzip
|
||||
import os
|
||||
import typing
|
||||
import urllib
|
||||
import urllib.request
|
||||
|
||||
import numpy as np
|
||||
from tensorflow.python.framework import dtypes, random_seed
|
||||
|
||||
@@ -192,7 +192,6 @@ environment.python-version = "3.14"
|
||||
rules.call-non-callable = "ignore"
|
||||
rules.deprecated = "ignore"
|
||||
rules.invalid-argument-type = "ignore"
|
||||
rules.invalid-assignment = "ignore"
|
||||
rules.invalid-parameter-default = "ignore"
|
||||
rules.invalid-return-type = "ignore"
|
||||
rules.invalid-type-arguments = "ignore"
|
||||
|
||||
@@ -334,7 +334,10 @@ class CPUSchedulerGUI:
|
||||
def delete_process(self) -> None:
|
||||
"""Deletes a selected process."""
|
||||
if sel := self.tree.selection():
|
||||
pid = self.tree.item(sel[0])["values"][0]
|
||||
values = self.tree.item(sel[0])["values"]
|
||||
if not values:
|
||||
return
|
||||
pid = values[0]
|
||||
self.processes = [p for p in self.processes if p["pid"] != pid]
|
||||
self.tree.delete(sel[0])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user