Python: Train of Thought and simple implementation example for designing the Football League Schedule Program.

Source: Internet
Author: User

Python: Train of Thought and simple implementation example for designing the Football League Schedule Program.

Each year, the schedules of major league matches in the Premier League of Serie A are a must-have for fans. It is so comfortable to write this kind of things in Python when I think of the code that I have previously written to generate a competition schedule.
This algorithm is called the snake ring algorithm.
That is to say, all teams are arranged in a ring (two columns), and the first team does not move on the right side of the left side. Other teams cycle clockwise, so that they will definitely not repeat.
For convenience, assume there are eight Teams a to h. Sort in a ring as below.

In this way, in the 1st round, the game is (a, h) (B, g) (c, f) (d, e ).
In the next round, the first team a does not move, and other teams follow the same grid clockwise as the gear.

In this way, in the 2nd round, the game is (a, B) (c, h) (d, g) (e, f ).
The gear continues to slide until it returns to the origin so that every team matches all the other 7 teams.

A thousand words is worse than a single code. Take the Premier League as an example.

From collections import deque import random def build_schedule (_ teamarr): scheduleobj = dict. fromkeys (range () fixpos = _ teamarr [0] ring = _ teamarr [1:] ring = deque (ring) # First Half of the competition, 1-19 round (round) for round in range (1st): # teams do not move, plus the rotate cycle teams = [fixpos] + list (ring) # Cut into 2 columns, left main team, home, away = teams [: len (teams)/2], teams [len (teams)/2:] away = away [:: -1] # randomly disrupt the main queue scheduleobj [round] = [(x, y) if random. random ()> = 0.5 else (y, x) for x, y in zip (home, away)] ring. rotate (1) # the last half of the season is the same as the last half of the season, but the main line is opposite to round in range (20, 39): scheduleobj [round] = [(y, x) for x, y in scheduleobj [round-19] return scheduleobj if _ name _ = '_ main _': teamarr = [u 'manchester United ', u 'aston Villa ', u 'Chelsea ', u 'westham', u 'call', u 'hotspur', u 'Liverpool ', u 'southup', u 'efl', u 'inval ', u 'newcastle ', u 'manchester City', u 'stoke city', u 'santand', u 'crystal gong', u 'West buroww', u 'gun ', u 'hull city', u 'cardiff city', u 'swangsi'] scheduleobj = build_schedule (teamarr) print U' --- League 1st round --- 'for h, a in scheduleobj [1]: print U '{}-{}'. format (h, a) print U' --- League Round 2nd --- 'for h, a in scheduleobj [2]: print U '{}-{}'. format (h,)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.