{"id":894,"date":"2013-11-29T16:59:17","date_gmt":"2013-11-29T20:59:17","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/jreyes\/?p=894"},"modified":"2016-11-25T13:38:03","modified_gmt":"2016-11-25T17:38:03","slug":"round-and-round-the-robin-goes","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/jreyes\/2013\/11\/29\/round-and-round-the-robin-goes\/","title":{"rendered":"Round and round the robin goes"},"content":{"rendered":"<p>Recently I found it useful to implement a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Round-robin_tournament#Scheduling_algorithm\">round-robin tournament<\/a>. Here&#8217;s a little Python generator that produces schedules for you, for your enjoyment.<\/p>\n<pre>\r\nfrom collections import deque\r\ndef round_robin(size):\r\n  if size &lt; 2:\r\n    yield []\r\n    raise StopIteration\r\n\r\n  teams = range(1, size)\r\n  rounds = size - 1\r\n  if size % 2:\r\n    teams.append(None)\r\n    rounds = rounds + 1\r\n  roster = deque(teams)\r\n  half = (len(roster) + 1)\/2\r\n  for round in range(rounds):\r\n    positions = [0] + list(roster)\r\n    backwards = positions[::-1]\r\n    yield [(positions[i], backwards[i]) for i in range(half)]\r\n    roster.rotate()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently I found it useful to implement a round-robin tournament. Here&#8217;s a little Python generator that produces schedules for you, for your enjoyment. from collections import deque def round_robin(size): if size &lt; 2: yield [] raise StopIteration teams = range(1, &hellip; <a href=\"https:\/\/archive.blogs.harvard.edu\/jreyes\/2013\/11\/29\/round-and-round-the-robin-goes\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":102,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[380],"tags":[],"class_list":["post-894","post","type-post","status-publish","format-standard","hentry","category-computer-science"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/posts\/894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/users\/102"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/comments?post=894"}],"version-history":[{"count":6,"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/posts\/894\/revisions"}],"predecessor-version":[{"id":962,"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/posts\/894\/revisions\/962"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/media?parent=894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/categories?post=894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/jreyes\/wp-json\/wp\/v2\/tags?post=894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}