*Translations appreciated*

## Background information

The Hamming Code is used to correct errors, so-called bit flips, in data transmissions. Later in the description follows a detailed explanation of how it works.
In this Kata we will implement the Hamming Code with bit length 3, this has some advantages and disadvantages:
- ✓ Compared to other versions of hamming code, we can correct more mistakes
- ✓ It's simple to implement
- x  The size of the input triples



## Task 1: Encode function:

First of all we have to implement the encode function, which is pretty easy, just follow the steps below.

Steps:
1. convert every letter of our text to ASCII value
2. convert ASCII value to 8-bit binary string
3. replace every "0" with "000" and every "1" with "111"

Let's do an example:

We have to convert the string ```hey``` to hamming code sequence.

1. First convert it to ASCII values:

```104``` for ```h```, ```101``` for ```e``` and ```121``` for ```y```.


2. Now we convert the ASCII values to a 8-bit binary string:

```104``` -> ```01101000```, ```101``` -> ```01100101``` and ```121``` -> ```01111001```

if we concat the binarys we get ```011010000110010101111001```


3. Now we replace every "0" with "000" and every "1" with "111":

```011010000110010101111001``` -> ```000111111000111000000000000111111000000111000111000111111111111000000111```

That's it good job!


## Task 2: Decode function:

Now we have to check if there happened any mistakes and correct them.
Errors will only be a bit flip and not a loose of bits, so the length of the input string is always divisible by 3.

example: 
- 111 --> 101  this can and will happen
- 111 --> 11   this won't happen

The length of the input string is also always divsible by 24 so that you can convert it to an ASCII value.

Steps:
1. Split the string of 0 and 1 in groups of three characters example: "000", "111"
2. Check if an error occured:
 If no error occured the group is "000" or "111", then replace "000" with "0" and "111" with 1
 If an error occured the group is for example "001" or "100" or "101" and so on...
 Replace this group with the character that occurs most often. example: "010" -> "0" , "110" -> "1" 

3. Now take a group of 8 characters and convert that binary number to decimal ASCII value
4. Convert the ASCII value to a char and well done you made it :)



Look at this example carefully to understand it better:

We got a bit sequence:

```100111111000111001000010000111111000000111001111000111110110111000010111``` 

First we split the bit sequence into groups of three:

```100```, ```111```, ```111```, ```000```, ```111```, ```001``` ....

Every group with the most "0" becomes "0" and every group with the most "1" becomes "1":

```100``` -> ```0``` Because there are two ```0``` and only one ```1```

```111``` -> ```1``` Because there are zero ```0``` and three ```1```

```111``` -> ```1``` Because there are zero ```0``` and three ```1```

```000``` -> ```0``` Because there are three ```0``` and zero ```1```

```111``` -> ```1``` Because there are zero ```0``` and three ```1```

```001``` -> ```0``` Because there are two ```0``` and one ```1```

Now concat all 0 and 1 to get ```011010000110010101111001```

We split this string into groups of eight:
```01101000```, ```01100101``` and ```01111001```.

And now convert it back to letters:

```01101000``` is binary representation of 104, which is ASCII value of ```h```

```01100101``` is binary representation of 101, which is ASCII value of ```e```

```01111001``` is binary representation of 121, which is ASCII value of ```y```

Now we got our word ```hey``` !

Difficulty: EASY
Tags: Bit manipulation
Skill types: Bit manipulation

Starter code:
def encode(string):
	

Solutions:
def encode(stg):
	return ''.join((digit * 3 for char in stg for digit in f'{ord(char):08b}'))

def decode(binary):
	reduced = (get_digit(triplet) for triplet in chunks(binary, 3))
	return ''.join((get_char(byte) for byte in chunks(''.join(reduced), 8)))

def chunks(seq, size):
	return (seq[i:i + size] for i in range(0, len(seq), size))

def get_digit(triplet):
	return max(triplet, key=triplet.count)

def get_char(byte):
	return chr(int(byte, 2))


def encode(uncoded):
	encoded = ''.join((''.join((b * 3 for b in bin(ord(c))[2:].zfill(8))) for c in uncoded))
	return encoded

def decode(encoded):
	patches = {'000': '0', '001': '0', '010': '0', '100': '0', '011': '1', '101': '1', '110': '1', '111': '1'}
	patched = ''.join((patches[encoded[i:i + 3]] for i in range(0, len(encoded), 3)))
	decoded = ''.join((chr(int(patched[i:i + 8], 2)) for i in range(0, len(patched), 8)))
	return decoded


