Skip to content

Instantly share code, notes, and snippets.

@fourdollars
Last active November 19, 2016 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fourdollars/255ce9037f6af1f4c0b5a8345ac99cf1 to your computer and use it in GitHub Desktop.
Save fourdollars/255ce9037f6af1f4c0b5a8345ac99cf1 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import random, re
collection = ('A1-1', 'A1-2', 'A1-3', 'A1-4', 'A1-5', 'A1-6', 'A1-7', 'A1-8', 'A1-9', 'A1-10', 'A1-11', 'A1-12', 'A1-13', 'A1-14', 'A2-1', 'A2-3', 'A2-4', 'A2-5', 'A2-6', 'A2-7', 'A2-8', 'A2-9', 'A2-10', 'A2-11', 'A2-12', 'A2-13', 'A2-14', 'A3-1', 'B1-3', 'B1-4', 'B1-5', 'B1-6', 'B1-7', 'B1-8', 'B1-9', 'B1-10', 'B1-11', 'B1-12', 'B2-3', 'B2-4', 'B2-5', 'B2-6', 'B2-7', 'B2-8', 'B2-9', 'B2-10', 'B2-11', 'B2-12')
print('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>機車腳踏車車位選填志願抽籤結果</title>')
print('''<style>
table {border: 0; border-collapse: collapse; width: 100%; table-layout: fixed;}
td,th {border: 1px solid black;}
td,th {text-align: center;}
th {background: lightgray;}
.pagebreak { page-break-before: always; }
</style>''')
print('</head><body>')
def show(data, idx=None, inv=(0, 16, 32), step=(16, 16, 16)):
print("<table>")
for i, j in zip(inv, step):
print("<tr>")
for k in range(i, i + j):
if idx:
print("<th><nobr>%s</nobr></th>" % (idx[k]))
else:
print("<th>%d</th>" % (k + 1))
print("</tr>")
print("<tr>")
for k in range(i, i + j):
print("<td><nobr>%s</nobr></td>" % (data[k]))
print("</tr>")
print("</table>")
def go(topic, idx):
moto = ['' for i in range(48)]
moto_reverse = ['' for i in range(48)]
for i in range(3):
print("<h3>%s第 %d 志願</h3>" % (topic, i + 1))
slot = [[] for i in range(48)]
print('<pre>')
for person, selection in people.items():
num = selection[idx + i]
m = re.search('\d+$', num)
if m:
num = m.group(0)
if num and person not in moto:
num = int(num)
if num > 48 or num < 1:
raise Exception("Invalid input by %s" % person)
slot[num - 1].append(person)
for j, bid in enumerate(slot):
if len(bid) == 1:
if moto[j]:
print("%s 填的位置 %d 已經在前面被 %s 抽走了" % (bid[0], j + 1, moto[j]))
else:
print("位置 %d 由 %s 獨得" % (j + 1, bid[0]))
moto[j] = bid[0]
elif len(bid) > 1:
if moto[j]:
bid = sorted(bid, key=lambda x: [int(w) if w.isdigit() else w for w in x.split('-')])
bid = ", ".join(bid)
print("%s 填的位置 %d 已經在前面被 %s 抽走了" % (bid, j + 1, moto[j]))
else:
who = random.randrange(len(bid))
moto[j] = bid[who]
bid = sorted(bid, key=lambda x: [int(w) if w.isdigit() else w for w in x.split('-')])
bid = ", ".join(bid)
print("位置 %d 由 %s 抽籤,結果為 %s 抽中。" % (j + 1, bid, moto[j]))
print('</pre>')
show(moto)
print("<h3>隨機分配剩下的位置</h3>")
for idv in collection:
if idv not in moto:
while True:
num = random.randrange(48)
if not moto[num]:
moto[num] = idv
#print("<p>隨機挑選了位置 %d 給 %s</p>" % (num + 1, idv))
break
show(moto)
print("<h3>%s選填志願及抽籤結果</h3>" % (topic))
for i, j in enumerate(collection):
moto_reverse[i] = moto.index(j) + 1
show(moto_reverse, collection, (0, 14, 27, 28, 38), (14, 13, 1, 10, 10))
people = {}
poll = {}
with open("collection.csv") as f:
for line in f:
line = line.strip().split(",")
line = [x.lstrip('"').rstrip('"') for x in line]
if line[0].startswith('2016'):
poll[line[1]] = [line[3], line[4], line[5], line[6], line[7], line[8], line[0]]
people[line[1]] = line[3:]
print("<h3>原始資料</h3>")
print("<table>")
print('<tr><td style="text-align:left">戶號</td><td>機車1</td><td>機車2</td><td>機車3</td><td>腳踏車1</td><td>腳踏車2</td><td>腳踏車3</td><td colspan="4">資料時間</td></tr>')
for k in sorted(poll.keys(), key=lambda x: [int(w) if w.isdigit() else w for w in x.split('-')]):
v = poll[k]
print("<tr><td style='text-align:left'>%s</td>" % k)
for i in v:
if i == v[-1]:
print('<td colspan="4">%s</td>' % i)
else:
print("<td>%s</td>" % i)
print("</tr>")
print("</table>")
print('<div class="pagebreak"> </div>')
go("機車", 0)
print('<div class="pagebreak"> </div>')
go("腳踏車", 3)
print("</body></html>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment