A stick is balanced horizontally on a support. Will it topple over or stay balanced? (This is a physics problem: imagine a real wooden stick balanced horizontally on someone's finger or on a narrow table, for example). 

The stick is represented as a list, where each entry shows the mass in that part of the stick.

The stick is balanced on a support. The "terrain" is represented by a list of 1s and 0s, where the 1s represent the support and the 0s represent empty space. Each index is a coordinate on the x-axis, so e.g. the physical interpretations of some terrains are as follows:

![](https://i.imgur.com/P4MpHZV.png)

The stick will only balance if its centre of mass is directly above some part of its support. Return `True` if the stick will balance and `False` if it will topple.

Both lists will be of equal length (that is, the stick and the terrain have equal width so that each index of the stick is directly above the corresponding index of the terrain). Every terrain will contain one, and only one, support.

## Examples:

![](https://i.imgur.com/PyMBsAl.png)

```
[2,3,2]
[0,1,0] ---> Perfectly balanced, return True

[5,1,1]
[0,1,0] ---> will topple to the left, return False

[3,3,4]
[0,1,0] ---> will topple to the right (but only just)

[7,1,1,1,1,1,1,1]
[0,1,1,0,0,0,0,0] ---> will balance, perfectly, without a doubt

[5, 3, 2, 8, 6]
[0, 0, 0, 1, 1] ---> will topple to the left

```

Difficulty: introductory

Starter code:

def will_it_balance(stick, terrain):
	

Solutions:
from math import ceil

def will_it_balance(stick, gnd):
    gravPt = sum(v*i for i,v in enumerate(stick)) / sum(stick)
    return gnd[int(gravPt)] == gnd[ceil(gravPt)] == 1

def will_it_balance(stick, terrain):
    com = sum(i*w for i,w in enumerate(stick))/sum(stick)
    return terrain.index(1)<=com<=max(i for i,v in enumerate(terrain) if v==1)

from scipy.ndimage.measurements import center_of_mass
from math import floor, ceil
from numpy import array

def will_it_balance(stick, terrain):
    center = center_of_mass(array(stick))[0]
    return terrain[floor(center)] and terrain[ceil(center)]

from math import ceil, floor

def will_it_balance(stick, terrain):
    c = sum(i*m for i, m in enumerate(stick, 1)) / sum(stick)
    a, b = floor(c), ceil(c)
    return all(x or not (a <= i <= b) for i, x in enumerate(terrain, 1))

def will_it_balance(stick, terrain):
    center_of_mass = sum(i * mass for i, mass in enumerate(stick)) / sum(stick)
    terrain_ones = [i for i, x in enumerate(terrain) if x]
    left, right = terrain_ones[0], terrain_ones[-1]
    return left <= center_of_mass <= right

will_it_balance=lambda a,s:s.index(1)<=sum(i*a[i]for i in range(len(a)))/sum(a)<=len(s)-s[::-1].index(1)-1

import math

def will_it_balance(stick, terrain):
    numerator = 0
    denominator = 0
    for index, weight in enumerate(stick):
        numerator += weight * (index+1)
        denominator += weight
    centerOfMass = numerator/denominator
    centerOfMassLowCoord, centerOfMassHighCoord = math.floor(centerOfMass), math.ceil(centerOfMass)
    return terrain[centerOfMassLowCoord-1]==1 and terrain[centerOfMassHighCoord-1]==1

def will_it_balance(stick, terrain):
    c=0
    T=[]
    for i in range(len(stick)):
        c=c+i*stick[i]
    m=c/sum(stick)
    for i in range(len(terrain)):
        if terrain[i]==1:
            T.append(i)
    
    return (len(T)>=2 and T[0]<=m<=T[-1])  or   (len(T)==1 and T[0]==m )


from operator import mul,add
from functools import reduce
def will_it_balance(stick, terrain):
    masses = enumerate(stick)
    com = reduce(add,[i*j for i,j in masses])/reduce(add,stick)
    start = terrain.index(1)
    end = [l[0] for l in enumerate(terrain) if l[1] == 1][-1]
    return True if com>= start and com<= end else False

def will_it_balance(stick, terrain):
    return terrain.index(1) \
           <= sum(m*x for x, m in enumerate(stick)) / sum(stick) \
           <= (len(terrain) - 1 - terrain[::-1].index(1))