-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmall-groups.py
106 lines (91 loc) · 2.82 KB
/
small-groups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from os import name, system
import networkx as nx
import numpy as np
# Requirements.
# Python 3.9.2
# newtorkx-2.5 [pip install networkx]
# numpy-1.20.1 [pip install numpy]
# Person class with init, repr, and str.
class Person:
def __init__(self, name, couple):
self.name = name
self.couple = couple
self.busy = False
self.hosting = []
self.host_count = 0
def __repr__(self):
return repr(self.name)
def __str__(self):
return self.name
def hosts(self, visitor):
# Make this household a host if it isn't already.
if (self.host_count == 0):
self.hosting.append(self)
self.host_count += 1
if self.couple:
self.host_count += 1
# Host the current household s.
visitor.busy = True
self.hosting.append(visitor)
self.host_count += 1
if visitor.couple:
self.host_count += 1
def reset(self):
self.busy = False
self.hosting = []
self.host_count = 0
# Defining variables.
G = nx.DiGraph()
week = 1
repeat_visits = []
file_name = input("Please enter your plaintext file with smallgroup names: ")
open(file_name, "r")
# Reading the input file.
with open(file_name) as file:
lines = file.read().splitlines()
for L in lines:
L = L.replace(", ", " + ")
if "+" in L:
P = Person(L, True)
else:
P = Person(L, False)
G.add_node(P)
group_size = int(input("What is the group size? (no more than half # of people)"))
print("\n\n")
# Build every edge of the complete graph.
for s in G.nodes():
for v in G.nodes():
if s != v:
G.add_edge(s, v)
# Iterate each week until there are no more connecting edges.
while G.number_of_edges() > 0:
print("Week", week)
# Finding required connections.
for s in G.nodes():
if s.hosting:
continue
for v in G.adj[s]:
if not v.busy and v.host_count < group_size:
v.hosts(s)
G.remove_edge(s, v)
break
# Assigning repeated visitors (not required connections).
for s in G.nodes():
if not s.busy and not s.hosting:
for v in G.nodes():
if s != v and not v.busy and v.host_count < group_size:
v.hosts(s)
repeat_visits.append(s)
break
# Showing the hosts for this week.
for s in G.nodes():
if (s.hosting):
print(s.hosting[0], s.hosting[1:])
s.reset()
# Show repeat visitors (to verify that the algorithm is efficient).
if (repeat_visits):
print("Repeats:", repeat_visits)
# Increment the week counter.
week += 1
repeat_visits = []
print("\n")