def combinable_paths_maybe_loops(draw):
"""Makes single-segment paths, with loops a possibility."""
endpoints = draw(lists(ipoints, min_size=2, max_size=200, unique_by=tuple))
rand = draw(randoms())
paths = set()
point_use = collections.defaultdict(int)
target_number = len(endpoints) / 3
if target_number < 1:
target_number = 1
while len(paths) < target_number:
# Choose two points at random from the possible endpoints, and make a
# segment.
a, b = rand.sample(endpoints, k=2)
if (a, b) in paths:
continue
paths.add((a, b))
# Track how many times the points have been used.
point_use[a] += 1
point_use[b] += 1
# Any point in two segments is no longer a candidate as an endpoint.
if point_use[a] == 2:
endpoints.remove(a)
if point_use[b] == 2:
endpoints.remove(b)
return [Path(p) for p in paths]
评论列表
文章目录