We need you to implement a method of receiving commands over a network, processing the information and responding.

Our device will send a single packet to you containing data and an instruction which you must perform before returning your reply.

To keep things simple, we will be passing a single "packet" as a string. 
Each "byte" contained in the packet is represented by 4 chars.



One packet is structured as below:
```
Header  Instruction   Data1    Data2   Footer
------   ------       ------   ------  ------
 H1H1     0F12         0012     0008    F4F4
------   ------       ------   ------  ------

The string received in this case would be - "H1H10F1200120008F4F4"

Instruction: The calculation you should perform, always one of the below.
            0F12 = Addition
            B7A2 = Subtraction
            C3D9 = Multiplication
            FFFF = This instruction code should be used to identify your return value.
``` 
- The Header and Footer are unique identifiers which you must use to form your reply.

- Data1 and Data2 are the decimal representation of the data you should apply your instruction to. _i.e 0109 = 109._

- Your response must include the received header/footer, a "FFFF" instruction code, and the result of your calculation stored in Data1.

- Data2 should be zero'd out to "0000".


```
To give a complete example:

If you receive message "H1H10F1200120008F4F4".
The correct response would be "H1H1FFFF00200000F4F4"

```
In the event that your calculation produces a negative result, the value returned should be "0000", similarily if the value is above 9999 you should return "9999".


Goodluck, I look forward to reading your creative solutions!

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code: def communication_module(packet):
	

Solutions:
INSTRUCTIONS = {'0F12': int.__add__, 'B7A2': int.__sub__, 'C3D9': int.__mul__}

def communication_module(packet):
	(header, inst, d1, d2, footer) = (packet[i:i + 4] for i in range(0, 20, 4))
	res = max(0, min(9999, INSTRUCTIONS[inst](int(d1), int(d2))))
	return f'{header}FFFF{res:0>4}0000{footer}'


import re
from operator import add, sub, mul
instructions = {'0F12': add, 'B7A2': sub, 'C3D9': mul}

def communication_module(packet):
	(header, instruction, data1, data2, footer) = re.findall('....', packet)
	result = instructions[instruction](int(data1), int(data2))
	return '{}FFFF{:04}0000{}'.format(header, min(max(result, 0), 9999), footer)


def communication_module(p):
	return '%sFFFF%04i0000%s' % (p[:4], max(min(eval(p[8:12].lstrip('0') + {'0F12': '+', 'B7A2': '-', 'C3D9': '*'}[p[4:8]] + p[12:16].lstrip('0')), 9999), 0), p[-4:])


def communication_module(packet):
	(header, footer) = (packet[:4], packet[-4:])
	(instr, rvalue) = (packet[4:8], 'FFFF')
	(data1, data2) = (packet[8:12], packet[12:16])
	if instr == '0F12':
		calc = int(data1) + int(data2)
	elif instr == 'B7A2':
		calc = int(data1) - int(data2)
	elif instr == 'C3D9':
		calc = int(data1) * int(data2)
	if calc > 9999:
		calc = 9999
	elif calc < 0:
		calc = 0
	rstring = f'{header}{rvalue}{calc:04d}0000{footer}'
	return rstring


def communication_module(packet):
	part = [packet[i:i + 4] for i in range(0, len(packet), 4)]
	d = {'0F12': '+', 'B7A2': '-', 'C3D9': '*'}
	res = eval(f'{int(part[2])}{d[part[1]]}{int(part[3])}')
	res = max(0, min(res, 9999))
	return part[0] + f'FFFF{str(res).zfill(4)}0000' + part[4]


from operator import add, sub, mul

def communication_module(packet):
	part = [packet[i:i + 4] for i in range(0, len(packet), 4)]
	res = {'0F12': add, 'B7A2': sub, 'C3D9': mul}[part[1]](int(part[2]), int(part[3]))
	res = max(0, min(res, 9999))
	return part[0] + f'FFFF{str(res).zfill(4)}0000' + part[4]


import operator
funcs = {'0F12': operator.add, 'B7A2': operator.sub, 'C3D9': operator.mul}

def communication_module(packet):
	(instruction, one, two) = (packet[4:8], int(packet[8:12]), int(packet[12:16]))
	result = funcs[instruction](one, two)
	if result > 9999:
		result = 9999
	elif result < 0:
		result = 0
	return '{}FFFF{:0>4}0000{}'.format(packet[:4], result, packet[16:])


def communication_module(packet):
	(header, instruction, data1, data2, footer) = (packet[:4], packet[4:8], int(packet[8:12]), int(packet[12:16]), packet[-4:])
	operations = {'0F12': data1 + data2, 'B7A2': data1 - data2, 'C3D9': data1 * data2}
	calc_str = str(min(9999, max(0, operations[instruction]))).zfill(4)
	(instruction, data2R) = ('F' * 4, '0' * 4)
	return header + instruction + calc_str + data2R + footer


def communication_module(packet):
	calc = 0
	if packet[4:8] == '0F12':
		calc = int(packet[8:12]) + int(packet[12:16])
	elif packet[4:8] == 'B7A2':
		calc = int(packet[8:12]) - int(packet[12:16])
	elif packet[4:8] == 'C3D9':
		calc = int(packet[8:12]) * int(packet[12:16])
	if calc < 0:
		calc = '0000'
	elif calc > 9999:
		calc = '9999'
	return packet[0:4] + 'FFFF' + str(calc).zfill(4) + '0000' + packet[16:20]


def communication_module(s):
	result = str(max(0, min(int(s[8:12]) + int(s[12:16]) if s[4] == '0' else int(s[8:12]) - int(s[12:16]) if s[4] == 'B' else int(s[8:12]) * int(s[12:16]), 9999)))
	return s[:4] + 'FFFF' + '0' * (4 - len(result)) + result + '0000' + s[-4:]