Skip to content

Commit c2719f4

Browse files
committed
misspelled idx for index.
1 parent caa7678 commit c2719f4

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

graph/generate.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ def nth_product(idx, *args):
6969
total = math.prod([len(a) for a in args])
7070
if idx < 0:
7171
idx += total
72-
if index < 0 or index >= total:
73-
raise IndexError(f"Index {index} out of range")
74-
72+
if idx < 0 or idx >= total:
73+
raise IndexError(f"Index {idx} out of range")
74+
7575
elements = ()
7676
for i in range(len(args)):
7777
offset = math.prod([len(a) for a in args[i:]]) // len(args[i])
@@ -81,22 +81,26 @@ def nth_product(idx, *args):
8181
return elements
8282

8383

84-
def n_products(*args, n=20):
84+
def n_products(n, *args):
8585
"""
8686
Returns the nth product of the given iterables.
87+
88+
Args:
89+
n (int): the number of products to generate.
90+
*args: the iterables.
8791
"""
8892
if len(args) == 0:
8993
return ()
9094
if any(len(a) == 0 for a in args):
9195
raise ZeroDivisionError("Cannot generate products of empty iterables")
9296

93-
n = min(n, math.prod([len(a) for a in args]))
97+
n = min(n, int(math.prod([len(a) for a in args])))
9498
step = math.prod([len(a) for a in args]) / n
9599

96100
for ni in range(n):
97101
ix = int(step * ni + step / 2)
98102
yield nth_product(ix, *args)
99-
103+
100104

101105
def random_graph(size, degree=1.7, seed=1):
102106
if not isinstance(size, int):
@@ -111,13 +115,9 @@ def random_graph(size, degree=1.7, seed=1):
111115
rng = random.Random(seed)
112116
rng.shuffle(nodes)
113117

114-
edges = size * degree
115-
116-
gen_length = math.factorial(nodes) / math.factorial(nodes - 2)
117-
comb = int(gen_length / edges)
118+
edges = int(size * degree)
118119

119-
for i in nodes:
120-
n = i * comb
121-
a, b = nth_permutation(n, size, nodes)
120+
L = n_products(edges, nodes, nodes)
121+
for a, b in L:
122122
g.add_edge(a, b)
123123
return g

tests/test_generate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ def test_nth_p():
3939

4040
def test_nth_products():
4141
a, b, c = [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]
42-
L = list(n_products(a, b, n=10))
42+
L = list(n_products(10, a, b))
4343
assert len(L) == 10
4444
assert L == [(1, 2), (1, 6), (2, 4), (3, 1), (3, 5), (4, 2), (4, 6), (5, 4), (6, 1), (6, 5)]
4545

46-
L = list(n_products(a, b, c, n=10))
46+
L = list(n_products(10, a, b, c))
4747
assert len(L) == 10
4848
# fmt:off
4949
assert L == [(1, 2, 5), (1, 6, 3), (2, 4, 1), (3, 1, 4), (3, 5, 2), (4, 2, 5), (4, 6, 3), (5, 4, 1), (6, 1, 4), (6, 5, 2)]

0 commit comments

Comments
 (0)