# Task
 In a black and white image we can use `1` instead of black pixels and `0` instead of white pixels. 
 
 For compression image file we can reserve pixels by consecutive pixels who have the same color. 
 
 Your task is to determine how much of black and white pixels is in each row sorted by place of those.
 
# Example:

 For `height=2,width=100 and compressed=[40,120,40,0]`

 The result should be `[[40,60],[0,60,40,0]]`
 
 Sum of compressed array always divisible by width and height of image.
 
 Pixels available is in Compressed array in this order:
 
 `[Black,White,Black,White,Black,White,...]`
 
 ![](http://s3.picofile.com/file/8194350650/Compressed_Image_1_.png)

 For `height=2,width=100 and compressed=[0, 180, 20, 0]`

 The result should be `[[0,100],[0,80,20,0]]`

 ![](http://s6.picofile.com/file/8194348868/Compressed_Image.png)


# Input/Output


- `[input]` integer `height`

  Height of image


- `[input]` integer `width`

  Width of image


- `[input]` integer array `compressed`

  Consecutive pixels


- `[output]` 2D integer array

 The black and white pixels in each row.

Source: codewars | Difficulty: EASY
Tags: Ad-hoc

Starter code: def black_and_white(height, width, compressed):
	

Solutions:
def black_and_white(height, width, compressed):
	(res, left, i, color) = ([], 0, 0, 0)
	for h in range(height):
		(tot, tmp) = (0, [])
		if color == 1:
			tmp.append(0)
		while tot < width and i < len(compressed):
			if left:
				if left <= width:
					tmp.append(left)
					(tot, left, color) = (left, 0, 1 - color)
				else:
					tmp.append(width)
					(tot, left) = (width, left - width)
			else:
				(i, val) = (i + 1, compressed[i])
				if tot + val <= width:
					tmp.append(val)
					(tot, color) = (tot + val, 1 - color)
				else:
					tmp.append(width - tot)
					(tot, left) = (width, tot + val - width)
		if len(tmp) % 2:
			tmp.append(left if h == height - 1 else 0)
		res.append(tmp)
	return res


def black_and_white(h, w, c):
	li = [[]]
	i = j = current = 0
	while i < len(c):
		current += c[i]
		if current > w:
			temp = current - w
			if c[i] - temp != 0:
				li[j].append([['b', 'w'][i & 1], c[i] - temp])
			c[i] = temp
			j += 1
			li.append([])
			current = 0
			continue
		li[j].append([['b', 'w'][i & 1], c[i]])
		i += 1
	for i in range(len(li)):
		if li[i][0][0] != 'b':
			li[i].insert(0, ['b', 0])
		if li[i][-1][0] != 'w':
			li[i].append(['w', 0])
	return [[j[1] for j in i] for i in li]


def black_and_white(h, w, compressed):
	s = ''.join(('BW'[i % 2] * b for (i, b) in enumerate(compressed)))
	r = [s[i:i + w].replace('WB', 'W B').replace('BW', 'B W') for i in range(0, h * w, w)]
	r = [([0] if row[0] == 'W' else []) + [len(w) for w in row.split()] for row in r]
	return [e + ([0] if len(e) % 2 else []) for e in r]


def black_and_white(height, width, compressed):
	r = []
	row = []
	for (i, x) in enumerate(compressed):
		color = i % 2
		if not row and color:
			row.append(0)
		remain = width - sum(row)
		if x > remain:
			row.append(remain)
			if len(row) % 2 == 1:
				if row[-1] == 0:
					row.pop()
				else:
					row.append(0)
			r.append(row)
			x = x - remain
			while x >= width:
				if color:
					r.append([0, width])
				else:
					r.append([width, 0])
				x -= width
			if color:
				row = [0, x]
			else:
				row = [x]
		else:
			row.append(x)
	if row:
		if len(row) % 2 == 1:
			if row[-1] == 0:
				row.pop()
			else:
				row.append(0)
		r.append(row)
	if r[-1] == [0, 0]:
		r.pop()
	return r


def black_and_white(height, width, compressed):
	result = [list() for _ in range(height)]
	line_idx = 0
	for (i, c) in enumerate(compressed):
		black = i % 2 == 0
		while c:
			if sum(result[line_idx]) == width:
				if len(result[line_idx]) % 2 != 0:
					result[line_idx].append(0)
				line_idx += 1
			if len(result[line_idx]) == 0 and (not black):
				result[line_idx].append(0)
			m = min(c, width - sum(result[line_idx]))
			result[line_idx].append(m)
			c -= m
	if len(result[line_idx]) % 2 != 0:
		result[line_idx].append(0)
	return result


from itertools import takewhile
from typing import List

def black_and_white(height: int, width: int, compressed: List[int]) -> List[List[int]]:

	def tracker():
		is_black = False
		line = 0
		for i in compressed:
			is_black = not is_black
			if line + i < width:
				yield i
				line += i
			else:
				yield (width - line)
				if is_black:
					yield 0
				yield (-1)
				i -= width - line
				line = 0
				while i > width:
					if is_black:
						yield width
						yield 0
					else:
						yield 0
						yield width
					yield (-1)
					i -= width
				if i == 0 and is_black:
					yield 0
				if i > 0:
					if is_black:
						yield i
					else:
						yield 0
						yield i
					line += i
	it = iter(tracker())
	return [[i for i in takewhile(lambda i: i >= 0, it)] for h in range(height)]


from re import findall

def black_and_white(height, width, compressed):
	compr = ''.join((('0' if i % 2 else '1') * v for (i, v) in enumerate(compressed)))
	res = []
	for i in range(0, len(compr), width):
		(row, temp, black) = (compr[i:i + width], [], True)
		m = [(c, len(g)) for (g, c) in findall('((.)\\2*)', row)]
		for (dig, n) in m:
			if black and dig == '0':
				temp.extend([0, n])
				black == False
			elif not black and dig == '1':
				temp.extend([n, 0])
				black = True
			else:
				temp.append(n)
				black = not black
		if len(temp) % 2:
			temp.append(0)
		res.append(temp)
	return res


def black_and_white(height, width, compressed):
	img = []
	line = []
	for i in range(len(compressed)):
		while compressed[i] >= 0:
			if compressed[i] > line_empty_size(line, width):
				compressed[i] -= line_empty_size(line, width)
				line.append(line_empty_size(line, width))
			else:
				line.append(compressed[i])
				compressed[i] = -1
			if line_empty_size(line, width) == 0:
				if len(line) % 2 == 0:
					img.append(line)
					line = []
					if compressed[i] > 0:
						line.append(0)
				else:
					line.append(0)
					img.append(line)
					line = []
					if compressed[i] <= 0:
						line.append(0)
	if len(img[height - 1]) % 2 != 0:
		img[height - 1].append(0)
	return img

def line_empty_size(line, width):
	size = width
	for i in range(len(line)):
		size -= line[i]
	return size


import _thread
import time

def black_and_white(height, width, compressed):
	actual = width
	resu = []
	lev = []
	isblack = True
	for x in compressed:
		while x >= actual:
			x -= actual
			lev.append(actual)
			if not len(lev) % 2 == 0:
				lev.append(0)
			actual = width
			resu.append(lev)
			lev = []
			if not isblack:
				lev.append(0)
		if x < actual:
			actual -= x
			lev.append(x)
			isblack = not isblack
	return resu


def black_and_white(height, width, compressed):
	answer = []
	(row, wFilled) = ([], 0)
	for (i, pixels) in enumerate(compressed):
		wSum = wFilled + pixels
		if wSum < width:
			row.append(pixels)
			wFilled = wSum
		else:
			colour = i % 2
			fitting = width - wFilled
			row.append(fitting)
			pixels -= fitting
			if colour == 0:
				row.append(0)
			answer.append(row)
			while True:
				if pixels >= width:
					answer.append([width, 0] if colour == 0 else [0, width])
					pixels -= width
				else:
					wFilled = pixels
					if colour == 0:
						row = [pixels]
					elif pixels > 0:
						row = [0, pixels]
					else:
						row = []
					break
	return answer