Skip to content

Instantly share code, notes, and snippets.

@billyeh
Created December 23, 2017 17:19
Show Gist options
  • Save billyeh/f1247e540e7b1edd6905369541c8f33c to your computer and use it in GitHub Desktop.
Save billyeh/f1247e540e7b1edd6905369541c8f33c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Implement round-robin tournament scheduling for prayer partners\n",
"# https://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm\n",
"\n",
"import csv\n",
"bros= []\n",
"with open('/Users/billyeh/bros.csv') as f:\n",
" r = csv.reader(f, delimiter=',', quotechar='\"')\n",
" for row in r:\n",
" bros.append(row[0])\n",
"\n",
"print(len(bros))\n",
"bros"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict\n",
"pairs = defaultdict(lambda: defaultdict(lambda: ''))\n",
"half = len(bros) / 2\n",
"bracket = [bros[:half], list(reversed(bros[half:]))]\n",
"num_partners = len(bros) - 1\n",
"\n",
"def rotate_bracket(b):\n",
" first_row = b[0]\n",
" second_row = b[1]\n",
" first_row.insert(1, second_row.pop(0))\n",
" second_row.append(first_row.pop())\n",
"\n",
"pairs = defaultdict(list)\n",
"for _ in range(num_partners):\n",
" for i, bro in enumerate(bracket[0]):\n",
" partner = bracket[1][i]\n",
" pairs[bro].append(partner)\n",
" pairs[partner].append(bro)\n",
" rotate_bracket(bracket)\n",
"\n",
"# sanity check\n",
"for bro, partners in pairs.items():\n",
" if len(partners) != num_partners:\n",
" print(bro)\n",
" for b in bros:\n",
" if b != bro and b not in partners:\n",
" print(bro, b)\n",
" for i, partner in enumerate(partners):\n",
" if pairs[partner][i] != bro:\n",
" print(partner, bro)\n",
"pairs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_name(p):\n",
" return ' '.join(p.split()[:-1])\n",
"\n",
"def chunks(l, n):\n",
" for i in range(0, len(l), n):\n",
" yield l[i:i + n]\n",
"\n",
"CHUNK_SIZE = 2\n",
"\n",
"with open('/Users/billyeh/out.csv', 'w') as f:\n",
" w = csv.writer(f)\n",
" for bro, partners in sorted(pairs.items()):\n",
" p = []\n",
" partner_chunks = chunks(partners, CHUNK_SIZE)\n",
" for c in partner_chunks:\n",
" p.append('; '.join([get_name(b) for b in c]))\n",
" w.writerow([bro] + p)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment