---

# Story

The Pied Piper has been enlisted to play his magical tune and coax all the rats out of town.

But some of the rats are deaf and are going the wrong way!

# Kata Task

How many deaf rats are there?

# Legend

* ```P``` = The Pied Piper
* ```O~``` = Rat going left
* ```~O``` = Rat going right

# Example

* ex1 ```~O~O~O~O P``` has 0 deaf rats


* ex2 ```P O~ O~ ~O O~``` has 1 deaf rat


* ex3 ```~O~O~O~OP~O~OO~``` has 2 deaf rats

---

# Series

* [The deaf rats of Hamelin (2D)](https://www.codewars.com/kata/the-deaf-rats-of-hamelin-2d)

Difficulty: EASY
Tags: String algorithms

Fundamentals

Data structures
Skill types: Data structures

Starter code:
def count_deaf_rats(town):
	

Solutions:
def count_deaf_rats(town):
	return town.replace(' ', '')[::2].count('O')


import re

def count_deaf_rats(town):
	return re.findall('O~|~O', town.split('P')[0]).count('O~') + re.findall('O~|~O', town.split('P')[1]).count('~O')


import re

def count_deaf_rats(town):
	town = re.sub('(~O|O~)', lambda m: '>' if m.group(1) == '~O' else '<', town)
	(left, right) = town.split('P')
	return left.count('<') + right.count('>')


from itertools import count

def count_deaf_rats(town):
	deaf = 0
	piper = False
	rat = ''
	for c in town:
		if c == 'P':
			piper = True
		if c in 'O~':
			rat += c
			if len(rat) == 2:
				deaf += piper != (rat == 'O~')
				rat = ''
	return deaf


def count_deaf_rats(town):
	total = 0
	town = town.replace(' ', '')
	print(town)
	town = town.split('P')
	print(town)
	if '' in town:
		for i in range(len(town)):
			if town[i] != '' and i == 0:
				town[i] = list(town[i])
				for j in range(0, len(town[i]), 2):
					print(''.join(town[i][j:j + 2]))
					if ''.join(town[i][j:j + 2]) == 'O~':
						total += 1
			elif town[i] != '' and i == 1:
				town[i] = list(town[i])
				for j in range(0, len(town[i]), 2):
					if ''.join(town[i][j:j + 2]) == '~O':
						total += 1
		return total
	else:
		for i in range(len(town)):
			if i == 0:
				town[i] = list(town[i])
				for j in range(0, len(town[i]), 2):
					print(town[i][j:j + 2])
					if ''.join(town[i][j:j + 2]) == 'O~':
						print(j)
						total += 1
			else:
				town[i] = list(town[i])
				for j in range(0, len(town[i]), 2):
					if ''.join(town[i][j:j + 2]) == '~O':
						print(j)
						total += 1
		return total


def count_deaf_rats(town):
	town = town.replace(' ', '')
	(left, p, right) = town.partition('P')
	left = replace_rats(left)
	right = replace_rats(right)
	return left.count('L') + right.count('R')

def replace_rats(rats):
	ret_array = []
	for x in range(0, len(rats), 2):
		if rats[x] == '~':
			ret_array.append('R')
		else:
			ret_array.append('L')
	return ret_array


def count_deaf_rats(s):
	return s.replace(' ', '')[::2].count('O')


def count_deaf_rats(town):
	piper = False
	count = 0
	expectedChar = '~'
	deafRats = 0
	while count < len(town):
		if town[count] == ' ':
			count += 1
		elif town[count] == expectedChar:
			count += 2
		elif town[count] == 'P':
			piper = True
			expectedChar = 'O'
			count += 1
		else:
			deafRats += 1
			count += 2
	return deafRats


def count_deaf_rats(town):
	twn = town.replace(' ', '').split('P')
	return sum((find_d(e, [['O', '~'], ['~', 'O']][i]) for (i, e) in enumerate(twn)))

def find_d(r, w):
	r = [e for e in r]
	return sum([1 for i in range(0, len(r), 2) if r[i:i + 2] == w])