# Task

Given a binary number, we are about to do some operations on the number. Two types of operations can be here:

* ['I', i, j] : Which means invert the bit from i to j (inclusive).

* ['Q', i] : Answer whether the i'th bit is 0 or 1.

The MSB (most significant bit) is the first bit (i.e. i = `1`). The binary number can contain leading zeroes.

## Example
```python
binary_simulation("0011001100", [['I', 1, 10], ['I', 2, 7], ['Q', 2], ['Q', 1], ['Q', 7], ['Q', 5]]) === [ '0', '1', '1', '0' ];
binary_simulation("1011110111", [['I', 1, 10], ['I', 2, 7], ['Q', 2], ['Q', 1], ['Q', 7], ['Q', 5]]) === [ '0', '0', '0', '1' ];
binary_simulation("1011110111", [['I', 1, 10], ['I', 2, 7]]) === [];
binary_simulation("0000000000", [['I', 1, 10], ['Q', 2]]) ===  ['1'];
```
## Note
* All inputs are valid.
* Please optimize your algorithm to avoid time out.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def binary_simulation(s, q):
	

Solutions:
def binary_simulation(s, q):
	(out, n, s) = ([], int(s, 2), len(s))
	for (cmd, *i) in q:
		if cmd == 'I':
			(a, b) = i
			n ^= (1 << b - a + 1) - 1 << s - b
		else:
			out.append(str(int(0 < 1 << s - i[0] & n)))
	return out


import numpy as np

def binary_simulation(s, b):
	(a, r) = (np.fromiter(map(int, s), dtype=np.int), [])
	for x in b:
		if x[0] == 'Q':
			r.append(str(a[x[1] - 1]))
		else:
			(i, j) = x[1:]
			a[i - 1:j] ^= 1
	return r


def binary_simulation(s, q):
	(x, l, result) = (int(s, 2), len(s), [])
	for op in q:
		if op[0] == 'I':
			(i, j) = op[1:]
			x ^= (1 << j - i + 1) - 1 << l - j
		else:
			result.append(str(x >> l - op[1] & 1))
	return result


def binary_simulation(bits, seq):
	arr = [0 for _ in range(len(bits) + 1)]
	display = []
	for grp in seq:
		if grp[0] == 'I':
			arr[grp[1] - 1] += 1
			arr[grp[2]] += -1
		else:
			display.append('01'[sum(arr[:grp[1]]) + int(bits[grp[1] - 1]) & 1])
	return display


def binary_simulation(s, q):
	(o, l, n) = ([], len(s), int(s, 2))
	for (c, *_) in q:
		if c == 'Q':
			(i,) = _
			o.append(str(n >> l - i & 1))
		else:
			(i, j) = _
			n ^= (1 << l - i + 1) - (1 << l - j)
	return o


def binary_simulation(s, r):
	ix = tuple((1 << n for n in range(len(s) + 1)))[::-1]
	tome = []
	n = int(s, 2)
	for (v, *x) in r:
		if v == 'I':
			n ^= ix[x[0] - 1] - ix[x[1]]
		else:
			tome.append(str(min(1, n & ix[x[0]])))
	return tome


def binary_simulation(s, q):
	outlst = []
	for op in q:
		ini = op[1] - 1
		if op[0] == 'Q':
			outlst.append(s[ini])
		elif op[0] == 'I':
			transl = s[ini:op[2]].maketrans('01', '10')
			s = s[:ini] + s[ini:op[2]].translate(transl) + s[op[2]:]
	return outlst


def binary_simulation(Q, S):
	(Q, L, R) = (int(Q, 2), len(Q), [])
	for S in S:
		if S[0] < 'L':
			Q ^= (1 << L - S[1] + 1) - 1
			Q ^= (1 << L - S[2]) - 1
		else:
			R.append('1' if Q & 1 << L - S[1] else '0')
	return R


def binary_simulation(s, q):
	n = int(s[::-1], 2)
	r = []
	for x in q:
		if x[0] == 'I':
			(_, a, b) = x
			n ^= (1 << b - a + 1) - 1 << a - 1
		else:
			(_, a) = x
			r += ['01'[n & 1 << a - 1 > 0]]
	return r


def binary_simulation(s, q):
	s = '0' + s
	ans = []
	for val in q:
		if val[0] == 'I':
			s = s[:val[1]] + s[val[1]:val[2] + 1].translate(''.maketrans('01', '10')) + s[val[2] + 1:]
		elif val[0] == 'Q':
			ans.append(s[val[1]])
	return ans