Task:
Given an array arr of strings complete the function landPerimeter by calculating the total perimeter of all the islands. Each piece of land will be marked with 'X' while the water fields are represented as 'O'. Consider each tile being a perfect 1 x 1piece of land. Some examples for better visualization:

['XOOXO',
  'XOOXO',
  'OOOXO',
  'XXOXO',
  'OXOOO'] 

or 


should return:
"Total land perimeter: 24",

while


['XOOO',
  'XOXO',
  'XOXO',
  'OOXX',
  'OOOO']



should return: "Total land perimeter: 18"
Good luck!

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def land_perimeter(arr):
	

Solutions:
def land_perimeter(arr):
	(I, J) = (len(arr), len(arr[0]))
	P = 0
	for i in range(I):
		for j in range(J):
			if arr[i][j] == 'X':
				if i == 0 or arr[i - 1][j] == 'O':
					P += 1
				if i == I - 1 or arr[i + 1][j] == 'O':
					P += 1
				if j == 0 or arr[i][j - 1] == 'O':
					P += 1
				if j == J - 1 or arr[i][j + 1] == 'O':
					P += 1
	return 'Total land perimeter: ' + str(P)


land = lambda a: sum((t == ('X', 'X') for r in a for t in zip(r, r[1:]))) * 2

def land_perimeter(a):
	return 'Total land perimeter: ' + str(''.join(a).count('X') * 4 - land(a) - land(zip(*a)))


def land_perimeter(arr):
	result = 0
	for row in arr + list(zip(*arr)):
		current = list(row)
		result += sum((a != b for (a, b) in zip(['O'] + current, current + ['O'])))
	return 'Total land perimeter: {}'.format(result)


def land_perimeter(arr):
	perimetr = 0
	height = len(arr)
	for y in range(height):
		width = len(arr[y])
		for x in range(width):
			if arr[y][x] == 'X':
				if x == 0 or arr[y][x - 1] == 'O':
					perimetr += 1
				if x == width - 1 or arr[y][x + 1] == 'O':
					perimetr += 1
				if y == 0 or arr[y - 1][x] == 'O':
					perimetr += 1
				if y == height - 1 or arr[y + 1][x] == 'O':
					perimetr += 1
	return 'Total land perimeter: ' + str(perimetr)


def land_perimeter(arr):
	(a, b) = (' '.join(arr), ' '.join((''.join(x) for x in zip(*arr))))
	p = 4 * a.count('X') - 2 * 'O{}O{}O'.format(a, b).split('X').count('')
	return 'Total land perimeter: {}'.format(p)


def land_perimeter(arr):
	total = 0
	for x in range(len(arr)):
		for y in range(len(arr[0])):
			if arr[x][y] == 'X':
				total += 4
				if x != len(arr) - 1 and arr[x + 1][y] == 'X':
					total -= 1
				if x != 0 and arr[x - 1][y] == 'X':
					total -= 1
				if y != len(arr[0]) - 1 and arr[x][y + 1] == 'X':
					total -= 1
				if y != 0 and arr[x][y - 1] == 'X':
					total -= 1
	return 'Total land perimeter: %i' % total


def land_perimeter(grid):
	(s, m) = (len(grid), len(grid[0]))
	ans = 0
	for x in range(s):
		for y in range(m):
			if grid[x][y] == 'X':
				ans += 4
				if x < s - 1 and grid[x + 1][y] == 'X':
					ans -= 2
				if y < m - 1 and grid[x][y + 1] == 'X':
					ans -= 2
	return 'Total land perimeter: {}'.format(ans)


def land_perimeter(arr):
	n = len(arr)
	m = len(arr[0])
	dr = [1, 0, -1, 0]
	dc = [0, 1, 0, -1]
	ans = 0
	for i in range(len(arr)):
		for j in range(len(arr[i])):
			if arr[i][j] == 'O':
				continue
			cur = 0
			for k in range(4):
				nx = i + dr[k]
				ny = j + dc[k]
				inside = nx >= 0 and ny >= 0 and (nx < n) and (ny < m)
				ans += (inside and arr[nx][ny] == 'O') + (not inside)
	ampogiko = 'Total land perimeter: ' + str(ans)
	return ampogiko


def land_perimeter(arr):
	per = 0
	for _ in range(4):
		arr.insert(0, ['0'] * (len(arr[0]) + (_ + 1)))
		arr = list(zip(*arr[::-1]))
	for i in range(1, len(arr)):
		for y in range(1, len(arr[i])):
			if arr[i][y] == 'X':
				per += 4 - sum([arr[i - 1][y] == 'X', arr[i + 1][y] == 'X', arr[i][y + 1] == 'X', arr[i][y - 1] == 'X'])
	return f'Total land perimeter: {per}'