Question: Write an application in Python or Java that will take a list of integers and validate that the list is a valid grey code. Please
Write an application in Python or Java that will take a list of integers and validate that the list is a valid grey code. Please upload the code, and a console output from a sample run in which you supplied at least 8 (eight) integers.
****Remember that a gray code is simply a sequence of integers that only change one bit at a time. To tell if a given sequence meets that criteria, you would look at the bit representation of the first two in the list, see if they differ by just one bit. If so, move on to the next pair and so on. Finally, you check the first and the last members of the list to see if they only differ by one bit. If the answer is "yes" in all cases, it's a gray code.
*** Example in Python : I wrote a little function in Python called gray that takes a list of integers. If you choose Java or C++, then an array or ArrayList
>>> gray ([1, 2]) # 0001 and 0010 differ in two bit positions, so it's not a gray code False >>> gray ([1, 3]) # 0001 is just one bit away from 0011, so we're good. True >>> gray ([15, 7, 3, 1, 9, 11]) # 1111, 0111, 0011, 0001, 1001, 1101 is the sequence in binary. Note that 1111 is just one bit away from 1101. True >>> gray ([0, 1, 3]) # 0000 is just one bit away from 0001, and 0001 is just one bit from 0011, but 0011 is two bits away from 0000. False >>> gray ([0, 1, 3, 2]) True >>>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
