# Task
 `N` candles are placed in a row, some of them are initially lit. For each candle from the 1st to the Nth the following algorithm is applied: if the observed candle is lit then states of this candle and all candles before it are changed to the opposite. Which candles will remain lit after applying the algorithm to all candles in the order they are placed in the line?

# Example

 For `a = [1, 1, 1, 1, 1]`, the output should be `[0, 1, 0, 1, 0].`

 Check out the image below for better understanding:
 
 

 ![](https://codefightsuserpics.s3.amazonaws.com/tasks/switchLights/img/example.png?_tm=1484040239470)
 
 
 
 For `a = [0, 0]`, the output should be `[0, 0].`

 The candles are not initially lit, so their states are not altered by the algorithm.

# Input/Output

 - `[input]` integer array `a`

     Initial situation - array of zeros and ones of length N, 1 means that the corresponding candle is lit.

    Constraints: `2 ≤ a.length ≤ 5000.`

 - `[output]` an integer array

    Situation after applying the algorithm - array in the same format as input with the same length.

Source: codewars | Difficulty: EASY
Tags: Ad-hoc

Starter code: def switch_lights(a):
	

Solutions:
def switch_lights(initial_states):
	states = list(initial_states)
	parity = 0
	for i in reversed(range(len(states))):
		parity ^= initial_states[i]
		states[i] ^= parity
	return states


def switch_lights(a):
	n = sum(a)
	for (i, x) in enumerate(a):
		if n % 2:
			a[i] ^= 1
		n -= x
	return a


def switch_lights(a):
	s = sum(a)
	result = []
	for i in range(len(a) - 1):
		s -= a[i]
		result.append(s % 2)
	return result + [0]


from itertools import accumulate, chain
from operator import xor

def switch_lights(a):
	return list(accumulate(chain([0], reversed(a)), xor))[-2::-1]


def switch_lights(a):
	i = len(a) - 1
	s = 0
	while i >= 0:
		if a[i]:
			s += 1
		if s:
			a[i] = (a[i] + s) % 2
		i -= 1
	return a


def switch_lights(a):
	(i, l) = (sum(a) % 2, [])
	for n in a:
		i ^= n
		l.append(i)
	return l


def switch_lights(a):
	ones = a.count(1)
	for (i, x) in enumerate(a):
		if ones % 2:
			a[i] = 0 if x else 1
		ones -= x
	return a


def switch_lights(a):
	S = sum(a) % 2
	for i in range(len(a)):
		(a[i], S) = ([a[i], 1 - a[i]][S == 1], [S, 1 - S][a[i] == 1])
	return a


def switch_lights(a):
	s = sum(a)
	for i in range(len(a)):
		if s % 2:
			s -= a[i]
			a[i] = 1 - a[i]
		else:
			s -= a[i]
	return a


def switch_lights(a):
	z = a[::-1]
	status = 0
	for i in range(len(z)):
		status = status + z[i]
		if status % 2 == 1:
			if z[i] == 1:
				z[i] = 0
			elif z[i] == 0:
				z[i] = 1
	return z[::-1]