In the world of birding there are four-letter codes for the common names of birds. These codes are created by some simple rules:

* If the bird's name has only one word, the code takes the first four letters of that word. 
* If the name is made up of two words, the code takes the first two letters of each word.
* If the name is made up of three words, the code is created by taking the first letter from the first two words and the first two letters from the third word.
* If the name is four words long, the code uses the first letter from all the words.

*(There are other ways that codes are created, but this Kata will only use the four rules listed above)*

Complete the function that takes an array of strings of common bird names from North America, and create the codes for those names based on the rules above. The function should return an array of the codes in the same order in which the input names were presented.

Additional considertations:

* The four-letter codes  in the returned array should be in UPPER CASE.
* If a common name has a hyphen/dash, it should be considered a space. 

## Example

If the input array is: `["Black-Capped Chickadee", "Common Tern"]`

The return array would be: `["BCCH", "COTE"]`

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Data structures

Starter code: def bird_code(arr):
	

Solutions:
import re
SPLITTER = re.compile('[\\s-]')

def birdify(lst):
	return ''.join((x[:4 // len(lst)] for x in lst)) + ('' if len(lst) != 3 else lst[-1][1])

def bird_code(arr):
	return [birdify(SPLITTER.split(name)).upper() for name in arr]


def bird_code(arr):
	codes = []
	for name in arr:
		name = name.replace('-', ' ').split()
		if len(name) == 1:
			code = name[0][:4]
		elif len(name) == 2:
			code = name[0][:2] + name[1][:2]
		elif len(name) == 3:
			code = name[0][0] + name[1][0] + name[2][:2]
		elif len(name) == 4:
			code = ''.join((n[0] for n in name))
		else:
			return 'More than 4 words!'
		codes.append(code.upper())
	return codes


def encode(bird):
	words = bird.replace('-', ' ').split()
	patterns = {1: (4,), 2: (2, 2), 3: (1, 1, 2), 4: (1, 1, 1, 1)}
	return ''.join(map(lambda w, l: w[:l], words, patterns[len(words)])).upper()

def bird_code(birds):
	return list(map(encode, birds))


RULES = {1: lambda x: ''.join(x[0][:4]).upper(), 2: lambda x: ''.join(x[0][:2] + x[1][:2]).upper(), 3: lambda x: ''.join(x[0][:1] + x[1][:1] + x[2][:2]).upper(), 4: lambda x: ''.join(x[0][:1] + x[1][:1] + x[2][:1] + x[3][:1]).upper()}

def bird_code(arr):
	arr = [bird.replace('-', ' ') for bird in arr]
	ret_arr = []
	for bird in arr:
		name_parts = bird.split()
		ret_arr.append(RULES[len(name_parts)](name_parts))
	return ret_arr


def bird_code(birds):
	codes = []
	for bird in birds:
		words = bird.upper().replace('-', ' ').split()
		if len(words) == 1:
			codes.append(words[0][:4])
		elif len(words) == 2:
			codes.append(''.join((word[:2] for word in words)))
		else:
			code = ''.join((word[0] for word in words))
			codes.append(f"{code}{(words[2][1] if len(words) == 3 else '')}")
	return codes


REGEX = __import__('re').compile('[ -]').split
letters = ((4,), (2, 2), (1, 1, 2), (1, 1, 1, 1))

def code(L):
	return ''.join((L[i][:j] for (i, j) in enumerate(letters[len(L) - 1]))).upper()

def bird_code(arr):
	return [code(REGEX(name)) for name in arr]


import re

def bird_code(s):
	li = []
	for i in s:
		i = re.sub(' *-', ' ', i).upper().split()
		if len(i) == 1:
			li.append(i[0][:4])
		if len(i) == 2:
			li.append(i[0][:2] + i[1][:2])
		if len(i) == 3:
			li.append(i[0][0] + i[1][0] + i[2][:2])
		if len(i) == 4:
			li.append(''.join([k[0] for k in i]))
	return li


def bird_code(a):
	return [bird(b.upper()) for b in a]

def bird(s):
	b = s.replace('-', ' ').split()
	if len(b) == 1:
		return s[:4]
	if len(b) == 2:
		return b[0][:2] + b[1][:2]
	if len(b) == 3:
		return b[0][0] + b[1][0] + b[2][:2]
	if len(b) == 4:
		return b[0][0] + b[1][0] + b[2][0] + b[3][0]


import re

def f(name):
	words = re.findall("[\\w']+", name)
	if len(words) == 1:
		return words[0][:4]
	elif len(words) == 2:
		return words[0][:2] + words[1][:2]
	elif len(words) == 3:
		return words[0][:1] + words[1][:1] + words[2][:2]
	return ''.join((word[0] for word in words))

def bird_code(arr):
	return [f(name).upper() for name in arr]


def bird_code(arr):
	arr1 = [s.replace('-', ' ') for s in arr]
	res = []
	for s in arr1:
		arr2 = s.split(' ')
		if len(arr2) == 1:
			res.append(arr2[0][:4].upper())
		elif len(arr2) == 2:
			res.append(arr2[0][:2].upper() + arr2[1][:2].upper())
		elif len(arr2) == 3:
			res.append(arr2[0][0].upper() + arr2[1][0].upper() + arr2[2][:2].upper())
		else:
			res.append(arr2[0][0].upper() + arr2[1][0].upper() + arr2[2][0].upper() + arr2[3][0].upper())
	return res