I'm writing a function that is supposed to decode an inputted NRZ scheme. My professor also prohibited any usages of import libraries. Here's my code so far.
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
I don't know why but it works for some test cases but not others. Like this one:
nrz_input = """
______
| |
| |
___| |___
"""
The input above should spit out a 0110 based on the scheme. But my code outputs a 1000. I don't really know why either. Also, I should note that I'm fairly new to coding.
I'm writing a function that is supposed to decode an inputted NRZ scheme. My professor also prohibited any usages of import libraries. Here's my code so far.
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
I don't know why but it works for some test cases but not others. Like this one:
nrz_input = """
______
| |
| |
___| |___
"""
The input above should spit out a 0110 based on the scheme. But my code outputs a 1000. I don't really know why either. Also, I should note that I'm fairly new to coding.
Share Improve this question edited Mar 4 at 1:40 Tuan Nguyen asked Mar 4 at 1:35 Tuan NguyenTuan Nguyen 12 bronze badges1 Answer
Reset to default 0You have two major problems.
First, you do input_scheme.strip()
. That gets rid of the leading spaces in your first line. If you had done print(first_line)
and print(last_line)
, you would have seen that.
Once you do that, you have a problem because there's a blank first line. You can resolve that by deleting the first (empty) line.
(Note that you don't need to convert each line to a list. Since you aren't changing them, a string operates exactly like a list in this case.)
Finally, you have the levels swapped. If your match is in the first row, then the level is 1, not 0.
So, if I make these two changes:
nrz_scheme = input_scheme.splitlines()[1:]
and
# Translate the signal level to 0 or 1
if current_row is first_row:
nrz_output += "1"
else:
nrz_output += "0"
then I get "0110" as an output.
Also, note that you don't need to set the current row BOTH based on the top/bottom AND based on the transition character. You can delete the "# Switch the current_row" block.
So, here's the corrected code:
def nrz_encoding(input_scheme):
nrz_scheme = input_scheme.splitlines()[1:]
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 += "1"
else:
nrz_output += "0"
index += 3
# Check for a transition (vertical bar)
if index < len(nrz_scheme[1]) and nrz_scheme[1][index] == "|":
# Move past the transition character
index += 1
return nrz_output
nrz_input = """
______
| |
| |
___| |___
"""
print(nrz_encoding(nrz_input))
And the output:
timr@Tims-NUC:~/src$ python x.py
0110
timr@Tims-NUC:~/src$
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745064921a4609197.html
评论列表(0条)