Программа распределения учасников по столам
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
12 KiB

2 years ago
<!-- Copyright 2022 by Alexei Bezborodov <AlexeiBv@narod.ru> -->
2 years ago
<!-- public domain -->
2 years ago
<html lang="ru">
2 years ago
<head>
2 years ago
<meta charset="UTF-8">
2 years ago
<title>Распределение участников по столам</title>
2 years ago
<!--
2 years ago
Загрузка скриптов из интернета
2 years ago
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.8.9/brython.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.8.9/brython_stdlib.js"></script>
-->
2 years ago
<!--
Загрузка скриптов из локальной папки (рядом с файлом html)
-->
2 years ago
<script type="text/javascript" src="brython.min.js"></script>
<script type="text/javascript" src="brython_stdlib.js"></script>
</head>
<body onload="brython()">
2 years ago
<details open>
2 years ago
<summary>Начальные данные</summary>
<div>
<p>
<label>Номер шага:</label>
<input id = "step_id" type="number" value="0" step="1"/>
</p>
<p>
2 years ago
<label>Входные данные:<br/></label>
<textarea id="input_data" cols="40" rows="5"></textarea>
<p>Данные должны быть в таком формате:<br/>
2 years ago
1; ФИО 1; {3,4}<br/>
2; ФИО 2; {3,4}<br/>
3; ФИО 3; {1,2}<br/>
4; ФИО 4; {1,2}<br/>
2 years ago
, где первое число - номер посетителя<br/>
второе - имя посетителя<br/>
третье - номера участников, с которыми посетитель уже знаком<br/>
</p>
</p>
2 years ago
</div>
<div>
<button id="load_data">Загрузить</button>
2 years ago
</div>
</details>
<details open>
<summary>Таблица знакомств</summary>
2 years ago
<div>
<p id="meet_table" class="text-center"></p>
2 years ago
</div>
</details>
2 years ago
<details>
2 years ago
<summary>Расстановка</summary>
<div>
2 years ago
<p>
<label>Размеры столов (через запятую):</label>
<input id = "table_size"/>
2 years ago
</p>
<p>
<label>Имена столов:</label>
<input id = "table_name" value="АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"/>
</p>
2 years ago
<textarea id="result_table_txt" cols="40" rows="5"></textarea>
2 years ago
</div>
<div>
<button id="table_distrib">Расставить по столам</button>
2 years ago
<button id="table_distrib_load">Загрузить расстановку</button>
2 years ago
</div>
</details>
<details>
2 years ago
<summary>Таблица</summary>
<div>
<p id="result_table" class="text-center"></p>
</div>
</details>
<details>
2 years ago
<summary>Конечные данные</summary>
<label>Входные данные для нового шага:<br/></label>
2 years ago
<textarea id="output_data" cols="40" rows="5"></textarea>
<div>
<button id="next_step">Перейти на следующий шаг</button>
2 years ago
</div>
</details>
<details>
<summary>Полная таблица всех шагов</summary>
<label>Входные данные:<br/></label>
<textarea id="all_output_data" cols="40" rows="25"></textarea>
</details>
2 years ago
<script type="text/python">
from browser import document, html, window
import random
class Table:
def __init__(self, id, person_set):
self.id = id
self.person_set = person_set
id = 0
person_set = set()
class Person:
def __init__(self, beig_id, name, meet_set):
self.beig_id = beig_id
self.name = name
self.meet_set = meet_set
def __str__(self):
return str(self.beig_id) + ", " + self.name + "\n" + str(self.meet_set)
beig_id = 0
name = ""
meet_set = set()
global persons
persons = []
global tables
tables = []
def add_to_all_data(val):
document["all_output_data"].value += "\n\n#Шаг " + document["step_id"].value + "\n" + val
2 years ago
def load_input(val):
global persons
persons = []
input_data = val
for p in input_data.split("\n"):
if p == "":
continue
tmp = p.split(";")
cur_beig_id = int(tmp[0])
cur_name = ""
if len(tmp) > 1:
cur_name = tmp[1]
str_meet_list = []
if len(tmp) > 2:
str_meet_list = tmp[2].replace(" ", "").replace("{", "").replace("}", "").split(",")
cur_meet_list = set()
for s in str_meet_list:
if len(s) != 0:
cur_meet_list = cur_meet_list | {int(s)}
p = Person(cur_beig_id,cur_name, cur_meet_list)
persons.append(p)
def get_present_persons():
global persons
return list(filter(lambda p : p.beig_id > 0, persons))
2 years ago
def show_meet_table_data():
global persons
2 years ago
html = "Всего участников: " + str(len(persons)) + "<br/>"
html += "Присутсвуют участников: " + str(len(get_present_persons())) + "<br/>"
html += "Шаг: " + document["step_id"].value + "<br/>"
meet_count = 0;
2 years ago
html += "<table border = 1>"
# Первая строка
html += "<tr>"
html += "<td></td>"
for p in persons:
html += "<th" + " title='"+ p.name + "'>" + str(abs(p.beig_id)) + "</th>"
2 years ago
html += "</tr>"
# Остальные строки
for p in persons:
html += "<tr>"
html += "<th" + " title='"+ p.name + "'>" + str(abs(p.beig_id)) + "</th>"
2 years ago
for ip in persons:
if abs(p.beig_id) in ip.meet_set:
2 years ago
html += "<td bgcolor='red'></td>"
meet_count += 1
2 years ago
else:
html += "<td></td>"
html += "</tr>"
html += "</table>"
html += "<br/>Знакомств: " + str(meet_count) + "/" + str(len(persons)**2) + "<br/>"
2 years ago
document["meet_table"].innerHTML = html
def get_result():
global persons
result = ""
for p in persons:
2 years ago
result += str(p.beig_id) + ";" + p.name + ";"
if len(p.meet_set):
result += str(p.meet_set)
result += "\n"
2 years ago
return result
def result_table_fill():
global tables
result_txt = ""
for t in tables:
result_txt += str(t.id) + ";"
if len(t.person_set):
result_txt += str(t.person_set)
result_txt += "\n"
return result_txt
2 years ago
def print_result_table():
table_names = document["table_name"].value
2 years ago
global tables
html = ""
html += "<table border = 1>"
# Первая строка
html += "<tr>"
html += "<th>Номер стола</th>"
html += "<th>Участники</th>"
html += "</tr>"
# Остальные строки
for t in tables:
html += "<tr>"
if t.id >= len(table_names):
html += "<td>" + str(t.id) + "</td>"
else:
html += "<td>" + str(table_names[t.id]) + "</td>"
2 years ago
if len(t.person_set):
2 years ago
html += "<td>" + str(t.person_set).replace("{","").replace("}","") + "</td>"
2 years ago
else:
html += "<td></td>"
2 years ago
html += "</tr>"
html += "</table>"
document["result_table"].innerHTML = html
document["result_table_txt"].value = result_table_fill()
2 years ago
2 years ago
def get_person_by_id(id):
global persons
for p in persons:
if abs(p.beig_id) == id:
2 years ago
return p
print("Error", id)
def fill_next_table(start_person_id, persons_set, table_size_count, persons):
2 years ago
result_persons = [get_person_by_id(start_person_id)]
result_set = {start_person_id}
while(len(result_set) < table_size_count):
search_succes = False
for p in persons:
cur_id = abs(p.beig_id)
2 years ago
if cur_id in persons_set:
continue
if cur_id in result_set:
continue
pers_free = True
for ip in result_persons:
if cur_id in ip.meet_set:
pers_free = False
break
if pers_free:
result_set = result_set | {cur_id}
result_persons.append(p)
search_succes = True
break
if not search_succes:
for p in persons:
cur_id = abs(p.beig_id)
2 years ago
if cur_id in persons_set:
continue
if cur_id in result_set:
continue
result_set = result_set | {cur_id}
result_persons.append(p)
break
return result_set
def get_beig_id_set(persons):
2 years ago
result = set()
for p in persons:
result = result | {abs(p.beig_id)}
2 years ago
return result
def fill_tables():
global tables
tables = []
cur_table_id = 0
sort_persons = sorted(get_present_persons(), key=lambda person: len(person.meet_set))
all_table_size = 0
str_table_size = document["table_size"].value.replace(" ", "").split(",")
table_size = []
for s in str_table_size:
if len(s) != 0:
t = int(s)
all_table_size += t
table_size += [t]
table_count = len(table_size);
beig_id_set = get_beig_id_set(sort_persons)
2 years ago
persons_set = set()
while len(persons_set) != len(sort_persons):
if len(persons_set) >= all_table_size:
2 years ago
break
new_person_id = random.sample(list(beig_id_set - persons_set), 1)[0]
2 years ago
cur_set = fill_next_table(new_person_id, persons_set, min(table_size[cur_table_id], len(sort_persons) - len(persons_set)), sort_persons)
2 years ago
persons_set = persons_set | cur_set
tables.append(Table(cur_table_id, cur_set))
cur_table_id += 1
def fill_persons():
global persons
global tables
for t in tables:
2 years ago
for p in persons:
if abs(p.beig_id) in t.person_set:
2 years ago
p.meet_set = t.person_set | p.meet_set
2 years ago
def load_input_table(val):
global tables
tables = []
input_data = val
for t in input_data.split("\n"):
if t == "":
continue
tmp = t.split(";")
cur_id = int(tmp[0])
str_meet_list = []
if len(tmp) > 1:
str_meet_list = tmp[1].replace(" ", "").replace("{", "").replace("}", "").split(",")
cur_meet_list = set()
for s in str_meet_list:
if len(s) != 0:
cur_meet_list = cur_meet_list | {int(s)}
t = Table(cur_id, cur_meet_list)
tables.append(t)
2 years ago
def make_result():
fill_tables()
fill_persons()
def load_data(event):
val = document["input_data"].value
load_input(val)
add_to_all_data("#Таблица знакомств\n" + val)
2 years ago
show_meet_table_data()
def table_distrib(event):
load_input(document["input_data"].value)
2 years ago
make_result()
print_result_table()
document["output_data"].value = get_result()
show_meet_table_data()
2 years ago
2 years ago
def table_distrib_load(event):
load_input(document["input_data"].value)
2 years ago
load_input_table(document["result_table_txt"].value)
fill_persons()
2 years ago
print_result_table()
2 years ago
document["output_data"].value = get_result()
show_meet_table_data()
2 years ago
def next_step(event):
document["step_id"].value = str(int(document["step_id"].value) + 1)
val = document["output_data"].value
document["input_data"].value = val
load_input(val)
add_to_all_data("#Столы\n" + result_table_fill())
add_to_all_data("#Таблица знакомств\n" + val)
show_meet_table_data()
2 years ago
document["load_data"].bind("click", load_data)
document["table_distrib"].bind("click", table_distrib)
document["next_step"].bind("click", next_step)
2 years ago
document["table_distrib_load"].bind("click", table_distrib_load)
2 years ago
</script>
</body>
</html>