#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 15 13:15:21 2018

@author: yanglinxuan
"""

#[544LeetCode] Output Contest Matches 
'''
During the NBA playoffs, we always arrange the rather strong team to 
play with the rather weak team, like make the rank 1 team play with 
the rank nth team, which is a good strategy to make the contest more 
interesting. Now, you're given n teams, you need to output their final 
contest matches in the form of a string.

The n teams are given in the form of positive integers from 1 to n, 
which represents their initial rank. (Rank 1 is the strongest team and 
Rank n is the weakest team.) We'll use parentheses('(', ')') and commas(',') 
to represent the contest team pairing - parentheses('(' , ')') for pairing 
and commas(',') for partition. During the pairing process in each round, 
you always need to follow the strategy of making the rather strong one pair 
with the rather weak one.
'''



def contestMatch(n):
   l=[]
   for i in range (n):
       l.append(i+1)
   helperalg(n,l)
   return l
 
#contestMatch(8)

def helperalg(n,l):
    if int(n)==1:
        return l
    for i in range (int(n/2)):
        l[i]= "(" + str(l[i])+"," + str(l[n-1-i]) + ")"
        l.remove(l[n-1-i])
    helperalg(n//2,l)
    return l
'''
l=[1,2,3,4,5,6,7,8]
n=8
print(helperalg(n,l))
l2=['(1,8)', '(2,7)', '(3,6)', '(4,5)']
n2=4
print(helperalg(n2,l2))
l3=['((1,8),(4,5))', '((2,7),(3,6))']
n3=2
print(helperalg(n3,l3))
'''
print(contestMatch(16))