def encode(string):
	return ''.join(map('{:08b}'.format, string.encode())).replace('0', '000').replace('1', '111')

def decode(bits):
	bytes_ = ('01'['11' in a + b + c + a] for (a, b, c) in zip(*[iter(bits)] * 3))
	return bytes((int(''.join(b), 2) for b in zip(*[iter(bytes_)] * 8))).decode()


def encode(string):
	values = ''.join((format(x, '08b') for x in map(ord, string)))
	return values.replace('0', '000').replace('1', '111')

def decode(bits):
	bs = ''.join(('0' if xs.count('0') >= 2 else '1' for xs in zip(*[iter(bits)] * 3)))
	return ''.join((chr(int(''.join(b), 2)) for b in zip(*[iter(bs)] * 8)))


def encode(string):
	bits = ''
	for i in string:
		bits += '{0:08b}'.format(ord(i))
	return bits.replace('1', '111').replace('0', '000')

def decode(bits):
	x = 3
	res = [bits[y - x:y] for y in range(x, len(bits) + x, x)]
	decoded = ''
	for i in res:
		sorti = sorted(i)
		decoded += sorti[1]
	x = 8
	bin = [decoded[y - x:y] for y in range(x, len(decoded) + x, x)]
	return ''.join([chr(int(x, 2)) for x in bin])


def encode(string):
	return ''.join([i * 3 for i in ''.join([bin(i)[2:].zfill(8) for i in [ord(i) for i in string]])])

def decode(bits):
	three = [i for i in bits]
	for i in range(3, len(bits) // 3 * 4, 4):
		three.insert(i, ' ')
	three = ''.join(three).split()
	binar = ['0' if i.count('0') > i.count('1') else '1' for i in three]
	for i in range(8, len(binar) // 8 * 9, 9):
		binar.insert(i, ' ')
	binar = ''.join(binar).split()
	asci = [int(i, 2) for i in binar]
	string = ''.join([chr(i) for i in asci])
	return string


def encode(string):
	ASCII = [ord(c) for c in string]
	BIN = [format(i, '08b') for i in ASCII]
	bits = ''.join([BIN[i].replace('1', '111').replace('0', '000') for i in range(0, len(BIN))])
	return bits

def decode(bits):
	a = [list(bits[i:i + 3]) for i in range(0, len(bits), 3)]
	b = [int(a[i][j]) for i in range(0, len(a)) for j in range(0, 3)]
	c = [list(b[i:i + 3]) for i in range(0, len(bits), 3)]
	d = []
	for i in range(0, len(bits) // 3):
		if sum(c[i]) >= 2:
			d.append('1')
		else:
			d.append('0')
	e = ''.join(d)
	n = int(e, 2)
	return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()


def encode(string):
	bits = ''.join([format(i, '#010b').replace('0b', '') for i in [ord(i) for i in string]])
	return ''.join([i * 3 for i in bits])

def decode(bits):
	parts = [bits[i:i + 3] for i in range(0, len(bits), 3)]
	bits = ''.join(['1' if i.count('1') > i.count('0') else '0' for i in parts])
	return ''.join([chr(int(bits[i:i + 8], 2)) for i in range(0, len(bits), 8)])


def packets(it, n):
	for i in range(0, len(it), n):
		yield it[i:i + n]

def encode(string):
	return ''.join((x * 3 for c in string for x in f'{ord(c):08b}'))

def decode(bits):
	tmp = ''.join((s[0] if s[0] in s[1:] else s[1] for s in packets(bits, 3)))
	return ''.join((chr(int(x, 2)) for x in packets(tmp, 8)))


def encode(string):
	ascii_bits = []
	for char in string:
		ascii_bits.append(f'{ord(char):08b}'.replace('0', '000').replace('1', '111'))
	return ''.join(ascii_bits)

def decode(bits):
	chunked_bits = []
	for i in range(0, len(bits), 3):
		chunked_bits.append(bits[i:i + 3])
	dechunked_bits = []
	for bit in chunked_bits:
		if bit.count('1') >= 2:
			dechunked_bits.append('1')
		else:
			dechunked_bits.append('0')
	dechunked_bits = ''.join(dechunked_bits)
	converted_ascii = []
	for j in range(0, len(dechunked_bits), 8):
		converted_ascii.append(chr(int(dechunked_bits[j:j + 8], 2)))
	converted_ascii = ''.join(converted_ascii)
	print(converted_ascii)
	return converted_ascii