# Task

Christmas is coming. In the [previous kata](https://www.codewars.com/kata/5a405ba4e1ce0e1d7800012e), we build a custom Christmas tree with the specified characters and the specified height.

Now, we are interested in the center of the Christmas tree. 

Please imagine that we build a Christmas tree with `chars = "abc" and n = Infinity`, we got:
```
          a
         b c
        a b c
       a b c a
      b c a b c
     a b c a b c
    a b c a b c a
   b c a b c a b c
  a b c a b c a b a
 b c a b c a b a b c
a b c a b a b c a b c
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
          |
          |
          .
          .
```
If we keep only the center part of leaves, we will got:
```
          a
            
          b  
              
          a    
                
          a        
                    
          b        
                    
          a          
          .
          
          .
          
          .
```
As you can see, it's a infinite string, but it has a repeat substring "aba"(spaces will be removed). If we join them together, it looks like:`"abaabaabaabaaba......"`.

So, your task is to find the repeat substring of the center part of leaves.

# Inputs:

- `chars`: the specified characters. In this kata, they always be lowercase letters.

# Output:

- The repeat substring that satisfy the above conditions.

Still not understand the task? Look at the following example ;-)

# Examples


For `chars = "abc"`,the output should be `"aba"`
```
center leaves sequence: "abaabaabaabaabaaba....."
```
For `chars = "abab"`,the output should be `a`
```
center leaves sequence: "aaaaaaaaaa....."
```
For `chars = "abcde"`,the output should be `aecea`
```
center leaves sequence: "aeceaaeceaaecea....."
```
For `chars = "aaaaaaaaaaaaaa"`,the output should be `a`
```
center leaves sequence: "aaaaaaaaaaaaaa....."
```
For `chars = "abaabaaab"`,the output should be `aba`
```
center leaves sequence: "abaabaabaaba....."
```

Difficulty: introductory

Starter code:

def center_of(chars):
	

Solutions:
def center_of(chars):
    if not chars:
        return ""
    total=0
    res=[]
    for i in range(1,len(chars)*2+1):       
        if i%2==1:
            res.append((i+1)//2+total)
            res[-1]=chars[(res[-1]-1)%len(chars)]
        total+=i
    res="".join(res)
    for i in range(len(res)//2+1):
        if len(res)%len(res[:i+1])!=0:
            continue
        if res[:i+1]*(len(res)//len(res[:i+1]))==res:
            return res[:i+1]
    return res

from itertools import count

def center_of(s):
    if not s: return ''
    seq, le, i = '', len(s), 0
    for k in count(1):
        seq += s[i % le]
        i += 4 * k
        n = len(seq)/3
        if n.is_integer():
            n = int(n)
            if seq[:n] == seq[n:n*2] == seq[n*2:] and i >= le : return seq[:n]

from collections import deque
from itertools import count, islice

def iter_center(s):
    q = deque(s)
    for i in count():
        yield q[i % len(q)]
        q.rotate(-3-i*4)

def find_repeat(g):
    xs = []
    for i in count(1):
        xs.extend(islice(g, 10))
        if all(len(set(xs[j::i])) == 1 for j in range(i)):
            return ''.join(xs[:i])

def center_of(chars):
    return chars and find_repeat(iter_center(chars))

from re import sub; center_of=lambda c: sub(r"^(.+?)\1*$","\g<1>","".join((c+c)[(i*(i*2+1)+i)%len(c)] for i in range(len(c))))

def center_of(chars):
    if not chars:
        return ""
    total=0
    res=[]
    for i in range(1,len(chars)*2+1):       
        if i%2==1:
            res.append((i+1)//2+total)
            res[(i-1)//2]=chars[(res[(i-1)//2]-1)%len(chars)]
        total+=i
    check="".join(res)
    for i in range(len(check)//2+1):
        if len(check)%len(check[:i+1])!=0:
            continue
        if check[:i+1]*(len(check)//len(check[:i+1]))==check:
            return check[:i+1]
    return check

def center_of(chars):
    total=0
    res=[]
    if not chars:
        return ""
    for i in range(1,len(chars)*2+1):       
        if i%2==1:
            res.append((i+1)//2+total)
            res[(i-1)//2]=chars[((i+1)//2+total-1)%len(chars)]
        total+=i
    if len(set(res))==1: return res[0]
    check="".join(res)
    for i in range(len(check)//2+1):
        if len(check)%len(check[:i+1])!=0:
            continue
        if check[:i+1]*(len(check)//len(check[:i+1]))==check:
            return check[:i+1]
    return check

def center_of(chars):
    size = len(chars)
    if size == 0:
        return ""
    
    size = len(chars)
    center_calc = size * 4
    center = ''
    index_large = 0
    for n in range(center_calc):
        index_large += n * 4
        index = index_large % size
        center += chars[index]
        
    #print(center)
    
    for n in range(1, center_calc):
        if center[:n] == center[n:2*n] and center[n:2*n] == center[2*n:3*n] and center[2*n:3*n] == center[3*n:4*n]:
            return center[:n]
    
    return(center)

def get_seq(string, slen):
    for i in range(1, slen+1):
        sub = string[:i]
        times = slen // i
        if sub*times == string:
            return sub
    else:
        return False

def center_of(chars):
    if not chars: return chars
    seq = chars[0]
    chars_len = len(chars)

    i = 2
    lcount = 0
    while True:
        if i % 2:
            middle = chars[(lcount + (i // 2)) % chars_len]
            seq += middle
        else:
            lcount += 2*i - 1
        i += 1
        lseq = len(seq)
        if lseq >= chars_len:
            subseq = get_seq(seq, lseq)
            if subseq:
                return subseq


# I like to think of this in terms of math but it's been a while
# since number theory and modular arithmatic. Looking at this, we
# get the serries 1, 5, 13, 25, 41,... as we're starting at 0,
# it's actually 0, 4, 12, 24, 40, ... So this is 4*sum of the first
# n-1 numbers which gives us the formula of the nth number (starting
# with 0) is 2n(n+1).

# So the the nth character in the center is 2n(n+1) % len(chars).
# But this is an infinite serries so I need to know when to stop.
# If n is odd, then the pattern repeats every n (only half + 1 letters)
# If n is even, then the pattern repeats ever n/2 generally (sometimes shorter).

# Need to check the final product to make sure that there is no repeat
# in there as the exceptions in the evens and the characters may not be unique.
def center_of(chars):
    center = ""
    n = len(chars)
    if n == 0:
        return center
    elif n % 2 == 0:
        n //= 2
    # get the list of character positions
    pos = []
    for i in range(n):
        pos.append(2*i*(i+1) % len(chars))
    # get the characters in the center
    for num in pos:
        center += chars[num]
    # test if there's are repeat in the center
    for i in range(len(center)//2):
        test = center[:i+1]
        if center == test * (len(center)//len(test)):
            center = test
            break  
    return center

def center_of(chars):
    if chars == '':
        return ''
    chars = chars * 10000
    def n_int(n):
        return n*(n+1) // 2
    center = ''
    c = 0
    i= 0
    while n_int(i) <= len(chars):
        if i%2 != 0:
            c += 1
            pos_center = (n_int(i) - c) 
            center += chars[pos_center]
        i += 1
    for x in range(1, len(center)):
        subcenter = center[:x]
        if subcenter * (len(center)//len(subcenter))+(subcenter[:len(center)%len(subcenter)]) == center:
            return(subcenter)