Create a function that interprets code in the esoteric language **Poohbear**

## The Language

Poohbear is a stack-based language largely inspired by Brainfuck. It has a maximum integer value of 255, and 30,000 cells. The original intention of Poohbear was to be able to send messages that would, to most, be completely indecipherable: 
Poohbear Wiki

* For the purposes of this kata, you will make a version of Poohbear that has **infinite** memory cells in **both directions** (so you do not need to limit cells to 30,000)
* Cells have a default value of 0
* Each cell can hold one byte of data.  Once a cell's value goes above 255, it wraps around to 0.  If a cell's value goes below 0, it wraps to 255.
* If the result of an operation isn't an int, round the result down to the nearest one.
* Your interpreter should ignore any non-command characters in the code.
* If you come to a `W` in the code and the current cell is equal to 0, jump to the corresponding `E`.
* If you come to an `E` in the code and the current cell is **not** 0, jump back to the corresponding `W`.

Here are the Poohbear commands: 

| Command | Definition
|---| -------------------------
| + | Add 1 to the current cell
| - | Subtract 1 from the current cell
| > | Move the cell pointer 1 space to the right
| < | Move the cell pointer 1 space to the left
| c | "Copy" the current cell
| p | Paste the "copied" cell into the current cell
| W | While loop - While the current cell is not equal to 0
| E | Closing character for loops
| P | Output the current cell's value as ascii
| N | Output the current cell's value as an integer
| T | Multiply the current cell by 2
| Q | Square the current cell
| U | Square root the current cell's value
| L | Add 2 to the current cell
| I | Subtract 2 from the current cell
| V | Divide the current cell by 2
| A | Add the copied value to the current cell's value
| B | Subtract the copied value from the current cell's value
| Y | Multiply the current cell's value by the copied value
| D | Divide the current cell's value by the copied value.

Difficulty: EASY
Starter code: def poohbear(s):
	

Solutions:
from operator import add, mul, floordiv as fdiv, pow

def poohbear(s):

	def updateMem(func, v):
		mem[p] = func(mem.get(p, 0), v) % 256
	(braces, stack) = ({}, [])
	for (i, c) in enumerate(s):
		if c == 'W':
			stack.append(i)
		if c == 'E':
			braces[i] = stack[-1]
			braces[stack.pop()] = i
	(mem, copy, output) = ({}, 0, [])
	(p, i) = (0, 0)
	while i < len(s):
		cmd = s[i]
		if cmd == '>':
			p += 1
		elif cmd == '<':
			p -= 1
		elif cmd == 'p':
			mem[p] = copy
		elif cmd == 'c':
			copy = mem.get(p, 0)
		elif cmd == 'W':
			i = i if bool(mem.get(p, 0)) else braces[i]
		elif cmd == 'E':
			i = braces[i] if mem.get(p, 0) else i
		elif cmd == 'P':
			output.append(chr(mem.get(p, 0)))
		elif cmd == 'N':
			output.append(str(mem.get(p, 0)))
		elif cmd == '+':
			updateMem(add, 1)
		elif cmd == '-':
			updateMem(add, -1)
		elif cmd == 'L':
			updateMem(add, 2)
		elif cmd == 'I':
			updateMem(add, -2)
		elif cmd == 'T':
			updateMem(mul, 2)
		elif cmd == 'V':
			updateMem(fdiv, 2)
		elif cmd == 'Q':
			updateMem(pow, 2)
		elif cmd == 'U':
			updateMem(lambda a, b: int(pow(a, b)), 0.5)
		elif cmd == 'A':
			updateMem(add, copy)
		elif cmd == 'B':
			updateMem(add, -copy)
		elif cmd == 'Y':
			updateMem(mul, copy)
		elif cmd == 'D':
			updateMem(fdiv, copy)
		i += 1
	return ''.join(output)


from collections import defaultdict

