#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 13 11:56:33 2017

@author: brendontucker
"""

s = "Code(Cha(lle)nge)"
#s = 'pre(inner(bracket)test(because)need)post'
sList = list(s)
expectedOutput = "CodeegnlleahC"

length = len(s)

half = length // 2

paraCounter = 0 

for x in s:
    if x == '(':
        paraCounter += 1

        
prePara = list()

#creates portion of word that has no reverse applied at the begining
#might be harder to find Python in... "xxx(dd)Python(dd)xxxx"
closed1 = 0
closed2 = 0

preCounter = 0

while preCounter < len(s):
    if sList[preCounter] == '(':
        closed2 = preCounter
        prePara = sList[closed1:closed2]
        break
    else:
        preCounter += 1

   
# find inner sections
#max inner sections limited to number of parenthases

innerContent = [[] for x in range(paraCounter - 1)] #should be paraCounter -1
                       
closed1 = 0
closed2 = 0

innerCounter = 0
innerPos = 0
#put this all in a {while innerPos < len(innerContent)} ??
#Don't actually think I need this for the new list method
while innerCounter < len(sList):
    if sList[innerCounter] == ')':
        closed1 = innerCounter
        innerCounter += 1
        while innerCounter < len(sList):
            if sList[innerCounter] == '(':
                closed2 = innerCounter
                innerContent[innerPos] = sList[closed1 + 1:closed2]
                innerPos += 1
                break
            else:
                innerCounter += 1
        
    else:
        innerCounter += 1        

#count parenthases
#determine innermost

#sList = list(s)
newSNeed = 1 + ((paraCounter - 1) * 2) #this is an upper limit
 
newS = [[] for x in range(newSNeed)]

newSCounter = 0
leftPara = 0
leftPos = [[]for x in range((paraCounter // 2) + 1)]



#find para positions?
while newSCounter < len(sList):
    if sList[newSCounter] == '(':
        leftPos[leftPara].append(newSCounter)
        leftPara =+ 1
        newSCounter += 1
    else:
        newSCounter += 1 
           
    

rightPara = 0
rightPos = [[]for x in range((paraCounter // 2) + 1)]
newSCounter = 0

while newSCounter < len(sList): 
    if sList[newSCounter] == ')':
        rightPos[rightPara].append(newSCounter)
        rightPara += 1
        newSCounter += 1
    else:
        newSCounter += 1

#forgot to reset the counter.... thats embaressing 

#if we reverse right position then moving from right to elft through both leftPos
#and rightPos we get teh inner brackets moving towards the outer
#should probably just reverse leftPos then, will have inner brackets first
#Might be a bad idea with an leftPos of length 1.... 
leftPos.reverse()

#this copies too much ...
newSCounter = 0
newSPos = 0
while newSPos < len(rightPos):
    if newSCounter == 0:
        newS[newSPos] = sList[leftPos[newSCounter][0] + 1: rightPos[newSCounter][0]] 
        newSPos += 1
        newSCounter += 1
    else: #have an even odd check here to partition each half correctly
        newS[newSCounter] = sList[leftPos[newSCounter][0] + 1: leftPos[newSCounter - 1][0]]
        newSPos += 1
        newS[newSPos] =  newS[newSCounter] = sList[rightPos[newSCounter][0] + 1: rightPos[newSCounter - 1][0]]
        newSPos += 1
                            



'''
if newSCounter % 2 == 0:
            newS[newSCounter] = sList[leftPos[newSCounter][0] + 1: leftPos[newSCounter - 1][0]]
            newSCounter += 1
        else:
            newS[newSCounter] = sList[rightPos[newSCounter][0] + 1: rightPos[newSCounter - 1][0]]
            newSCounter += 1
'''
    