-
Notifications
You must be signed in to change notification settings - Fork 0
/
manchester.py
52 lines (40 loc) · 1.29 KB
/
manchester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Standard library imports
import typing
class Manchester:
"""
Manchester encoding using G. E. Thomas convention method
Manchester II (BiPhase-L)
"""
def __init__(self) -> typing.NoReturn:
pass
def encode(self, data: str) -> typing.List[str]:
"""
Encoding data to Manchester encoded data
0 -> Low To High: "01"
1 -> High To Low: "10"
S -> Space (no voltage): "00"
e.g. Input: 01011 10
-> ["01", "10", "01", "10", "10", "00", "10", "01"]
Parameters:
data (str): data to be encoded
Returns:
typing.List[str]: encoded data in list
"""
# Set output variable
output: typing.List[str] = list()
# Encode each digit of data
for digit in data:
output.append(
"01" if digit == "0" else "10"
if digit == "1" else "00"
if digit == " " else "x"
)
# Validate output and return error on invalid input
try:
assert len(output) == len(data) and not "x" in output
except AssertionError:
raise SystemExit("Invalid input provided")
# Return data
return output