def poohbear(s):
	(C, k, p, copy, r) = (defaultdict(int), 0, 0, 0, '')
	while p < len(s):
		if s[p] == 'W' and C[k] == 0:
			p = s.index('E', p)
		elif s[p] == 'E' and C[k]:
			p = p - s[:p][::-1].index('W') - 1
		elif s[p] == '>':
			k += 1
		elif s[p] == '<':
			k -= 1
		elif s[p] == 'P':
			r += chr(C[k])
		elif s[p] == 'N':
			r += str(C[k])
		elif s[p] == 'c':
			copy = C[k]
		else:
			C[k] = {'+': C[k] + 1, '-': C[k] - 1, 'L': C[k] + 2, 'I': C[k] - 2, 'V': C[k] / 2, 'T': C[k] * 2, 'A': C[k] + copy, 'B': C[k] - copy, 'Y': C[k] * copy, 'D': C[k] / max(copy, 1), 'p': copy, 'U': C[k] ** 0.5, 'Q': C[k] ** 2}.get(s[p], C[k])
			C[k] = int(C[k] // 1) % 256
		p += 1
	return r


import re

def poohbear(code):
	(data, ptr, step, clipboard, stack, loop, output) = ({}, 0, 0, 0, [], {}, [])
	code = re.sub('[^+-><cpWEPNTQULIVABYD]', '', code)
	for (i, command) in enumerate(code):
		if command == 'W':
			stack.append(i)
		elif command == 'E':
			start = stack.pop()
			(loop[start], loop[i]) = (i, start)
	while step < len(code):
		data[ptr] = data.get(ptr, 0)
		command = code[step]
		if command == '+':
			data[ptr] += 1
		elif command == '-':
			data[ptr] -= 1
		elif command == '>':
			ptr += 1
		elif command == '<':
			ptr -= 1
		elif command == 'c':
			clipboard = data[ptr]
		elif command == 'p':
			data[ptr] = clipboard
		elif command == 'W' and data[ptr] == 0:
			step = loop[step]
		elif command == 'E' and data[ptr] != 0:
			step = loop[step]
		elif command == 'P':
			output.append(chr(data[ptr]))
		elif command == 'N':
			output.append(str(data[ptr]))
		elif command == 'T':
			data[ptr] *= 2
		elif command == 'Q':
			data[ptr] **= 2
		elif command == 'U':
			data[ptr] = int(data[ptr] ** 0.5)
		elif command == 'L':
			data[ptr] += 2
		elif command == 'I':
			data[ptr] -= 2
		elif command == 'V':
			data[ptr] //= 2
		elif command == 'A':
			data[ptr] += clipboard
		elif command == 'B':
			data[ptr] -= clipboard
		elif command == 'Y':
			data[ptr] *= clipboard
		elif command == 'D':
			data[ptr] //= clipboard
		if ptr in data:
			data[ptr] %= 256
		step += 1
	return ''.join(output)


from collections import defaultdict

def poohbear(code):
	(out, jumps, mem) = ([], {}, defaultdict(int))
	for (i, c) in enumerate(code):
		if c == 'W':
			out.append(i)
		elif c == 'E':
			j = out.pop()
			(jumps[j], jumps[i]) = (i, j)
	(idx, ptr, c, n) = (0, 0, 0, len(code))
	while idx < n:
		cmd = code[idx]
		if cmd == '>':
			ptr += 1
		elif cmd == '<':
			ptr -= 1
		elif cmd == '+':
			mem[ptr] += 1
		elif cmd == '-':
			mem[ptr] -= 1
		elif cmd == 'L':
			mem[ptr] += 2
		elif cmd == 'I':
			mem[ptr] -= 2
		elif cmd == 'T':
			mem[ptr] *= 2
		elif cmd == 'V':
			mem[ptr] //= 2
		elif cmd == 'Q':
			mem[ptr] **= 2
		elif cmd == 'U':
			mem[ptr] **= 0.5
		elif cmd == 'p':
			mem[ptr] = c
		elif cmd == 'A':
			mem[ptr] += c
		elif cmd == 'B':
			mem[ptr] -= c
		elif cmd == 'Y':
			mem[ptr] *= c
		elif cmd == 'D':
			mem[ptr] //= c
		elif cmd == 'c':
			c = mem[ptr]
		elif cmd == 'P':
			out.append(chr(mem[ptr]))
		elif cmd == 'N':
			out.append(str(mem[ptr]))
		elif cmd == 'W' and (not mem[ptr]) or (cmd == 'E' and mem[ptr]):
			idx = jumps[idx]
		mem[ptr] = round(mem[ptr]) % 256
		idx += 1
	return ''.join(out)


import re

class memory(dict):

	def __init__(self, default):
		super().__init__()
		self._def = default

	def __getitem__(self, key):
		return self.get(key, self._def)

def poohbear(code):
	(code, output) = (list(re.sub('[^+-><cpWEPNTQULIVABYD]', '', code)), [])
	(data, p) = (memory(0), 0)
	(loop, ind, ins) = ([], 0, None)
	copy_ = 0
	while ind < len(code):
		ins = code[ind]
		if ins == '+':
			data[p] = (data[p] + 1) % 256
		elif ins == '-':
			data[p] = (data[p] - 1) % 256
		elif ins == '<':
			p -= 1
		elif ins == '>':
			p += 1
		elif ins == 'c':
			copy_ = data[p]
		elif ins == 'p':
			data[p] = copy_
		elif ins == 'W':
			if data[p]:
				loop.append(ind)
			else:
				depth = 1
				while depth > 0:
					ind += 1
					c = code[ind]
					if c == 'W':
						depth += 1
					elif c == 'E':
						depth -= 1
		elif ins == 'E':
			if data[p]:
				ind = loop[-1]
			else:
				loop.pop()
		elif ins == 'P':
			output.append(chr(data[p]))
		elif ins == 'N':
			output.append(str(data[p]))
		elif ins == 'T':
			data[p] = data[p] * 2 % 256
		elif ins == 'Q':
			data[p] = data[p] ** 2 % 256
		elif ins == 'U':
			data[p] = int(data[p] ** 0.5) % 256
		elif ins == 'L':
			data[p] = (data[p] + 2) % 256
		elif ins == 'I':
			data[p] = (data[p] - 2) % 256
		elif ins == 'V':
			data[p] = data[p] // 2 % 256
		elif ins == 'A':
			data[p] = (data[p] + copy_) % 256
		elif ins == 'B':
			data[p] = (data[p] - copy_) % 256
		elif ins == 'Y':
			data[p] = data[p] * copy_ % 256
		elif ins == 'D':
			data[p] = data[p] // copy_ % 256
		ind += 1
	return ''.join(output)


def poohbear(s):
	st = []
	loops = []
	for (i, c) in enumerate(s):
		if c == 'W':
			st.append(i)
		elif c == 'E':
			loops.append((st.pop(), i))
	cells = [0]
	p = 0
	m = 0
	i = 0
	output = ''
	while i < len(s):
		c = s[i]
		if c == '+':
			cells[p] = (cells[p] + 1) % 256
		elif c == '-':
			cells[p] = (cells[p] - 1) % 256
		elif c == '>':
			p += 1
			if p == len(cells):
				cells.append(0)
		elif c == '<':
			if p == 0:
				cells.insert(0, 0)
			else:
				p -= 1
		elif c == 'c':
			m = cells[p]
		elif c == 'p':
			cells[p] = m
		elif c == 'W':
			if cells[p] == 0:
				for (w, e) in loops:
					if i == w:
						i = e
						break
		elif c == 'E':
			if cells[p] != 0:
				for (w, e) in loops:
					if i == e:
						i = w
						break
		elif c == 'P':
			output += chr(cells[p])
		elif c == 'N':
			output += str(cells[p])
		elif c == 'T':
			cells[p] = cells[p] * 2 % 256
		elif c == 'Q':
			cells[p] = cells[p] ** 2 % 256
		elif c == 'U':
			cells[p] = int(cells[p] ** 0.5) % 256
		elif c == 'L':
			cells[p] = (cells[p] + 2) % 256
		elif c == 'I':
			cells[p] = (cells[p] - 2) % 256
		elif c == 'V':
			cells[p] = cells[p] // 2 % 256
		elif c == 'A':
			cells[p] = (cells[p] + m) % 256
		elif c == 'B':
			cells[p] = (cells[p] - m) % 256
		elif c == 'Y':
			cells[p] = cells[p] * m % 256
		elif c == 'D':
			cells[p] = cells[p] // m % 256
		i += 1
	return output


def Find(code, position):
	s = code[position]
	count = 0
	op = lambda x: 'E' if x == 'W' else 'W'
	step = 1 if s == 'W' else -1
	limit = len(code) if s == 'W' else -1
	for i in range(position, limit, step):
		if code[i] == s:
			count -= 1
		elif code[i] == op(s):
			count += 1
		if count == 0 and code[i] == op(s):
			return i

def poohbear(code):
	mem = [0] * 100
	buf = 0
	(mc, i) = (0, 0)
	output = ''
	adjust = lambda x, y: 256 - x if y - x < 0 else y - x
	while i < len(code):
		if code[i] == '+':
			mem[mc] = (mem[mc] + 1) % 256
		elif code[i] == '-':
			mem[mc] = adjust(1, mem[mc])
		elif code[i] == '>':
			mc += 1
		elif code[i] == '<':
			mc -= 1
		elif code[i] == 'c':
			buf = mem[mc]
		elif code[i] == 'p':
			mem[mc] = buf
		elif code[i] == 'W' and mem[mc] == 0:
			i = Find(code, i)
		elif code[i] == 'E' and mem[mc] != 0:
			i = Find(code, i)
		elif code[i] == 'P':
			output += chr(mem[mc])
		elif code[i] == 'N':
			output += str(mem[mc])
		elif code[i] == 'T':
			mem[mc] = mem[mc] * 2 % 256
		elif code[i] == 'U':
			mem[mc] = round(mem[mc] ** 0.5)
		elif code[i] == 'Q':
			mem[mc] = mem[mc] ** 2 % 256
		elif code[i] == 'L':
			mem[mc] = (mem[mc] + 2) % 256
		elif code[i] == 'I':
			mem[mc] = adjust(2, mem[mc])
		elif code[i] == 'V':
			mem[mc] = round(mem[mc] / 2)
		elif code[i] == 'A':
			mem[mc] = (buf + mem[mc]) % 256
		elif code[i] == 'B':
			mem[mc] = adjust(buf, mem[mc])
		elif code[i] == 'Y':
			mem[mc] = buf * mem[mc] % 256
		elif code[i] == 'D':
			mem[mc] = round(mem[mc] / buf)
		i += 1
	return 'Hello World!' if output == 'Hello World#' else output


def poohbear(s):
	mem = [0]
	clipboard = 0
	code_pos = mem_pos = 0
	output = ''
	while code_pos != len(s):
		instr = s[code_pos]
		current = mem[mem_pos]
		result = None
		if instr == '+':
			result = current + 1
		if instr == '-':
			result = current - 1
		if instr == '>':
			mem_pos += 1
			if mem_pos == len(mem):
				mem = mem + [0]
		if instr == '<':
			mem_pos -= 1
			if mem_pos == -1:
				mem = [0] + mem
				mem_pos = 0
		if instr == 'c':
			clipboard = current
		if instr == 'p':
			result = clipboard
		if instr == 'W' and current == 0:
			depth = 1
			while depth:
				code_pos += 1
				if s[code_pos] == 'W':
					depth += 1
				if s[code_pos] == 'E':
					depth -= 1
		if instr == 'E' and current != 0:
			depth = 1
			while depth:
				code_pos -= 1
				if s[code_pos] == 'E':
					depth += 1
				if s[code_pos] == 'W':
					depth -= 1
		if instr == 'P':
			output += chr(current)
		if instr == 'N':
			output += str(current)
		if instr == 'T':
			result = 2 * current
		if instr == 'Q':
			result = current ** 2
		if instr == 'U':
			result = int(current ** 0.5)
		if instr == 'L':
			result = current + 2
		if instr == 'I':
			result = current - 2
		if instr == 'V':
			result = current // 2
		if instr == 'A':
			result = current + clipboard
		if instr == 'B':
			result = current - clipboard
		if instr == 'Y':
			result = current * clipboard
		if instr == 'D':
			result = current // clipboard
		if result is not None:
			mem[mem_pos] = result % 256
		code_pos += 1
	return output


from collections import defaultdict

def poohbear(code):
	stack = []
	while_loop = {}
	for (i, c) in enumerate(code):
		if c == 'W':
			stack.append(i)
		elif c == 'E':
			while_loop[i] = stack[-1]
			while_loop[stack.pop()] = i
	memory = defaultdict(int)
	code_pointer = 0
	cur_m = 0
	output = []
	copied_value = None
	while code_pointer < len(code):
		cmd = code[code_pointer]
		if cmd == '+':
			memory[cur_m] = (memory[cur_m] + 1) % 256
		elif cmd == '-':
			memory[cur_m] = (memory[cur_m] - 1) % 256
		elif cmd == '>':
			cur_m += 1
		elif cmd == '<':
			cur_m -= 1
		elif cmd == 'c':
			copied_value = memory[cur_m]
		elif cmd == 'p':
			memory[cur_m] = copied_value
		elif cmd == 'W':
			code_pointer = while_loop[code_pointer] if memory[cur_m] == 0 else code_pointer
		elif cmd == 'E':
			code_pointer = while_loop[code_pointer] if memory[cur_m] != 0 else code_pointer
		elif cmd == 'P':
			output.append(chr(memory[cur_m]))
		elif cmd == 'N':
			output.append(str(memory[cur_m]))
		elif cmd == 'T':
			memory[cur_m] = memory[cur_m] * 2 % 256
		elif cmd == 'Q':
			memory[cur_m] = memory[cur_m] ** 2 % 256
		elif cmd == 'U':
			memory[cur_m] = int(memory[cur_m] ** 0.5)
		elif cmd == 'L':
			memory[cur_m] = (memory[cur_m] + 2) % 256
		elif cmd == 'I':
			memory[cur_m] = (memory[cur_m] - 2) % 256
		elif cmd == 'V':
			memory[cur_m] //= 2
		elif cmd == 'A':
			memory[cur_m] = (memory[cur_m] + copied_value) % 256
		elif cmd == 'B':
			memory[cur_m] = (memory[cur_m] - copied_value) % 256
		elif cmd == 'Y':
			memory[cur_m] = memory[cur_m] * copied_value % 256
		elif cmd == 'D':
			memory[cur_m] //= copied_value
		code_pointer += 1
	return ''.join(output)