Skip to content

Instantly share code, notes, and snippets.

@Vivek-abstract
Created October 6, 2018 04:55
Show Gist options
  • Save Vivek-abstract/f10e697040fee67175856cad8b133bc4 to your computer and use it in GitHub Desktop.
Save Vivek-abstract/f10e697040fee67175856cad8b133bc4 to your computer and use it in GitHub Desktop.
print("Enter number of elements: ")
n = int(input())
elements, binary_elements = [], []
for i in range(n):
# Take input and store in array
x = int(input())
elements.append(x)
# Convert to binary array of 8 bits
binary_elements.append('{0:08b}'.format(x))
sum = '00000000'
print("Number\tGray Level\tSum\t\tIGS Code")
for i in range(n):
if binary_elements[i][:4] == '1111':
# Add 0000 => don't add anything
sum = binary_elements[i]
else:
# Add 4 bits of LSB of sum to input
sum = '{0:08b}'.format(int(sum[4:],2) + elements[i])
print("%d\t%s\t%s\t%s" % (elements[i], binary_elements[i], sum, sum[:4]))
@Vivek-abstract
Copy link
Author

Output:

Output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment