Your task is to calculate logical value of boolean array. Test arrays are one-dimensional and their size is in the range 1-50.

Links referring to logical operations: [AND](https://en.wikipedia.org/wiki/Logical_conjunction), [OR](https://en.wikipedia.org/wiki/Logical_disjunction) and [XOR](https://en.wikipedia.org/wiki/Exclusive_or).

You should begin at the first value, and repeatedly apply the logical operation across the remaining elements in the array sequentially.

First Example:

Input: true, true, false, operator: AND

Steps: true AND true -> true, true AND false -> false

Output: false

Second Example:

Input: true, true, false, operator: OR

Steps: true OR true -> true, true OR false -> true

Output: true

Third Example:

Input: true, true, false, operator: XOR

Steps: true XOR true -> false, false XOR false -> false

Output: false
___

Input:

boolean array, string with operator' s name: 'AND', 'OR', 'XOR'.

Output:

calculated boolean

Difficulty: EASY
Starter code: def logical_calc(array, op):
	

Solutions:
import operator
from functools import reduce
OPS = {'AND': operator.and_, 'OR': operator.or_, 'XOR': operator.xor}

def logical_calc(array, op):
	return reduce(OPS[op], array)


def logical_calc(array, op):
	ops = {'AND': lambda x, y: x & y, 'OR': lambda x, y: x | y, 'XOR': lambda x, y: x ^ y}
	from functools import reduce
	return reduce(ops[op], array)


def logical_calc(array, op):
	res = array[0]
	for x in array[1:]:
		if op == 'AND':
			res &= x
		elif op == 'OR':
			res |= x
		else:
			res ^= x
	return res


def logical_calc(array, op):
	s = ' ' + op.lower().replace('xor', '^') + ' '
	return eval(s.join((str(x) for x in array)))


from functools import reduce

def logical_calc(array, op):
	return reduce(lambda x, y: {'OR': x or y, 'AND': x and y, 'XOR': x ^ y}[op], array)


def logical_calc(array, op):
	res = array[0]
	for i in array[1:]:
		if op == 'AND':
			res = res and i
		elif op == 'OR':
			res = res or i
		elif op == 'XOR':
			res = res != i
	return res


def logical_calc(array, op):
	result = False
	if len(array) == 1:
		return array[0]
	if op == 'AND':
		result = array[0] and array[1]
	if op == 'OR':
		result = array[0] or array[1]
	if op == 'XOR':
		result = array[0] ^ array[1]
	for n in range(2, len(array)):
		if op == 'AND':
			result = result and array[n]
		if op == 'OR':
			result = result or array[n]
		if op == 'XOR':
			result = result ^ array[n]
	return result


def logical_calc(array, op):
	return {'AND': all(array), 'OR': any(array)}.get(op) if op != 'XOR' else __import__('functools').reduce(lambda x, y: x ^ y, array)


def logical_calc(arr, op):
	return eval((' ^ ' if op == 'XOR' else ' ' + op.lower() + ' ').join((str(x) for x in arr)))


from functools import reduce

def logical_calc(array, op):
	if op == 'AND':
		return reduce(lambda x, y: x and y, array)
	elif op == 'OR':
		return reduce(lambda x, y: x or y, array)
	else:
		return reduce(lambda x, y: x ^ y, array)


ops = {'AND': '&', 'OR': '|', 'XOR': '^'}

def logical_calc(array, op):
	return bool(eval(ops[op].join([str(int(i)) for i in array])))


from operator import and_, or_, xor
from functools import reduce
logical_calc = lambda xs, op: reduce({'AND': and_, 'OR': or_, 'XOR': xor}.get(op), xs)


import operator
from functools import reduce

def logical_calc(array, op):
	op = '__{}__'.format(op.lower())
	op = getattr(operator, op)
	return reduce(op, array)


def logical_calc(array, op):
	return eval({'AND': '&', 'OR': '|', 'XOR': '^'}[op].join(map(str, array)))


from functools import reduce
from operator import ixor
from typing import List

def logical_calc(array: List[bool], op: str) -> bool:
	return {'AND': lambda : all(array), 'OR': lambda : any(array), 'XOR': lambda : reduce(ixor, array)}.get(op)()


def logical_calc(array, op):
	out = array.pop(0)
	for x in array:
		out = {'AND': x & out, 'OR': x | out, 'XOR': x ^ out}[op]
	return out


def logical_calc(array, op):
	return {'AND': all, 'OR': any, 'XOR': lambda a: sum(a) % 2}[op](array)


def logical_calc(array, op):
	if op == 'AND':
		for item in array:
			if item is False:
				return False
		return True
	elif op == 'OR':
		for item in array:
			if item is True:
				return True
		return False
	else:
		a = len([a for a in array if a is True])
		if a % 2 != 0 and a > 0:
			return True
		return False


from functools import reduce
logical_calc = lambda array, op: reduce(lambda a, b: a and b if op == 'AND' else a or b if op == 'OR' else a != b, array)


def logical_calc(array, op):
	if op == 'AND':
		return all(array)
	elif op == 'OR':
		return any(array)
	else:
		return sum(array) % 2 == 1


def logical_calc(array, op):
	if op == 'AND':
		return False not in array
	if op == 'OR':
		return True in array
	result = array[0]
	for index in range(1, len(array)):
		result ^= array[index]
	return result


def logical_calc(arr, op):
	return all(arr) if op == 'AND' else any(arr) if op == 'OR' else arr.count(True) % 2


def logical_calc(array, op):
	return eval({'AND': '&', 'OR': '|'}.get(op, '^').join((str(i) for i in array)))


import functools

def logical_calc(array, op):
	return {'AND': all, 'OR': any, 'XOR': xor}[op](array)

def xor(array):
	return functools.reduce(lambda x, y: x != y, array)


def logical_calc(array, op):
	if op == 'AND':
		return all((boolian for boolian in array))
	elif op == 'OR':
		return any((boolian for boolian in array))
	else:
		return sum(array) % 2


def logical_calc(array, op):
	if op == 'AND':
		return all(array)
	elif op == 'OR':
		return any(array)
	else:
		return array.count(True) % 2


def logical_calc(array, op):
	ans = array[0]
	for i in array[1:]:
		if op == 'AND':
			ans = ans & i
		elif op == 'OR':
			ans = ans | i
		else:
			ans = ans ^ i
	return ans


def logical_calc(array, op):
	ans = array[0]
	for operator in array[1:]:
		if op == 'AND':
			ans = ans and operator
		elif op == 'OR':
			ans = ans or operator
		else:
			ans = ans ^ operator
	return ans


def logical_calc(arr, op):
	answer = arr[0]
	if op == 'AND':
		for i in range(len(arr) - 1):
			answer &= arr[i + 1]
	if op == 'OR':
		for i in range(len(arr) - 1):
			answer |= arr[i + 1]
	if op == 'XOR':
		for i in range(len(arr) - 1):
			answer ^= arr[i + 1]
	return answer


def logical_calc(array, op):
	curr = array[0]
	for x in range(1, len(array)):
		if op == 'AND':
			curr = array[x] and curr
		if op == 'OR':
			curr = array[x] or curr
		if op == 'XOR':
			curr = array[x] and (not curr) or (not array[x] and curr)
	return curr


def logical_calc(array, op):
	start = array[0]
	for i in range(1, len(array)):
		if op == 'AND':
			start = start and array[i]
		elif op == 'OR':
			start = start or array[i]
		elif op == 'XOR':
			if start == array[i]:
				start = False
			else:
				start = True
	return start


def logical_calc(array, op):
	if op == 'AND':
		return not False in array
	elif op == 'OR':
		return True in array
	elif op == 'XOR':
		return array.count(True) % 2 != 0


def logical_calc(array, op):
	ops = {'XOR': ' ^ ', 'AND': ' and ', 'OR': ' or '}
	return eval(ops[op].join([str(v) for v in array]))


from functools import reduce

def logical_calc(array, op):
	return all(array) if op == 'AND' else any(array) if op == 'OR' else reduce(lambda x, y: x != y, array)


def logical_calc(array, op):
	state = None
	operation = None
	if op == 'AND':
		for i in range(len(array)):
			state = array[i] if i == 0 else state and array[i]
	elif op == 'OR':
		for i in range(len(array)):
			state = array[i] if i == 0 else state or array[i]
	elif op == 'XOR':
		for i in range(len(array)):
			state = array[i] if i == 0 else state ^ array[i]
	else:
		return f'input - {op} - not recognized'
	return state


def logical_calc(array, op):
	print(array)
	print(op)
	print(len(array))
	count_True = 0
	count_False = 0
	for line in array:
		if line == True:
			count_True += 1
		else:
			count_False += 1
	print(count_False)
	print(count_True)
	if op == 'AND':
		if count_True == len(array):
			return True
		else:
			return False
	elif op == 'OR':
		if count_False == len(array):
			return False
		else:
			return True
	elif op == 'XOR':
		if count_True % 2 == 0:
			return False
		else:
			return True


def logical_calc(array, op):
	if len(array) == 1:
		return array[0]
	else:
		if op.lower() == 'and':
			s = array[0] & array[1]
			for i in range(2, len(array)):
				s = s & array[i]
			return s
		elif op.lower() == 'or':
			s = array[0] | array[1]
			for i in range(2, len(array)):
				s = s | array[i]
			return s
		if op.lower() == 'xor':
			s = array[0] ^ array[1]
			for i in range(2, len(array)):
				s = s ^ array[i]
			return s


logical_calc = lambda a, o: __import__('functools').reduce({'AND': lambda a, b: a & b, 'OR': lambda a, b: a | b, 'XOR': lambda a, b: a ^ b}[o], a, o == 'AND')


def logical_calc(array, op):
	bool = array[0]
	for x in range(1, len(array)):
		if op == 'AND':
			bool = array[x] & bool
		if op == 'OR':
			bool = array[x] | bool
		if op == 'XOR':
			bool = array[x] ^ bool
	return bool


from functools import reduce

def logical_calc(array, op):
	operators = {'AND': all(array), 'OR': any(array), 'XOR': reduce(lambda x, y: x ^ y, array)}
	return operators[op]


dict = {True: 1, False: 2}

def logical_calc(array, op):
	print(array, op)
	arrn = []
	length = len(array)
	for x in array:
		arrn.append(dict[x])
	if op == 'XOR':
		if sum(arrn) == 1:
			return True
		elif sum(arrn) == 2:
			return False
		else:
			i = 0
			x = array[0]
			while i < len(array) - 1:
				x = x == array[i + 1]
				i += 1
			if x:
				return False
			else:
				return True
	elif sum(arrn) == length and op == 'AND':
		return True
	elif array.count(False) == length:
		return False
	elif (sum(arrn) != length or sum(arrn) != length * 2) and op == 'OR':
		return True
	else:
		return False


def logical_calc(array, op):
	print(array, op)
	if op == 'AND':
		if array.count(False):
			return False
	elif op == 'OR':
		if array.count(True) == 0:
			return False
	elif array.count(True) % 2 == 0:
		return False
	return True


import distutils

def quick_op(op, old, new):
	if op == 'AND':
		return old and new
	elif op == 'OR':
		return old or new
	elif op == 'XOR':
		return old ^ new

def logical_calc(array, op):
	latest_operation = array[0]
	array.pop(0)
	for item in array:
		latest_operation = quick_op(op, latest_operation, item)
	return latest_operation


def logical_calc(array, op):
	if op == 'XOR':
		result = array[0]
		for element in array[1:]:
			if element == result:
				result = False
			else:
				result = True
		return result
	return all(array) if op == 'AND' else any(array)


from functools import reduce

def logical_calc(array, op):
	oper = op.lower() if op != 'XOR' else '^'
	return reduce(lambda x, y: eval(f'{x} {oper} {y}'), array)


def logical_calc(array, op):
	if op == 'AND':
		return array.count(False) == 0
	elif op == 'OR':
		return array.count(True) != 0
	else:
		return array.count(True) % 2 == 1


from operator import __and__, __or__, __xor__
operators = {'AND': __and__, 'OR': __or__, 'XOR': __xor__}

def logical_calc(arr, op):
	acc = arr[0]
	for item in arr[1:]:
		acc = operators[op](acc, item)
	return acc


from operator import and_
from operator import or_
from operator import xor
from functools import reduce

def logical_calc(array, op):
	if op == 'XOR':
		return reduce(xor, array)
	elif op == 'AND':
		return reduce(and_, array)
	elif op == 'OR':
		return reduce(or_, array)


def logical_calc(array, op):
	bool = array[0] == True
	for i in range(1, len(array)):
		if op == 'AND':
			bool = bool & array[i]
		elif op == 'OR':
			bool = bool | array[i]
		elif op == 'XOR':
			bool = bool ^ array[i]
	return bool == True


def logical_calc(array, op):
	if op == 'AND':
		for bool in array:
			if bool == False:
				return False
		return True
	elif op == 'OR':
		for bool in array:
			if bool == True:
				return True
		return False
	else:
		aux = array[0]
		i = 1
		while i < len(array):
			if aux != array[i]:
				aux = True
			else:
				aux = False
			i += 1
		return aux


def logical_calc(array, op):
	return eval(' {} '.format({'AND': 'and', 'OR': 'or'}.get(op, '^')).join((str(x) for x in array)))


def logical_calc(array, op):
	val = array.pop()
	for i in array:
		if op == 'AND':
			if not i:
				return False
		elif op == 'OR':
			if i:
				return True
		else:
			val ^= i
	return val


def logical_calc(array, op):
	print(array, op)
	if op == 'AND':
		return False not in array
	if op == 'OR':
		return True in array or (True in array and False in array)
	if op == 'XOR':
		return array.count(True) % 2 != 0 or (True in array and array.count(False) % 2 != 0)


def logical_calc(array, op):
	if op == 'XOR':
		op = '^'
	op = op.lower()
	array = [str(i) for i in array]
	arr2 = []
	for i in range(len(array)):
		arr2.append(array[i])
		arr2.append(op)
	arr2 = arr2[0:-1]
	return eval(' '.join(arr2))


import functools

def logical_calc(array, op):
	if op == 'AND':
		return functools.reduce(lambda prev, curr: prev and curr, array)
	if op == 'OR':
		return functools.reduce(lambda prev, curr: prev or curr, array)
	if op == 'XOR':
		return functools.reduce(lambda prev, curr: prev ^ curr, array)


def logical_calc(array, op):
	if False in array and op == 'AND':
		return False
	elif op == 'AND':
		return True
	elif True in array and op == 'OR':
		return True
	elif op == 'OR':
		return False
	elif int(array.count(True)) % 2 == 0 and int(array.count(False)) % 2 == 0 and (op == 'XOR'):
		return False
	elif int(array.count(True)) % 2 == 1 and op == 'XOR':
		return True
	elif int(array.count(False)) % 2 == 1 and int(array.count(True)) % 2 == 1 and (op == 'XOR'):
		return False
	elif int(array.count(False)) % 2 == 1 and op == 'XOR':
		return False


def logical_calc(array, op):
	log = array[0]
	for i in array[1:51]:
		if op == 'AND':
			log = log and i
		elif op == 'OR':
			log = log or i
		else:
			log = log ^ i
	return log


def logical_calc(array, op):
	if op == 'AND':
		return False not in array
	elif op == 'OR':
		return True in array
	elif op == 'XOR':
		s1 = sum(array)
		if s1 % 2 == 0:
			return False
		else:
			return True


def logical_calc(array, op):
	bool = array[0]
	if op == 'AND':
		for n in range(len(array) - 1):
			bool = bool and array[n + 1]
	elif op == 'OR':
		for n in range(len(array) - 1):
			bool = bool or array[n + 1]
	elif op == 'XOR':
		for n in range(len(array) - 1):
			bool = (bool or array[n + 1]) and (not (bool and array[n + 1]))
	else:
		bool = False
	return bool


def logical_calc(array, op):
	logic = ''
	if op == 'XOR':
		op = '^'
	for x in array[0:len(array) - 1]:
		logic = logic + ' ' + str(x) + ' ' + op.lower()
	logic = logic + ' ' + str(array[-1])
	ans = eval(logic.strip())
	return ans


def logical_calc(arr, log):
	x = {'AND': 'and', 'OR': 'or', 'XOR': '!='}
	y = arr.pop(0)
	while len(arr) > 0:
		y = eval(str(y) + f' {x[log]} ' + str(arr.pop(0)))
	return y


def xor(a, b):
	return a + b - 2 * a * b

def logical_calc(array, op):
	if op == 'OR':
		result = array[0]
		for value in range(1, len(array)):
			result = result or array[value]
		return result
	if op == 'AND':
		result = array[0]
		for value in range(1, len(array)):
			result = result and array[value]
		return result
	if op == 'XOR':
		result = array[0]
		for value in range(1, len(array)):
			result = xor(result, array[value])
		return result


def logical_calc(array, op):
	a = array[0]
	if op == 'AND':
		for i in range(len(array) - 1):
			a = a and array[i + 1]
		return bool(a)
	elif op == 'OR':
		for i in range(len(array) - 1):
			a = a or array[i + 1]
		return bool(a)
	elif op == 'XOR':
		for i in range(len(array) - 1):
			a = a != array[i + 1]
		return bool(a)


def logical_calc(array, op):
	print((array, op))
	if op == 'AND':
		return not False in array
	if op == 'OR':
		return True in array
	status = False
	for bool in array:
		status = bool ^ status
	return status


import functools

def logical_calc(array, op):
	arr = {'AND': lambda a, b: a and b, 'OR': lambda a, b: a or b, 'XOR': lambda a, b: a != b}
	return functools.reduce(arr[op], array)


def logical_calc(array, op):
	if op == 'AND':
		return all(array)
	elif op == 'OR':
		return any(array)
	elif op == 'XOR':
		return array.count(True) % 2 != 0


def logical_calc(array, op):
	return eval(f" {(op.lower() if op != 'XOR' else '^')} ".join(map(str, array)))


from operator import __and__, __or__, __xor__
from functools import reduce

def logical_calc(a, op):
	if op == 'AND':
		return reduce(__and__, a)
	if op == 'OR':
		return reduce(__or__, a)
	else:
		return reduce(__xor__, a)


def logical_calc(array, op):
	import functools
	if op == 'AND':
		return functools.reduce(lambda i, j: i and j, array)
	elif op == 'OR':
		return functools.reduce(lambda i, j: i or j, array)
	elif op == 'XOR':
		return functools.reduce(lambda i, j: i ^ j, array)


from functools import reduce

def logical_calc(array, op):
	if op == 'AND':
		return reduce(lambda a, b: a and b, array)
	if op == 'OR':
		return reduce(lambda a, b: a or b, array)
	return reduce(lambda a, b: a ^ b, array)


from functools import reduce

def logical_calc(array, op):
	if op == 'XOR':
		op = '^'
	return reduce(lambda a, b: eval(str(a) + ' ' + op.lower() + ' ' + str(b)), array)


def logical_calc(array, op):
	c = len(array)
	res = array[0]
	for i in range(c - 1):
		if op == 'AND':
			res = res and array[i + 1]
		if op == 'OR':
			res = res or array[i + 1]
		if op == 'XOR':
			res = res != array[i + 1]
	return res


def logical_calc(array, op):
	res = array[0]
	if op == 'AND':
		for i in range(1, len(array)):
			res *= array[i]
	elif op == 'OR':
		for i in range(1, len(array)):
			res |= array[i]
	else:
		for i in range(1, len(array)):
			res ^= array[i]
	return res


def logical_calc(array, op):
	op = op.lower()
	if op == 'xor':
		op = '!='
	for id in range(len(array) - 1):
		array[id + 1] = eval(str(array[id]) + ' ' + op + ' ' + str(array[id + 1]))
	return array[-1]


def logical_calc(ar, op):
	if op == 'AND':
		return False not in ar
	elif op == 'OR':
		return True in ar
	elif op == 'XOR':
		if len(ar) == 2:
			return not ar[0] == ar[1]
		elif len(ar) > 2:
			a = ar.pop(0)
			b = ar.pop(0)
			ar.insert(0, not a == b)
			return logical_calc(ar, op)
		elif len(ar) == 1:
			return ar[0]