Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python question! please show code and steps! Bonus Question 1 (Gray Code) [3 poi

ID: 3668870 • Letter: P

Question

python question!

please show code and steps!

Bonus Question 1 (Gray Code) [3 points]: Consider following Python function for Gray code by bit width enumerating all binary numbers With a given Width. Hor example, it 0, 1 are the binary numbers with one digit, those with two digits are formed by prepending 0 to those 2-bit 4-bit numbers, 00, 01, and the prepending 1 to those numbers 10, 11, resulting in 00, 01, 10, 00 0000 11. Those with three digits are formed by prepending 0 again, 000, 001, 010, 011, and 01 0001 then prepending 1 as well, 100, 101, 110, 111 11 0011 def binary Numbers (width) 10 0010 return ''1 if width else 0110 '0' +x for X in binaryNumbers (width-1) 0111 3-bit I' 1 +x for x in binaryNumbers (width-1) The Gray code is a binary code that has the property that any two consecutive numbers 000 0101 0100 001 differ only by one bit. The Gray code has several applications, for example in digital TV 1100 011 transmissions. It is constructed similarly to binary numbers, but with reversal when 1101 prepending 1: for example, given 0, 1, the Gray code is formed by prepending 0 to those 010 110 numbers, 00, 01, and then prepending 1 after reversing those numbers, 11, 10, resulting 1110 in 00, 01, 11, 10. The Gray code with three digits is formed by prepending 0 again, 000 1010 101 001, 011, 010, and the prepending 1 after reversing the numbers, 110, 111, 101, 100 1011 Write a Python function graycode (width) that returns a list with the Gray codes of the 100 1001 given width 1000 gray Code (3) '110', '111', '101', '100 1 000 001 011 010 Place the function in the file B1 yourmacid.py

Explanation / Answer

def greycode(width):
if width == 0:
return ['']
else:
L1 = greycode(width - 1)
L2 = list(reversed(L1))
return ['0' + x for x in L1] + ['1' + x for x in L2]
  

Concept: Recursion

Code Explanation: If the length of grey code sequence required is zero (termination condtion) then return empty sequence.

Else by defination of grey code we first compute grey code for (width - 1) length sequence and for each of its element we prepend (add to front) '0' to generate first half of sequence of "width".

To generate second half of sequence we reverse sequence of (width - 1) and for each element prepend '1' to it.

Then combine two list for final result.