I have started studying electronics recently, and I came up with a circuit made up of 2 LEDs and 3 buttons.

Here 's how it works: 2 buttons (`red` and `blue`) are connected to the LEDs (`red` and `blue` respectively). Buttons pressing pattern will be remembered and represented through the LEDs when the third button is pressed.

 - Only one LED can blink at a time.
 - The LED will only blink once even if the button is held down.
 - The button must be released to be pressed again.
 - If a button is pressed while the other button is being held down, it will be ignored.
 - If two buttons are pressed simultaneously, the red button will be preferred.
 - If a button is released while the other is being held down, the other 's LED will blink.
 - `0` is up and `1` is down.
 - The two inputs will always have the same length.
 
Here is an example:

```Python
Red:  "10011010"
Blue: "10110111"
#=> "RBRB"

Red:  "01001000"
Blue: "01011100"
#=> "RB"

Red:  "01101000"
Blue: "00111000"
#=> "RB"
```

PS:

This is my first time making a kata, so there may be some errors. 
You may report to me if the description is too confusing. 
Sorry for my poor grammar.

Source: codewars | Difficulty: EASY
Tags: String algorithms

Game theory

Ad-hoc

Starter code: def button_sequences(seqR, seqB):
	

Solutions:
def button_sequences(seqR, seqB):
	(pattern, state) = ('', '')
	toBool = lambda seq: [i == '1' for i in seq]
	for (red, blue) in zip(toBool(seqR), toBool(seqB)):
		if red and state == 'R' or (blue and state == 'B'):
			continue
		state = 'R' if red else 'B' if blue else ''
		pattern += state
	return pattern


def button_sequences(red, blue):
	s = ''
	(r, b) = (False, False)
	for i in range(len(red)):
		if red[i] == '0':
			r = False
		if blue[i] == '0':
			b = False
		if red[i] == '1' and (not r) and (not b):
			r = True
			s += 'R'
		elif blue[i] == '1' and (not r) and (not b):
			b = True
			s += 'B'
	return s


def button_sequences(seqR, seqB):
	(A, B, res) = (1, 1, '')
	for (x, y) in zip(map(int, seqR), map(int, seqB)):
		if A and B and x:
			y = 0
		if A and x and (not y):
			res += 'R'
		if B and (not x) and y:
			res += 'B'
		if not (x and y):
			(A, B) = (not x, not y)
	return res


def button_sequences(seqR, seqB):
	(state, s) = ('', '')
	for (r, b) in zip([c == '1' for c in seqR], [c == '1' for c in seqB]):
		if state == 'R' and (not r) or (state == 'B' and (not b)):
			state = ''
		if not state and r:
			(s, state) = (s + 'R', 'R')
		elif not state and b:
			(s, state) = (s + 'B', 'B')
	return s


from itertools import groupby

def button_sequences(seq_r, seq_b):
	pairs = [''.join(pair) for pair in zip(seq_r, seq_b)]
	blinks = ('' if rb == '00' else 'B' if rb == '01' else 'R' if rb == '10' or prev == '00' else '-' for (rb, prev) in zip(pairs, ['00'] + pairs))
	return ''.join((key for (key, _) in groupby((x for x in blinks if x != '-'))))


def button_sequences(*seqRB):
	(wasR, wasB, seq) = (0, 0, [])
	for (r, b) in zip(*map(lambda s: map(int, s), seqRB)):
		what = 'BR'[r]
		realeaseOneofTwo = wasR + wasB == 2 and r + b == 1 and (what != seq[-1])
		freshPress = not wasR + wasB and r + b
		switchButtons = b + r == 1 and (wasR, wasB) == (b, r)
		if realeaseOneofTwo or freshPress or switchButtons:
			seq.append(what)
		(wasR, wasB) = (r, b)
	return ''.join(seq)


button_sequences = lambda R, B: (lambda r, f: r.sub('(.)\\1+', '\\1', f.reduce(lambda x, y: x + ('_' if y == ('0', '0') else 'R' if y == ('1', '0') else 'B' if y == ('0', '1') else 'R' if x[-1:] in ['', '_', 'R'] else 'B'), zip(R, B), '')).replace('_', ''))(__import__('re'), __import__('functools'))


def button_sequences(r, b):
	(r_on, b_on) = (False, False)
	res = ''
	for i in range(len(r)):
		(ri, bi) = (int(r[i]), int(b[i]))
		if not r_on | b_on:
			if ri:
				res += 'R'
				r_on = True
			elif bi:
				res += 'B'
				b_on = True
		elif r_on:
			if not ri:
				r_on = False
				if bi:
					b_on = True
					res += 'B'
		elif b_on:
			if not bi:
				b_on = False
				if ri:
					r_on = True
					res += 'R'
	return res


def button_sequences(seqR, seqB):
	result = ''
	prec_r = '0'
	prec_b = '0'
	seqR = list(seqR)
	seqB = list(seqB)
	for (r, b) in zip(seqR, seqB):
		if r == b and r == '1' and (prec_r != '1') and (prec_b != '1'):
			result += 'R'
		elif r != b and r == '1' and (prec_r != '1'):
			result += 'R'
		elif r != b and b == '1' and (prec_b != '1'):
			result += 'B'
		elif prec_r == prec_b and prec_r == '1' and (r == '1') and (b != r) and (result[len(result) - 1] != 'R'):
			result += 'R'
		elif prec_r == prec_b and prec_r == '1' and (b == '1') and (b != r) and (result[len(result) - 1] != 'B'):
			result += 'B'
		prec_r = r
		prec_b = b
	return result


def button_sequences(seqR, seqB):
	seq = ''
	leds = ['R', 'B']
	action = [None, None]
	for (r, b) in zip(seqR, seqB):
		r = int(r)
		b = int(b)
		for (ind, button) in enumerate([r, b]):
			if button and (not action[ind]):
				action[ind] = 'press'
			elif button and action[ind]:
				action[ind] = 'held'
			elif action[ind] and (not button):
				action[ind] = 'release'
		for ind in [0, 1]:
			if action[ind] == 'press' and action[ind - 1] != 'held':
				seq += leds[ind]
				break
			elif action[ind] == 'held' and action[ind - 1] == 'release':
				if seq[-1] == leds[ind]:
					continue
				seq += leds[ind]
				break
		if action[0] == 'release':
			action[0] = None
		if action[1] == 'release':
			action[1] = None
	return seq