Skip to content

Commit

Permalink
Merge branch 'master' into pr/AlfredChester/114
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Python-in-China committed Oct 2, 2024
2 parents 1a9744c + 8027e5a commit 40e7247
Show file tree
Hide file tree
Showing 15 changed files with 688 additions and 375 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install poetry
uses: abatilo/actions-poetry@v2
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
run: poetry install
- name: Build package
run: python -m build
run: poetry build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
config.py
*.in
*.out
*.exe

# Created by .ignore support plugin (hsz.mobi)
### Python template
Expand Down Expand Up @@ -133,4 +134,7 @@ target/
# Pycharm
venv

*.DS_Store
*.DS_Store

# VS Code
.vscode
3 changes: 3 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[MASTER]
py-version=3.5
disable=R0902,R0913,R0917
22 changes: 12 additions & 10 deletions cyaron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
"""

from __future__ import absolute_import

from random import choice, randint, random, randrange, uniform

#from .visual import visualize
from . import log
from .compare import Compare
from .consts import *
from .graph import Edge, Graph
from .io import IO
from .graph import Graph, Edge
from .string import String
from .math import *
from .merger import Merger
from .polygon import Polygon
from .sequence import Sequence
from .string import String
from .utils import *
from .consts import *
from .vector import Vector
from .polygon import Polygon
from .compare import Compare
from .math import *
from .merger import Merger
#from .visual import visualize
from . import log
from random import randint, randrange, uniform, choice, random
3 changes: 2 additions & 1 deletion cyaron/compare.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, print_function
from cyaron import IO, log
from .io import IO
from . import log
from cyaron.utils import *
from cyaron.consts import *
from cyaron.graders import CYaRonGraders
Expand Down
55 changes: 52 additions & 3 deletions cyaron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def __init__(self, point_count, directed=False):
"""
self.directed = directed
self.edges = [[] for i in range(point_count + 1)]

def edge_count(self):
"""edge_count(self) -> int
Return the count of the edges in the graph.
"""
cnt = sum(len(node) for node in self.edges)
if not self.directed:
cnt //= 2
return cnt

def to_str(self, **kwargs):
"""to_str(self, **kwargs) -> str
Expand Down Expand Up @@ -146,6 +155,9 @@ def tree(point_count, chain=0, flower=0, **kwargs):
int/float weight_gen()
= lambda: random.randint(weight_limit[0], weight_limit[1])
-> the generator of the weights. It should return the weight. The default way is to use the random.randint()
int father_gen(cur)
= lambda cur: random.randrange(1, cur)
-> the generator of the fathers of current point.
"""
directed = kwargs.get("directed", False)
weight_limit = kwargs.get("weight_limit", (1, 1))
Expand All @@ -154,6 +166,7 @@ def tree(point_count, chain=0, flower=0, **kwargs):
weight_gen = kwargs.get(
"weight_gen", lambda: random.randint(
weight_limit[0], weight_limit[1]))
father_gen = kwargs.get("father_gen", lambda cur: random.randrange(1, cur))

if not 0 <= chain <= 1 or not 0 <= flower <= 1:
raise Exception("chain and flower must be between 0 and 1")
Expand All @@ -174,7 +187,7 @@ def tree(point_count, chain=0, flower=0, **kwargs):
for i in range(chain_count + 2, chain_count + flower_count + 2):
graph.add_edge(1, i, weight=weight_gen())
for i in range(point_count - random_count + 1, point_count + 1):
u = random.randrange(1, i)
u = father_gen(i)
graph.add_edge(u, i, weight=weight_gen())

return graph
Expand Down Expand Up @@ -256,6 +269,11 @@ def graph(point_count, edge_count, **kwargs):
directed = kwargs.get("directed", False)
self_loop = kwargs.get("self_loop", True)
repeated_edges = kwargs.get("repeated_edges", True)
if not repeated_edges:
max_edge = Graph._calc_max_edge(point_count, directed, self_loop)
if edge_count > max_edge:
raise Exception("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count, max_edge))

weight_limit = kwargs.get("weight_limit", (1, 1))
if not list_like(weight_limit):
weight_limit = (1, weight_limit)
Expand Down Expand Up @@ -286,7 +304,7 @@ def graph(point_count, edge_count, **kwargs):
@staticmethod
def DAG(point_count, edge_count, **kwargs):
"""DAG(point_count, edge_count, **kwargs) -> Graph
Factory method. Return a graph with point_count vertexes and edge_count edges.
Factory method. Return a directed connected graph with point_count vertexes and edge_count edges.
int point_count -> the count of vertexes
int edge_count -> the count of edges
**kwargs(Keyword args):
Expand All @@ -305,6 +323,11 @@ def DAG(point_count, edge_count, **kwargs):
self_loop = kwargs.get("self_loop", False) # DAG default has no loop
repeated_edges = kwargs.get("repeated_edges", True)
loop = kwargs.get("loop", False)
if not repeated_edges:
max_edge = Graph._calc_max_edge(point_count, not loop, self_loop)
if edge_count > max_edge:
raise Exception("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count, max_edge))

weight_limit = kwargs.get("weight_limit", (1, 1))
if not list_like(weight_limit):
weight_limit = (1, weight_limit)
Expand Down Expand Up @@ -348,7 +371,7 @@ def DAG(point_count, edge_count, **kwargs):
@staticmethod
def UDAG(point_count, edge_count, **kwargs):
"""UDAG(point_count, edge_count, **kwargs) -> Graph
Factory method. Return a graph with point_count vertexes and edge_count edges.
Factory method. Return a undirected connected graph with point_count vertexes and edge_count edges.
int point_count -> the count of vertexes
int edge_count -> the count of edges
**kwargs(Keyword args):
Expand All @@ -365,6 +388,11 @@ def UDAG(point_count, edge_count, **kwargs):

self_loop = kwargs.get("self_loop", True)
repeated_edges = kwargs.get("repeated_edges", True)
if not repeated_edges:
max_edge = Graph._calc_max_edge(point_count, False, self_loop)
if edge_count > max_edge:
raise Exception("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count, max_edge))

weight_limit = kwargs.get("weight_limit", (1, 1))
if not list_like(weight_limit):
weight_limit = (1, weight_limit)
Expand Down Expand Up @@ -398,6 +426,18 @@ def UDAG(point_count, edge_count, **kwargs):
i += 1

return graph

@staticmethod
def connected(point_count, edge_count, directed=False, **kwargs):
"""connected(point_count, edge_count, **kwargs) -> Graph
Factory method. Return a connected graph with point_count vertexes
int point_count -> the count of vertexes
bool directed -> whether the graph is directed
"""
if directed:
return Graph.DAG(point_count, edge_count, **kwargs)
else:
return Graph.UDAG(point_count, edge_count, **kwargs)

@staticmethod
def hack_spfa(point_count, **kwargs):
Expand Down Expand Up @@ -446,3 +486,12 @@ def hack_spfa(point_count, **kwargs):
graph.add_edge(u, v, weight=weight_gen())

return graph

@staticmethod
def _calc_max_edge(point_count, directed, self_loop):
max_edge = point_count * (point_count - 1)
if not directed:
max_edge //= 2
if self_loop:
max_edge += point_count
return max_edge
Loading

0 comments on commit 40e7247

Please sign in to comment.