Browse Source

Исправлены некоторые опечатки, и улучшен стиль #8

styling
Artur Galyamov 2 years ago
parent
commit
f9cdbdd985
  1. 136
      table_distributor.html

136
table_distributor.html

@ -28,7 +28,7 @@
<summary>Начальные данные</summary> <summary>Начальные данные</summary>
<div> <div>
<p> <p>
<label>Номер расперделения:</label> <label>Номер распределения:</label>
<input id = "step_id" type="number" value="1" step="1"/> <input id = "step_id" type="number" value="1" step="1"/>
</p> </p>
@ -100,14 +100,16 @@
from browser import document, html, window from browser import document, html, window
import random import random
class Table: class Table:
def __init__(self, id, person_set): def __init__(self, a_id, person_set):
self.id = id self.id = a_id
self.person_set = person_set self.person_set = person_set
id = 0 id = 0
person_set = set() person_set = set()
class Person: class Person:
def __init__(self, beig_id, name, meet_set): def __init__(self, beig_id, name, meet_set):
self.beig_id = beig_id self.beig_id = beig_id
@ -121,18 +123,21 @@ class Person:
name = "" name = ""
meet_set = set() meet_set = set()
global persons
persons = [] global people
people = []
global tables global tables
tables = [] tables = []
def add_to_all_data(val): def add_to_all_data(val):
document["all_output_data"].value += "\n\n#Шаг " + document["step_id"].value + "\n" + val document["all_output_data"].value += "\n\n#Шаг " + document["step_id"].value + "\n" + val
def load_input(val): def load_input(val):
global persons global people
persons = [] people = []
input_data = val input_data = val
for p in input_data.split("\n"): for p in input_data.split("\n"):
if p == "": if p == "":
@ -149,34 +154,36 @@ def load_input(val):
for s in str_meet_list: for s in str_meet_list:
if len(s) != 0: if len(s) != 0:
cur_meet_list = cur_meet_list | {int(s)} cur_meet_list = cur_meet_list | {int(s)}
p = Person(cur_beig_id,cur_name, cur_meet_list) p = Person(cur_beig_id, cur_name, cur_meet_list)
persons.append(p) people.append(p)
def get_present_people():
global people
return list(filter(lambda p: p.beig_id > 0, people))
def get_present_persons():
global persons
return list(filter(lambda p : p.beig_id > 0, persons))
def show_meet_table_data(): def show_meet_table_data():
global persons global people
html = "Всего участников: " + str(len(persons)) + "<br/>" html = "Всего участников: " + str(len(people)) + "<br/>"
html += "Присутсвуют участников: " + str(len(get_present_persons())) + "<br/>" html += "Присутствуют участников: " + str(len(get_present_people())) + "<br/>"
html += "Номер расперделения: " + document["step_id"].value + "<br/>" html += "Номер распределения: " + document["step_id"].value + "<br/>"
meet_count = 0; meet_count = 0
html += "<table border = 1>" html += "<table border = 1>"
# Первая строка # Первая строка
html += "<tr>" html += "<tr>"
html += "<td></td>" html += "<td></td>"
for p in persons: for p in people:
html += "<th" + " title='"+ p.name + "'>" + str(abs(p.beig_id)) + "</th>" html += "<th" + " title='" + p.name + "'>" + str(abs(p.beig_id)) + "</th>"
html += "</tr>" html += "</tr>"
# Остальные строки # Остальные строки
for p in persons: for p in people:
html += "<tr>" html += "<tr>"
html += "<th" + " title='"+ p.name + "'>" + str(abs(p.beig_id)) + "</th>" html += "<th" + " title='" + p.name + "'>" + str(abs(p.beig_id)) + "</th>"
for ip in persons: for ip in people:
if abs(p.beig_id) in ip.meet_set: if abs(p.beig_id) in ip.meet_set:
html += "<td bgcolor='red'></td>" html += "<td bgcolor='red'></td>"
meet_count += 1 meet_count += 1
@ -185,20 +192,22 @@ def show_meet_table_data():
html += "</tr>" html += "</tr>"
html += "</table>" html += "</table>"
html += "<br/>Знакомств: " + str(meet_count) + "/" + str(len(persons)**2) + "<br/>" html += "<br/>Знакомств: " + str(meet_count) + "/" + str(len(people) ** 2) + "<br/>"
document["meet_table"].innerHTML = html document["meet_table"].innerHTML = html
def get_result(): def get_result():
global persons global people
result = "" result = ""
for p in persons: for p in people:
result += str(p.beig_id) + ";" + p.name + ";" result += str(p.beig_id) + ";" + p.name + ";"
if len(p.meet_set): if len(p.meet_set):
result += str(p.meet_set) result += str(p.meet_set)
result += "\n" result += "\n"
return result return result
def result_table_fill(): def result_table_fill():
global tables global tables
result_txt = "" result_txt = ""
@ -209,6 +218,7 @@ def result_table_fill():
result_txt += "\n" result_txt += "\n"
return result_txt return result_txt
def print_result_table(): def print_result_table():
table_names = document["table_name"].value table_names = document["table_name"].value
@ -229,7 +239,7 @@ def print_result_table():
html += "<td>" + str(table_names[t.id]) + "</td>" html += "<td>" + str(table_names[t.id]) + "</td>"
if len(t.person_set): if len(t.person_set):
html += "<td>" + str(t.person_set).replace("{","").replace("}","") + "</td>" html += "<td>" + str(t.person_set).replace("{", "").replace("}", "") + "</td>"
else: else:
html += "<td></td>" html += "<td></td>"
html += "</tr>" html += "</tr>"
@ -238,61 +248,65 @@ def print_result_table():
document["result_table_txt"].value = result_table_fill() document["result_table_txt"].value = result_table_fill()
def get_person_by_id(id): def get_person_by_id(id):
global persons global people
for p in persons: for p in people:
if abs(p.beig_id) == id: if abs(p.beig_id) == id:
return p return p
print("Error", id) print("Error", id)
def fill_next_table(start_person_id, persons_set, table_size_count, persons):
result_persons = [get_person_by_id(start_person_id)] def fill_next_table(start_person_id, people_set, table_size_count, people):
result_people = [get_person_by_id(start_person_id)]
result_set = {start_person_id} result_set = {start_person_id}
while(len(result_set) < table_size_count): while len(result_set) < table_size_count:
search_succes = False search_success = False
for p in persons: for p in people:
cur_id = abs(p.beig_id) cur_id = abs(p.beig_id)
if cur_id in persons_set: if cur_id in people_set:
continue continue
if cur_id in result_set: if cur_id in result_set:
continue continue
pers_free = True pers_free = True
for ip in result_persons: for ip in result_people:
if cur_id in ip.meet_set: if cur_id in ip.meet_set:
pers_free = False pers_free = False
break break
if pers_free: if pers_free:
result_set = result_set | {cur_id} result_set = result_set | {cur_id}
result_persons.append(p) result_people.append(p)
search_succes = True search_success = True
break break
if not search_succes: if not search_success:
for p in persons: for p in people:
cur_id = abs(p.beig_id) cur_id = abs(p.beig_id)
if cur_id in persons_set: if cur_id in people_set:
continue continue
if cur_id in result_set: if cur_id in result_set:
continue continue
result_set = result_set | {cur_id} result_set = result_set | {cur_id}
result_persons.append(p) result_people.append(p)
break break
return result_set return result_set
def get_beig_id_set(persons):
def get_beig_id_set(people):
result = set() result = set()
for p in persons: for p in people:
result = result | {abs(p.beig_id)} result = result | {abs(p.beig_id)}
return result return result
def fill_tables(): def fill_tables():
global tables global tables
tables = [] tables = []
cur_table_id = 0 cur_table_id = 0
sort_persons = sorted(get_present_persons(), key=lambda person: len(person.meet_set)) sort_people = sorted(get_present_people(), key=lambda person: len(person.meet_set))
all_table_size = 0 all_table_size = 0
str_table_size = document["table_size"].value.replace(" ", "").split(",") str_table_size = document["table_size"].value.replace(" ", "").split(",")
@ -303,29 +317,31 @@ def fill_tables():
all_table_size += t all_table_size += t
table_size += [t] table_size += [t]
table_count = len(table_size); beig_id_set = get_beig_id_set(sort_people)
beig_id_set = get_beig_id_set(sort_persons)
persons_set = set() people_set = set()
while len(persons_set) != len(sort_persons): while len(people_set) != len(sort_people):
if len(persons_set) >= all_table_size: if len(people_set) >= all_table_size:
break break
new_person_id = random.sample(list(beig_id_set - persons_set), 1)[0] new_person_id = random.sample(list(beig_id_set - people_set), 1)[0]
cur_set = fill_next_table(new_person_id, persons_set, min(table_size[cur_table_id], len(sort_persons) - len(persons_set)), sort_persons) cur_set = fill_next_table(new_person_id, people_set,
persons_set = persons_set | cur_set min(table_size[cur_table_id], len(sort_people) - len(people_set)), sort_people)
people_set = people_set | cur_set
tables.append(Table(cur_table_id, cur_set)) tables.append(Table(cur_table_id, cur_set))
cur_table_id += 1 cur_table_id += 1
def fill_persons():
global persons def fill_people():
global people
global tables global tables
for t in tables: for t in tables:
for p in persons: for p in people:
if abs(p.beig_id) in t.person_set: if abs(p.beig_id) in t.person_set:
p.meet_set = t.person_set | p.meet_set p.meet_set = t.person_set | p.meet_set
def load_input_table(val): def load_input_table(val):
global tables global tables
tables = [] tables = []
@ -345,9 +361,11 @@ def load_input_table(val):
t = Table(cur_id, cur_meet_list) t = Table(cur_id, cur_meet_list)
tables.append(t) tables.append(t)
def make_result(): def make_result():
fill_tables() fill_tables()
fill_persons() fill_people()
def load_data(event): def load_data(event):
val = document["input_data"].value val = document["input_data"].value
@ -355,6 +373,7 @@ def load_data(event):
add_to_all_data("#Таблица знакомств\n" + val) add_to_all_data("#Таблица знакомств\n" + val)
show_meet_table_data() show_meet_table_data()
def table_distrib(event): def table_distrib(event):
load_input(document["input_data"].value) load_input(document["input_data"].value)
make_result() make_result()
@ -362,14 +381,16 @@ def table_distrib(event):
document["output_data"].value = get_result() document["output_data"].value = get_result()
show_meet_table_data() show_meet_table_data()
def table_distrib_load(event): def table_distrib_load(event):
load_input(document["input_data"].value) load_input(document["input_data"].value)
load_input_table(document["result_table_txt"].value) load_input_table(document["result_table_txt"].value)
fill_persons() fill_people()
print_result_table() print_result_table()
document["output_data"].value = get_result() document["output_data"].value = get_result()
show_meet_table_data() show_meet_table_data()
def next_step(event): def next_step(event):
document["step_id"].value = str(int(document["step_id"].value) + 1) document["step_id"].value = str(int(document["step_id"].value) + 1)
@ -380,6 +401,7 @@ def next_step(event):
add_to_all_data("#Таблица знакомств\n" + val) add_to_all_data("#Таблица знакомств\n" + val)
show_meet_table_data() show_meet_table_data()
document["load_data"].bind("click", load_data) document["load_data"].bind("click", load_data)
document["table_distrib"].bind("click", table_distrib) document["table_distrib"].bind("click", table_distrib)
document["next_step"].bind("click", next_step) document["next_step"].bind("click", next_step)

Loading…
Cancel
Save