Compare commits

..

4 Commits

Author SHA1 Message Date
Minha, Jeong
f8fe72dc37
Update game_of_life.py (#4921)
* Update game_of_life.py

docstring error fix
delete no reason delete next_gen_canvas code(local variable)

* Update cellular_automata/game_of_life.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-07-31 14:24:12 -07:00
Tianyi Zheng
5cf34d901e
Ruff fixes (#8913)
* updating DIRECTORY.md

* Fix ruff error in eulerian_path_and_circuit_for_undirected_graph.py

* Fix ruff error in newtons_second_law_of_motion.py

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
2023-07-31 13:53:26 -07:00
Dylan Buchi
90a8e6e0d2
Update sorts/bubble_sort.py (#5802)
* Add missing type annotations in bubble_sort.py

* Refactor bubble_sort function
2023-07-31 11:50:00 -07:00
roger-sato
0b0214c42f
Handle empty input case in Segment Tree build process (#8718) 2023-07-31 11:46:30 -07:00
6 changed files with 16 additions and 14 deletions

View File

@ -236,8 +236,8 @@
* [Double Ended Queue](data_structures/queue/double_ended_queue.py) * [Double Ended Queue](data_structures/queue/double_ended_queue.py)
* [Linked Queue](data_structures/queue/linked_queue.py) * [Linked Queue](data_structures/queue/linked_queue.py)
* [Priority Queue Using List](data_structures/queue/priority_queue_using_list.py) * [Priority Queue Using List](data_structures/queue/priority_queue_using_list.py)
* [Queue By List](data_structures/queue/queue_by_list.py)
* [Queue By Two Stacks](data_structures/queue/queue_by_two_stacks.py) * [Queue By Two Stacks](data_structures/queue/queue_by_two_stacks.py)
* [Queue On List](data_structures/queue/queue_on_list.py)
* [Queue On Pseudo Stack](data_structures/queue/queue_on_pseudo_stack.py) * [Queue On Pseudo Stack](data_structures/queue/queue_on_pseudo_stack.py)
* Stacks * Stacks
* [Balanced Parentheses](data_structures/stacks/balanced_parentheses.py) * [Balanced Parentheses](data_structures/stacks/balanced_parentheses.py)

View File

@ -10,7 +10,7 @@ Python:
- 3.5 - 3.5
Usage: Usage:
- $python3 game_o_life <canvas_size:int> - $python3 game_of_life <canvas_size:int>
Game-Of-Life Rules: Game-Of-Life Rules:
@ -52,7 +52,8 @@ def seed(canvas: list[list[bool]]) -> None:
def run(canvas: list[list[bool]]) -> list[list[bool]]: def run(canvas: list[list[bool]]) -> list[list[bool]]:
"""This function runs the rules of game through all points, and changes their """
This function runs the rules of game through all points, and changes their
status accordingly.(in the same canvas) status accordingly.(in the same canvas)
@Args: @Args:
-- --
@ -60,7 +61,7 @@ def run(canvas: list[list[bool]]) -> list[list[bool]]:
@returns: @returns:
-- --
None canvas of population after one step
""" """
current_canvas = np.array(canvas) current_canvas = np.array(canvas)
next_gen_canvas = np.array(create_canvas(current_canvas.shape[0])) next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
@ -70,10 +71,7 @@ def run(canvas: list[list[bool]]) -> list[list[bool]]:
pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2] pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2]
) )
current_canvas = next_gen_canvas return next_gen_canvas.tolist()
del next_gen_canvas # cleaning memory as we move on.
return_canvas: list[list[bool]] = current_canvas.tolist()
return return_canvas
def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool: def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:

View File

@ -7,7 +7,8 @@ class SegmentTree:
self.st = [0] * ( self.st = [0] * (
4 * self.N 4 * self.N
) # approximate the overall size of segment tree with array N ) # approximate the overall size of segment tree with array N
self.build(1, 0, self.N - 1) if self.N:
self.build(1, 0, self.N - 1)
def left(self, idx): def left(self, idx):
return idx * 2 return idx * 2

View File

@ -20,7 +20,7 @@ def check_circuit_or_path(graph, max_node):
odd_degree_nodes = 0 odd_degree_nodes = 0
odd_node = -1 odd_node = -1
for i in range(max_node): for i in range(max_node):
if i not in graph.keys(): if i not in graph:
continue continue
if len(graph[i]) % 2 == 1: if len(graph[i]) % 2 == 1:
odd_degree_nodes += 1 odd_degree_nodes += 1

View File

@ -60,7 +60,7 @@ def newtons_second_law_of_motion(mass: float, acceleration: float) -> float:
>>> newtons_second_law_of_motion(2.0, 1) >>> newtons_second_law_of_motion(2.0, 1)
2.0 2.0
""" """
force = float() force = 0.0
try: try:
force = mass * acceleration force = mass * acceleration
except Exception: except Exception:

View File

@ -1,4 +1,7 @@
def bubble_sort(collection): from typing import Any
def bubble_sort(collection: list[Any]) -> list[Any]:
"""Pure implementation of bubble sort algorithm in Python """Pure implementation of bubble sort algorithm in Python
:param collection: some mutable ordered collection with heterogeneous :param collection: some mutable ordered collection with heterogeneous
@ -28,9 +31,9 @@ def bubble_sort(collection):
True True
""" """
length = len(collection) length = len(collection)
for i in range(length - 1): for i in reversed(range(length)):
swapped = False swapped = False
for j in range(length - 1 - i): for j in range(i):
if collection[j] > collection[j + 1]: if collection[j] > collection[j + 1]:
swapped = True swapped = True
collection[j], collection[j + 1] = collection[j + 1], collection[j] collection[j], collection[j + 1] = collection[j + 1], collection[j]