| Download
Worksheets related to Applied Discrete Structures
Project: Applied Discrete Structures
Views: 15774Image: ubuntu2004
Kernel: SageMath 9.4
Fun with the Gray Code
This function returns the th bit gray code string, .
In [5]:
In [6]:
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
In [92]:
error
[]
In [53]:
[[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 1],
[0, 0, 1, 0],
[0, 1, 1, 0],
[0, 1, 1, 1],
[0, 1, 0, 1],
[0, 1, 0, 0],
[1, 1, 0, 0],
[1, 1, 0, 1],
[1, 1, 1, 1],
[1, 1, 1, 0],
[1, 0, 1, 0],
[1, 0, 1, 1],
[1, 0, 0, 1],
[1, 0, 0, 0]]
Given an bit sequence, this function returns its position in the n bit Gray code, expressed as a sequence of binary digits.
In [10]:
In [8]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-0ebb3f9d5225> in <module>
----> 1 for l in map(lambda s:gray_decoder(s),[gray(Integer(3),k) for k in range(Integer(8))]): print(l)
<ipython-input-8-0ebb3f9d5225> in <lambda>(s)
----> 1 for l in map(lambda s:gray_decoder(s),[gray(Integer(3),k) for k in range(Integer(8))]): print(l)
NameError: name 'gray_decoder' is not defined
In [54]:
SageMath doesn't seem to have a function that converts a list of digits to a number, so here is the definiton of one that coverts a list using a specified base.
In [63]:
In [67]:
7
This verifies that the [removed]gray_decoder does invert [removed]gray.
In [72]:
[0, 1, 2, 3, 4, 5, 6, 7]
In [73]:
In [74]:
[[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1]]