# Task

In the field, two beggars A and B found some gold at the same time. They all wanted the gold, and they decided to use simple rules to distribute gold:
```
They divided gold into n piles and be in line. 
The amount of each pile and the order of piles all are randomly.

They took turns to take away a pile of gold from the 
far left or the far right.

They always choose the bigger pile. That is to say, 
if the left is 1, the right is 2, then choose to take 2.

If the both sides are equal, take the left pile.
```

Given an integer array `golds`, and assume that A always takes first. Please calculate the final amount of gold obtained by A and B. returns a two-element array `[amount of A, amount of B]`.

# Example

For `golds = [4,2,9,5,2,7]`, the output should be `[14,15]`.
```
The pile of most left is 4, 
The pile of most right is 7, 
A choose the largest one -- > take 7

The pile of most left is 4, 
The pile of most right is 2, 
B choose the largest one -- > take 4

The pile of most left is 2, 
The pile of most left is 2, 
A choose the most left one -- > take 2

The pile of most left is 9, 
The pile of most right is 2, 
B choose the largest one -- > take 9

The pile of most left is 5, 
The pile of most left is 2, 
A choose the largest one -- > take 5

Tehn, only 1 pile left, 
B  -- > take 2

A: 7 + 2 + 5 = 14
B: 4 + 9 + 2 = 15
```

For `golds = [10,1000,2,1]`, the output should be `[12,1001]`.
```
A take 10
B take 1000
A take 2
B take 1

A: 10 + 2 = 12
B: 1000 + 1 = 1001
```

Difficulty: introductory

Starter code:

def distribution_of(golds):
	

Solutions:
def distribution_of(golds):
    g = golds[:]
    turn, total = 0, [0, 0]
    while g:
        total[turn % 2] += g.pop(-(g[0] < g[-1]))
        turn += 1
    return total


from collections import deque
def distribution_of(golds):
    g = deque(golds)
    s = [0, 0]
    t = 0
    while g:
        s[t] += (g.popleft, g.pop)[g[0] < g[-1]]()
        t ^= 1
    return list(s)

def distribution_of(golds):
    ind, beggars = [0, len(golds)-1], [0, 0]
    for n in range(len(golds)):
        i = golds[ind[0]] < golds[ind[1]]
        beggars[n%2] += golds[ind[i]]
        ind[i] += (-1)**i
    return beggars

def distribution_of(golds):
    g = golds[:]
    turn, total, i, j = 0, [0, 0], 0, len(g) - 1
    while i <= j:
        total[turn % 2] += max(g[i], g[j])
        if g[j] > g[i]:
            j -= 1
        else:
            i += 1
        turn += 1
    return total

def take_one(golds):
    if len(golds) > 1 and golds[0] >= golds[-1]:
        return golds.pop(0)
    else:
        return golds.pop() if golds else 0
        
        
def distribution_of(golds):
    golds = golds[:]
    a = b = 0
    while golds:
        a += take_one(golds)
        b += take_one(golds)
    return [a, b]

def distribution_of(golds):
    golds = golds[:]
    take = lambda: golds.pop(0 if golds[0] >= golds[-1] else -1) if golds else 0
    a = b = 0
    while golds:
        a += take()
        b += take()
    return [a, b]

def distribution_of(piles):
    i, gold = False, [0, 0]
    while piles:
        gold[i], piles = (gold[i]+piles[-1], piles[:-1]) if piles[-1] > piles[0] else (gold[i]+piles[0], piles[1:])
        i = not i
    return gold

distribution_of=lambda g,r=[0,0],c=0: r if not len(g) else distribution_of(*([g[:-1],[r[0],r[1]+g[-1]],0] if g[-1]>g[0] else [g[1:],[r[0],r[1]+g[0]],0])) if c else distribution_of(*([g[:-1],[r[0]+g[-1],r[1]],1] if g[-1]>g[0] else [g[1:],[r[0]+g[0],r[1]],1]))

