8
\$\begingroup\$

Introduction

Right now I'm participating a chess tournament. I got inspired by the tournament schedule. Right now, I'm in a competition with three other players. That means with the four of us, we are playing 3 rounds. The schedule goes as following:

Round 1: 1-4 3-2
Round 2: 3-4 2-1
Round 3: 1-3 4-2

This is also known as Round Robin. Also, this is a valid schedule. We say that a schedule is valid, when it satisfies the following conditions:

  • Every player plays once against another player.
  • 1-2 means that player 1 has white. Every player has or (N / 2 - 0.5) or (N / 2 + 0.5) times white, with N being the amount of rounds.

For example, in the above example, there are 3 rounds. So N = 3. As you can see, the amount of rounds equals the amount of players - 1. A player has either

  • N / 2 - 0.5 = 1 time white, or
  • N / 2 + 0.5 = 2 times white.

In the above example:

  • player 1 has 2 times white,
  • player 2 has 1 time white,
  • player 3 has 2 times white,
  • player 4 has 1 time white.

The task

Given an even integer > 1 representing the amount of players in the competition, output the tournament schedule.

Test cases:

Input = 4, that means N = 3

Input:     Output:
4          1-4 3-2
           3-4 2-1
           1-3 4-2

Input = 10, that means N = 9

Input:     Output:
10         1-10  2-9  3-8  4-7  5-6
           10-6  7-5  8-4  9-3  1-2
           2-10  3-1  4-9  5-8  6-7
           10-7  8-6  9-5  1-4  2-3
           3-10  4-2  5-1  6-9  7-8
           10-8  9-7  1-6  2-5  3-4
           4-10  5-3  6-2  7-1  8-9
           10-9  1-8  2-7  3-6  4-5
           5-10  6-4  7-3  8-2  9-1

Input = 12, that means N = 11

Input:     Output:
12         1-14    2-13   3-12   4-11   5-10   6-9    7-8
           14-8    9-7    10-6   11-5   12-4   13-3   1-2
           2-14    3-1    4-13   5-12   6-11   7-10   8-9
           14-9    10-8   11-7   12-6   13-5   1-4    2-3
           3-14    4-2    5-1    6-13   7-12   8-11   9-10
           14-10   11-9   12-8   13-7   1-6    2-5    3-4
           4-14    5-3    6-2    7-1    8-13   9-12   10-11
           14-11   12-10  13-9   1-8    2-7    3-6    4-5
           5-14    6-4    7-3    8-2    9-1    10-13  11-12
           14-12   13-11  1-10   2-9    3-8    4-7    5-6
           6-14    7-5    8-4    9-3    10-2   11-1   12-13
           14-13   1-12   2-11   3-10   4-9    5-8    6-7
           7-14    8-6    9-5    10-4   11-3   12-2   13-1

Of course, the extra whitespaces I used between the numbers is optional but not necessary. You may output it in any form you like, as long as it's readable.

Note: The above examples aren't just the only valid outputs. There certainly are more valid outputs.

This is , so the submission with the least amount of bytes wins!

\$\endgroup\$
6
  • 2
    \$\begingroup\$ I think that validity is underspecified. In particular, shouldn't it require that the number of rounds is one less than the number of players? \$\endgroup\$ Jan 16, 2016 at 12:03
  • 1
    \$\begingroup\$ @PeterTaylor Yes, I forgot to mention that. I have edited it in the question \$\endgroup\$
    – Adnan
    Jan 16, 2016 at 19:34
  • \$\begingroup\$ Do players have to be numbered 1 to N, or can they be numbered 0 to N-1? \$\endgroup\$ Jan 16, 2016 at 20:48
  • \$\begingroup\$ @steveverrill Whatever fits best for you. \$\endgroup\$
    – Adnan
    Jan 16, 2016 at 20:59
  • \$\begingroup\$ Do you have to separate the rounds in the outputs or is 1-4 3-2 3-4 2-1 1-3 4-2 for n=4 a valid output? \$\endgroup\$
    – randomra
    Jan 16, 2016 at 22:00

1 Answer 1

3
\$\begingroup\$

Python 2, 200 186 Bytes


n=input()
t=range(2,n+1)
for k in t:
 s=[1]+t
 c=[[s[x],s[-x-1]] for x in range(n/2)]
 if k%2:c[0].reverse()
 t=t[-1:]+t[:-1]
 print"\t".join(["-".join([str(y) for y in x]) for x in c])

Example output:

$ python chess.py
4
1-4     2-3
3-1     4-2
1-2     3-4

$ python chess.py
10
1-10    2-9     3-8     4-7     5-6
9-1     10-8    2-7     3-6     4-5
1-8     9-7     10-6    2-5     3-4
7-1     8-6     9-5     10-4    2-3
1-6     7-5     8-4     9-3     10-2
5-1     6-4     7-3     8-2     9-10
1-4     5-3     6-2     7-10    8-9
3-1     4-2     5-10    6-9     7-8
1-2     3-10    4-9     5-8     6-7

$ python chess.py
14
1-14    2-13    3-12    4-11    5-10    6-9     7-8
13-1    14-12   2-11    3-10    4-9     5-8     6-7
1-12    13-11   14-10   2-9     3-8     4-7     5-6
11-1    12-10   13-9    14-8    2-7     3-6     4-5
1-10    11-9    12-8    13-7    14-6    2-5     3-4
9-1     10-8    11-7    12-6    13-5    14-4    2-3
1-8     9-7     10-6    11-5    12-4    13-3    14-2
7-1     8-6     9-5     10-4    11-3    12-2    13-14
1-6     7-5     8-4     9-3     10-2    11-14   12-13
5-1     6-4     7-3     8-2     9-14    10-13   11-12
1-4     5-3     6-2     7-14    8-13    9-12    10-11
3-1     4-2     5-14    6-13    7-12    8-11    9-10
1-2     3-14    4-13    5-12    6-11    7-10    8-9

Or 135 Bytes, by using a less pretty (but still readable) output:

...
print c

Which produces something like:

$ python chess.py
4
[[1, 4], [2, 3]]
[[3, 1], [4, 2]]
[[1, 2], [3, 4]]
\$\endgroup\$
2
  • \$\begingroup\$ The less pretty answer is also a valid submission. So this is 135 bytes :) \$\endgroup\$
    – Adnan
    Jan 17, 2016 at 18:38
  • \$\begingroup\$ I'd upvote you if you added a human explanation of your algorithm. \$\endgroup\$ Jan 18, 2016 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.