# Introduction 

Hamsters are rodents belonging to the subfamily Cricetinae. The subfamily contains about 25 species, classified in six or seven genera. They have become established as popular small house pets, and, partly because they are easy to breed in captivity, hamsters are often used as laboratory animals.


# Task 

Write a function that accepts two inputs: `code` and `message` and returns an encrypted string from `message` using the `code`.   
The `code` is a string that generates the key in the way shown below:

```
 1  | h a m s t e r
 2  | i b n   u f
 3  | j c o   v g
 4  | k d p   w
 5  | l   q   x
 6  |         y
 7  |         z

```

All letters from `code` get number `1`. All letters which directly follow letters from `code` get number `2` (unless they already have a smaller number assigned), etc. It's difficult to describe but it should be easy to understand from the example below:

```
 1  | a e h m r s t
 2  | b f i n     u
 3  | c g j o     v
 4  | d   k p     w
 5  |     l q     x
 6  |             y
 7  |             z

```

How does the encoding work using the `hamster` code?    

```
a => a1
b => a2
c => a3
d => a4
e => e1
f => e2
...
```

And applying it to strings :

```
hamsterMe('hamster', 'hamster')   => h1a1m1s1t1e1r1
hamsterMe('hamster', 'helpme')    => h1e1h5m4m1e1

```

And you probably started wondering what will happen if there is no `a` in the `code`.  
Just add these letters after the last available letter (in alphabetic order) in the `code`.

The key for code `hmster` is:
```
 1  | e h m r s t
 2  | f i n     u
 3  | g j o     v
 4  |   k p     w
 5  |   l q     x
 6  |           y
 7  |           z
 8  |           a
 9  |           b
10  |           c
11  |           d
```

# Additional notes

The `code` will have at least 1 letter.   
Duplication of letters in `code` is possible and should be handled.    
The `code` and `message` consist of only lowercase letters.

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code: def hamster_me(code, message):
	

Solutions:
def hamster_me(code, message):
	(code, dct) = (sorted(set(code)), {})
	for (c1, c2) in zip(code, code[1:] + [chr(ord('z') + ord(code[0]) - ord('a'))]):
		for n in range(ord(c1), ord(c2) + 1):
			dct[chr((n - 97) % 26 + 97)] = c1 + str(n - ord(c1) + 1)
	return ''.join((dct[c] for c in message))


def hamster_me(code, message):
	cache = {k: k + '1' for k in code}
	for l in code:
		for i in range(2, 27):
			shifted = chr(97 + (ord(l) - 98 + i) % 26)
			if shifted in cache:
				break
			cache[shifted] = l + str(i)
	return ''.join(map(lambda x: cache[x], message))


from string import ascii_lowercase
id = {c: i for (i, c) in enumerate(ascii_lowercase)}
char = (ascii_lowercase + ascii_lowercase).__getitem__

def hamster_me(code, message):
	(res, L) = ({}, sorted(map(id.get, code)))
	for (i, j) in zip(L, L[1:] + [26 + L[0]]):
		start = char(i)
		for (k, x) in enumerate(map(char, range(i, j)), 1):
			res[x] = f'{start}{k}'
	return ''.join(map(res.get, message))


from string import ascii_lowercase as a_low

def hamster_me(code, message):
	table = {}
	code = sorted(code)
	shift = a_low.index(code[0])
	abc = a_low[shift:] + a_low[:shift]
	for i in range(len(code)):
		start = abc.index(code[i])
		finish = abc.index(code[(i + 1) % len(code)])
		if finish == 0:
			finish = len(abc)
		ind = 1
		for j in abc[start:finish]:
			table[j] = code[i] + str(ind)
			ind += 1
	cipher = str.maketrans(table)
	return message.translate(cipher)


def hamster_me(code, message):
	(D, code, lower) = ({}, set((c for c in code)), 'abcdefghijklmnopqrstuvwxyz')
	for c in sorted(code):
		for (i, e) in enumerate((lower * 2)[lower.index(c):], 1):
			if e in code - {c} or e in D:
				break
			D[e] = c + str(i)
	return ''.join((D[c] for c in message))


def hamster_me(code, message):
	import string
	alpha = string.ascii_lowercase
	alplist = []
	clist = ''
	c = 0
	for x in message:
		while True:
			if alpha[alpha.index(x) - c] in code:
				clist += alpha[alpha.index(x) - c] + str(c + 1)
				c = 0
				break
			else:
				c += 1
	return clist


def hamster_me(code, message):
	alpha = 'abcdefghijklmnopqrstuvwxyz'
	output = ''
	for char in message:
		try:
			codeLetter = max([c for c in sorted(code) if c <= char])
		except:
			codeLetter = max(code)
		codeIndex = alpha.index(codeLetter)
		charIndex = alpha.index(char)
		if codeIndex <= charIndex:
			appendNo = str(charIndex - codeIndex + 1)
		else:
			appendNo = str(charIndex + len(alpha) - codeIndex + 1)
		output += codeLetter + appendNo
	return output


from string import ascii_lowercase

def hamster_me(code, message):
	(table, code_letters) = ({}, set(code))
	(current_index, current_letter) = (0, '')
	for letter in ascii_lowercase * 2:
		if letter in code_letters:
			(current_index, current_letter) = (0, letter)
		current_index += 1
		table[letter] = current_letter + str(current_index)
	return ''.join(map(table.get, message))


hamster_me = lambda k, s: ''.join(map({c: w[0] + str(n) for w in __import__('re').findall('.[^%s]*' % k, ''.join(map(chr, range(97, 123))) * 2) for (n, c) in enumerate(w, 1)}.get, s))