Skip to content
Open
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
29 changes: 29 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Python application

on: [push]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.13
uses: actions/setup-python@v3
with:
python-version: "3.13"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff
pip install pytest
- name: Lint with ruff
run: |
ruff check --output-format=github
- name: Test with pytest
run: |
pytest
39 changes: 39 additions & 0 deletions hw_10/dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Graph:
def __init__(self, vertices: list[int], edges: list[tuple[int, int]]) -> None:
self.vertices = vertices
self.edges = edges
self.visited_order = []

def __iter__(self):
if not self.visited_order:
self.dfs()
return iter(self.visited_order)

def dfs(self) -> list[int]:
states = {"w": [v for v in self.vertices], "g": list(), "b": list()}

def dfs_step(vertex):
if vertex in states["w"]:
states["g"].append(vertex)
states["w"].remove(vertex)
for edge in self.edges:
if edge[0] == vertex:
dfs_step(edge[1])
if edge[1] == vertex:
dfs_step(edge[0])

for vertex in self.vertices:
dfs_step(vertex)

self.visited_order = states["g"]
return self.visited_order


g = Graph([1, 2, 3], [(1, 3), (2, 3), (2, 1)])

a = g.dfs()
b = list(iter(g))
print(a, b)

Comment on lines +32 to +37
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это последняя домашка курса. Как можно было не научиться прятать такое за if __name__ == "__main__" хотя бы?



11 changes: 11 additions & 0 deletions hw_10/test_dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from dfs import Graph

graph = Graph([1, 2, 3], [(1, 3), (2, 3), (2, 1)])

def test_1():
g = graph.dfs()
assert g == [1, 3, 2]

def test_2():
g = list(iter(graph))
assert g == [1, 3, 2]