def distribution_of(gold):
    # Make gold a copy of gold because otherwise it breaks the random checks
    gold, beggargold = gold.copy(), [0, 0]
    for pile in range(len(gold)):
        if pile % 2 == 0:
            beggargold[0] += (gold.pop(0) if gold[0] >= gold[-1] else gold.pop())
        else:
            beggargold[1] += (gold.pop(0) if gold[0] >= gold[-1] else gold.pop())
    return beggargold


def distribution_of(golds):
    ab = [0, 0]
    g = golds[:]
    for i in range(len(g)):
        ab[i%2] += max(g[0], g[-1])
        g.pop(-1) if g[-1] > g[0] else g.pop(0)
    return ab

def distribution_of(golds):
    gg=golds[:]
    out=[0,0]
    pos=0
    for _ in range(len(gg)):
        if gg[0]>=gg[-1]:
            out[pos]+=(gg.pop(0)) 
        else:
            out[pos]+=(gg.pop(-1))
        pos=not pos
    return out


def distribution_of(golds):
    g = golds[:]
    i, beggars = 0,[0,0]
    while g:
        beggars[i%2] += g.pop(-(g[0] < g[-1]))
        i+=1
    return beggars

def distribution_of(golds):
    g = golds[:]
    total = [0,0]
    turn = 0
    while len(g) > 0:
        if g[0] >= g[-1]:
            value = g.pop(0)
        else:
            value = g.pop(-1)      
        total[turn % 2] += value    
        turn += 1
    return total

def distribution_of(golds):
    golds_list = golds[:]
    a = []
    b = []
    turn = 1
    while len(golds_list) > 0:
        if golds_list[0] >= golds_list[-1]:
            value = golds_list.pop(0)
        else:
            value = golds_list.pop(-1)
        if turn % 2 == 1:
            a.append(value)
        else:
            b.append(value)
        turn += 1
    return [sum(a), sum(b)]

def distribution_of(golds):
    gold_list = golds[:]        
    isA = True
    amount = [0 ,0]
    
    while len(gold_list) != 0:
        if (gold_list[0] > gold_list[-1]) or (gold_list[0] == gold_list[-1]):
            if isA:
                amount[0] += gold_list[0]
            else:
                amount[1] += gold_list[0]
            gold_list.pop(0)
        else:
            if isA:
                amount[0] += gold_list[-1]
            else:
                amount[1] += gold_list[-1]
            gold_list.pop(-1)
        isA = not isA

    return amount

def distribution_of(golds):
    g = golds[::]
    R = [0,0]
    toggle = 0
    currentVar = 'A'
    while g:
        if g[-1] > g[0]:
            R[toggle] += g.pop(-1)
        else:
            R[toggle] += g.pop(0)
        toggle = 1 - toggle

    return R

def distribution_of(golds):
    a,b,i=0,0,0
    golds_c=golds[:]
    while golds_c:
        m=golds_c.pop(0) if golds_c[0]>=golds_c[-1] else golds_c.pop()
        if i%2:b+=m
        else:a+=m
        i+=1
    return [a,b]

def distribution_of(golds):
    p,g,player=0,golds[:],[0,0]
    while g: player[p],p = player[p]+g.pop(-1 if g[0]<g[-1] else 0),1-p
    return player

def distribution_of(g):
    golds = g.copy()
    print(golds)
    a=0
    b=0
    while golds:
        if golds[0] >= golds[-1]:
            a += golds.pop(0)
        else:
            a += golds.pop(-1)
        if not golds:
            break
        if golds[0] >= golds[-1]:
            b += golds.pop(0)
        else:
            b += golds.pop(-1)
    return [a, b]

def distribution_of(golds):
    out, n, g = [0,0], 0, golds[::] #never directly edit supplied lists
    for i in range(len(golds)):
        if g[n] < g[-1]: x = g.pop()
        else:            x, n = g[n], n+1
        out[i&1] += x
    return out