Because my other two parts of the serie were pretty well received I decided to do another part.

Puzzle Tiles

You will get two Integer n (width) and m (height) and your task is to draw following pattern. Each line is seperated with '\n'.


Both integers are equal or greater than 1. No need to check for invalid parameters.
There are no whitespaces at the end of each line.

e.g.:


 

                                       \_( )\_\_ \_( )\_\_ \_( )\_\_ \_( )\_\_
                                     \_|     \_|     \_|     \_|     \_|
                                    (\_   \_ (\_   \_ (\_   \_ (\_   \_ (\_
                                     |\_\_( )\_|\_\_( )\_|\_\_( )\_|\_\_( )\_|
                                     |\_     |\_     |\_     |\_     |\_
             puzzleTiles(4,3)=>       \_) \_   \_) \_   \_) \_   \_) \_   \_)
                                     |\_\_( )\_|\_\_( )\_|\_\_( )\_|\_\_( )\_|
                                     \_|     \_|     \_|     \_|     \_|
                                    (\_   \_ (\_   \_ (\_   \_ (\_   \_ (\_
                                     |\_\_( )\_|\_\_( )\_|\_\_( )\_|\_\_( )\_|
                                     


For more informations take a look in the test cases!


 Serie: ASCII Fun

ASCII Fun #1: X-Shape
ASCII Fun #2: Funny Dots
ASCII Fun #3: Puzzle Tiles
ASCII Fun #4: Build a pyramid

Difficulty: EASY
Tags: String algorithms

Starter code:
def puzzle_tiles(width, height):
	

Solutions:
def puzzle_tiles(width, height):

	def f():
		yield ('  ' + ' _( )__' * width)
		for i in range(height):
			if i % 2 == 0:
				yield (' _|' + '     _|' * width)
				yield ('(_' + '   _ (_' * width)
				yield (' |' + '__( )_|' * width)
			else:
				yield (' |_' + '     |_' * width)
				yield ('  _)' + ' _   _)' * width)
				yield (' |' + '__( )_|' * width)
	return '\n'.join(f())


def puzzle_tiles(width, height):

	def line(i):
		if i == 0:
			return '  ' + ' _( )__' * width
		if i % 3 == 0:
			return ' ' + '__( )_'.join(('|' for _ in range(width + 1)))
		if i % 6 == 1:
			return ' ' + '     '.join(('_|' for _ in range(width + 1)))
		if i % 6 == 2:
			return '   _ '.join(('(_' for _ in range(width + 1)))
		if i % 6 == 4:
			return ' ' + '     '.join(('|_' for _ in range(width + 1)))
		if i % 6 == 5:
			return ' _ '.join(('  _)' for _ in range(width + 1)))
	return '\n'.join((line(i) for i in range(3 * height + 1)))


def puzzle_tiles(w, h):
	puzzle = ['  ' + ''.join([' _( )__'] * w)]
	for i in range(h):
		if i % 2:
			puzzle.append(''.join([' |_    '] * (w + 1)))
			puzzle.append(''.join(['  _) _ '] * (w + 1))[:-2])
		else:
			puzzle.append(''.join([' _|    '] * (w + 1)))
			puzzle.append(''.join(['(_   _ '] * (w + 1))[:-2])
		puzzle.append(' ' + ''.join(['|__( )_'] * (w + 1))[:-6])
	return '\n'.join((l.rstrip() for l in puzzle))


LINES = ['__( )_', '     _|', '   _ {0}_']

def puzzle_tiles(width, height):
	s = [''] * (height * 3 + 1)
	for i in range(len(s)):
		line = LINES[i % 3]
		if not i:
			s[i] = ' '.join(['  '] + [line[1:] + '_'] * width)
		elif not i % 3:
			s[i] = '|'.join([' '] + [line] * width + [''])
		elif i % 3 == 1:
			s[i] = ' ' + ('_|' + line * width)[::(-1) ** (not i % 2)]
		elif i % 3 == 2:
			s[i] = '  ' * (i % 2) + ('{0}_' + line * width).format('()'[i % 2])[::(-1) ** (i % 2)]
	return '\n'.join(s)


def puzzle_tiles(width, height):
	temp1 = (' ' * 3 + '_( )__ ' * width).rstrip()
	temp2 = ' _' + '     _'.join(['|'] * (width + 1))
	temp3 = '   _ '.join(['(_'] * (width + 1))
	temp4 = ' ' + '__( )_'.join(['|'] * (width + 1))
	temp5 = ' ' + temp2[::-1].rstrip()
	temp6 = '  _' + ' _   _'.join([')'] * (width + 1))
	res = [temp1]
	for i in range(height):
		if i % 2 == 0:
			res.append(temp2)
			res.append(temp3)
			res.append(temp4)
		else:
			res.append(temp5)
			res.append(temp6)
			res.append(temp4)
	return '\n'.join(res)


def puzzle_tiles(width, height):
	r = []
	r.append('  ' + ' _( )__' * width)
	for i in range(height):
		if i % 2 == 0:
			r.append(' _' + '|     _' * width + '|')
			r.append('(_   _ ' * width + '(_')
		else:
			r.append(' ' + '|_     ' * width + '|_')
			r.append('  _)' + ' _   _)' * width)
		r.append(' ' + '|__( )_' * width + '|')
	return '\n'.join(r)


from itertools import cycle
one = '   _( )__\n _|     _|\n(_   _ (_\n |__( )_|'.splitlines()
rev = ' |_     |_\n  _) _   _)\n |__( )_|'.splitlines()

def puzzle_tiles(n, m):
	(build, it) = (one.copy(), [2, 3, 2, 2])
	for i in range(n - 1):
		for j in range(len(build)):
			build[j] += one[j][it[j]:]
	(build_rev, it) = (rev.copy(), [3, 4, 2])
	for i in range(n - 1):
		for j in range(len(build_rev)):
			build_rev[j] += rev[j][it[j]:]
	nex = cycle([build[1:], build_rev])
	return '\n'.join([build[0]] + sum([next(nex) for i in range(m)], []))


from itertools import cycle

def puzzle_tiles(width, height):
	up_puzzle = ''.join(['  ', ' _( )__' * width, '\n'])
	puzzle = cycle([['', ' _|    ' * width, ' _|'], ['', '(_   _ ' * width, '(_'], [' ', '|__( )_' * width, '|'], [' ', '|_     ' * width, '|_'], [' ', ' _) _  ' * width, ' _)'], [' ', '|__( )_' * width, '|']])
	return up_puzzle + '\n'.join(('{}{}{}'.format(*next(puzzle)) for i in range(height * 3)))


def puzzle_tiles(width, height):
	top = '   ' + ' '.join(['_( )__'] * width)
	bottom = ' |' + '|'.join(['__( )_'] * width) + '|'
	rows = [top]
	for i in range(height):
		if i % 2:
			line1 = ' |_' + '|_'.join(['     '] * width) + '|_'
			line2 = '  _)' + '_)'.join([' _   '] * width) + '_)'
		else:
			line1 = ' _|' + '_|'.join(['     '] * width) + '_|'
			line2 = '(_' + '(_'.join(['   _ '] * width) + '(_'
		rows.append('\n'.join([line1, line2, bottom]))
	return '\n'.join(rows)