Ich schreibe eine Funktion, die ein eingegebenes NRZ -Schema dekodieren soll. Mein Professor untersagte auch alle Verwendung von Importbibliotheken. Hier ist mein Code bisher. < /P>
def nrz_encoding(input_scheme):
# Split the input into lines and convert each line into a list of characters
nrz_scheme = [list(line) for line in input_scheme.strip().split('\n')]
index = 0
nrz_output = ''
# Identify the first and last rows
first_row = nrz_scheme[0]
last_row = nrz_scheme[3]
# Determine the starting row (first or last) based on the leftmost underscore
if first_row[0] == "_":
current_row = first_row
elif last_row[0] == "_":
current_row = last_row
else:
raise ValueError("Please make sure that there's no spaces at the start of your leftmost underscore(_).")
# Determine the maximum length of the rows
max_length = max(len(first_row), len(last_row))
while index < max_length:
# Determine the current row based on the signal level
if index < len(first_row) and first_row[index] == "_":
current_row = first_row
elif index < len(last_row) and last_row[index] == "_":
current_row = last_row
# Translate the signal level to 0 or 1
if current_row is first_row:
nrz_output += "0"
else:
nrz_output += "1"
index += 3
# Check for a transition (vertical bar)
if index < len(nrz_scheme[1]) and nrz_scheme[1][index] == "|":
# Switch the current row
if current_row is first_row:
current_row = last_row
else:
current_row = first_row
# Move past the transition character
index += 1
return nrz_output
< /code>
Ich weiß nicht warum, aber es funktioniert für einige Testfälle, aber nicht für andere. So wie dieses: < /p>
nrz_input = """
______
| |
| |
___| |___
"""
< /code>
Die obige Eingabe sollte einen auf dem Schema basierenden 0110 ausspucken. Aber mein Code gibt 1000 aus. Ich weiß auch nicht wirklich warum. Ich sollte auch beachten, dass ich ziemlich neu im Codieren bin.
NRZ Scheme Decode ⇐ Python
-
- Similar Topics
- Replies
- Views
- Last post