# Introduction:

Reversi is a game usually played by 2 people on a 8x8 board.
Here we're only going to consider a single 8x1 row.

Players take turns placing pieces, which are black on one side and white on the
other, onto the board with their colour facing up.  If one or more of the
opponents pieces are sandwiched by the piece just played and another piece of
the current player's colour, the opponents pieces are flipped to the
current players colour.

Note that the flipping stops when the first piece of the player's colour is reached.

# Task:

Your task is to take an array of moves and convert this into a string
representing the state of the board after all those moves have been played.

# Input:

The input to your function will be an array of moves.
Moves are represented by integers from 0 to 7 corresponding to the 8 squares on the board.
Black plays first, and black and white alternate turns.
Input is guaranteed to be valid. (No duplicates, all moves in range, but array may be empty)

# Output:

8 character long string representing the final state of the board.
Use '*' for black and 'O' for white and '.' for empty.

# Examples:
```python
  reversi_row([])      # '........'
  reversi_row([3])     # '...*....'
  reversi_row([3,4])   # '...*O...'
  reversi_row([3,4,5]) # '...***..'
```

Source: codewars | Difficulty: EASY
Tags: Game theory

Starter code: def reversi_row(moves):
	

Solutions:
import re

def reversi_row(moves):
	row = '........'
	stones = '*O'
	for (i, m) in enumerate(moves):
		(L, M, R) = (row[:m], stones[i % 2], row[m + 1:])
		if R != '' and R[0] == stones[(i + 1) % 2] and (R.find(stones[i % 2]) > 0) and ('.' not in R[:R.find(stones[i % 2])]):
			R = R.replace(stones[(i + 1) % 2], stones[i % 2], R.find(stones[i % 2]))
		if L != '' and L[-1] == stones[(i + 1) % 2] and (L[::-1].find(stones[i % 2]) > 0) and ('.' not in L[-1 - L[::-1].find(stones[i % 2]):]):
			L = L[::-1].replace(stones[(i + 1) % 2], stones[i % 2], L[::-1].find(stones[i % 2]))[::-1]
		row = L + M + R
	return row


from re import sub

def reversi_row(moves):
	current = '.' * 8
	for (i, x) in enumerate(moves):
		(c1, c2) = 'Ox' if i & 1 else 'xO'
		left = sub('(?<={}){}+$'.format(c1, c2), lambda r: c1 * len(r.group()), current[:x])
		right = sub('^{}+(?={})'.format(c2, c1), lambda r: c1 * len(r.group()), current[x + 1:])
		current = left + c1 + right
	return current.replace('x', '*')


def reversi_row(moves):
	row = ['.'] * 8
	for (turn, move) in enumerate(moves):
		tile = '*O'[turn % 2]
		row[move] = tile
		for direction in (-1, 1):
			other = move + direction
			while 0 <= other < 8 and row[other] != '.':
				if row[other] == tile:
					break
				other += direction
			else:
				other = move
			row[move:other:direction] = [tile] * abs(move - other)
	return ''.join(row)


def reversi_row(moves):
	stones = [set(), set()]
	for (i, p) in enumerate(moves):
		(curr, oppn) = (stones[i % 2], stones[(i + 1) % 2])
		curr.add(p)
		(l, r) = (p - 1, p + 1)
		while l in oppn:
			l -= 1
		while r in oppn:
			r += 1
		flip = set(range(l + 1 if l in curr else p + 1, r if r in curr else p))
		oppn -= flip
		curr |= flip
	return ''.join(('*' if i in stones[0] else 'O' if i in stones[1] else '.' for i in range(8)))


from itertools import cycle

def reversi_row(moves):
	row = ['.'] * 8
	player = cycle('*O')
	for move in moves:
		current_player = next(player)
		invalid = {'.', current_player}
		row[move] = current_player
		row_str = ''.join(row)
		first_left = row_str.rfind(current_player, 0, move)
		if first_left != -1:
			left_chunk = row_str[first_left + 1:move]
			if not invalid.intersection(left_chunk):
				row[first_left + 1:move] = [current_player] * len(left_chunk)
		first_right = row_str.find(current_player, move + 1)
		if first_right != -1:
			right_chunk = row_str[move + 1:first_right]
			if not invalid.intersection(right_chunk):
				row[move + 1:first_right] = [current_player] * len(right_chunk)
	return ''.join(row)


from functools import reduce

def reversi_row(moves):
	return ''.join(reduce(lambda board, x: play(board, x[1], 'O' if x[0] % 2 else '*'), enumerate(moves), ['.'] * 8))

def play(board, position, pawn):
	board[position] = pawn
	opp = 'O' if pawn == '*' else '*'
	after = next((x for x in [opp * i + pawn for i in range(6)] if ''.join(board[position + 1:]).startswith(x)), None)
	if after:
		board[position + 1:position + len(after)] = [pawn] * (len(after) - 1)
	before = next((x for x in [pawn + opp * i for i in range(6)] if ''.join(board[:position]).endswith(x)), None)
	if before:
		board[position - len(before):position] = [pawn] * len(before)
	return board


from re import sub

def reversi_row(moves):
	base = '........'
	point = '*'
	for move in moves:
		base = list(base)
		base[move] = '#'
		base = ''.join(base)
		base = sub('#O+\\*', lambda m: '*' * len(m.group(0)), sub('\\*O+#', lambda m: '*' * (len(m.group(0)) - 1) + '#', base)) if point == '*' else sub('#\\*+O', lambda m: 'O' * len(m.group(0)), sub('O\\*+#', lambda m: 'O' * (len(m.group(0)) - 1) + '#', base))
		base = base.replace('#', point)
		point = 'O' if point == '*' else '*'
	return base


def reversi_row(a):
	r = ['.'] * 8
	for (p, i) in enumerate(a):
		(cur, opp) = ('*O'[p % 2], '*O'[1 - p % 2])
		r[i] = cur
		for range_ in (range(i - 1, -1, -1), range(i + 1, 8)):
			for j in range_:
				if r[j] == '.':
					break
				if r[j] == cur:
					if abs(i - j) > 1:
						r[min(i, j):max(i, j)] = cur * abs(i - j)
					break
	return ''.join(r)


import re

def reversi_row(moves):
	res = '.' * 8
	star = False
	for move in moves:
		star = not star
		assert res[move] == '.'
		res = res[:move] + '@' + res[move + 1:]
		if star:
			res = re.sub('(@)(O+)(\\*)', lambda g: g[1] + '*' * len(g[2]) + g[3], res)
			res = re.sub('(\\*)(O+)(@)', lambda g: g[1] + '*' * len(g[2]) + g[3], res)
			res = res.replace('@', '*')
		else:
			res = re.sub('(@)(\\*+)(O)', lambda g: g[1] + 'O' * len(g[2]) + g[3], res)
			res = re.sub('(O)(\\*+)(@)', lambda g: g[1] + 'O' * len(g[2]) + g[3], res)
			res = res.replace('@', 'O')
	return res