Created
September 4, 2019 17:41
-
-
Save nz-angel/ca91c51b2ba4b8ac3ddc5c612f092422 to your computer and use it in GitHub Desktop.
Handy functions to get the binary and/or Gray's code representation of an integer or of each integer in an iterable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def int_to_bin(n, length=0): | |
""" | |
Converts an integer to a string of its representation in binary code. | |
:param n: integer to convert | |
:type n: int | |
:param length: desired length of string if the binary representation is shorter than that length. | |
:type length: int | |
:return: binary representation of the integer | |
:rtype: str | |
Examples: | |
# Binary representation without leading zeroes | |
int_2_bin(6) | |
> '110' | |
# Binary representation adding leading zeroes to match desired length | |
int_2_bin(6, 8) | |
> '00000110' | |
# Given that the desired length is shorter than the length of the actual representation, the latter is returned | |
int_2_bin(6,1) | |
> '110' | |
""" | |
return '{0:0{length}b}'.format(n, length=length) | |
def int_to_gray(n, length=0): | |
""" | |
Converts an integer to a string of its representation in Gray code. | |
:param n: integer to convert | |
:type n: int | |
:param length: desired length of string if the Gray's representation is shorter than that length. | |
:type length: int | |
:return: Gray code representation of the integer | |
:rtype: str | |
Examples: | |
# Gray code representation without leading zeroes | |
int_2_gray(6) | |
> '101' | |
# Gray code representation adding leading zeroes to match desired length | |
int_2_gray(6, 8) | |
> '00000101' | |
# Given that the desired length is shorter than the length of the actual representation, the latter is returned | |
int_2_gray(6, 1) | |
> '101' | |
""" | |
g = bin(n ^ (n >> 1))[2:] | |
if len(g) < length: | |
g = '0'*(length-len(g)) + g | |
return g | |
def iterable_to_gray_dict(iterable): | |
""" | |
Returns a dictionary mapping each element of the iterable of integers to their Gray's code representation. | |
Leading zeroes are added so all strings are the same length. | |
:param iterable: iterable containing int | |
:type iterable: | |
:return: dictionary where the key is the int and the value is its representation in Gray code. | |
:rtype: dict | |
Example: | |
iterable_to_gray(range(4)) | |
> {0: '00', 1: '01', 2: '11', 3: '10'} | |
""" | |
largest = max(iterable) | |
large_len = len(int_to_bin(largest)) | |
gray_vals = {} | |
for i in iterable: | |
gray_vals[i] = int_to_gray(i, large_len) | |
return gray_vals | |
def iterable_to_bin_dict(iterable): | |
""" | |
Returns a dictionary mapping each element of the iterable of integers to their binary code representation. | |
Leading zeroes are added so all strings are the same length. | |
:param iterable: iterable containing int | |
:type iterable: | |
:return: dictionary where the key is the int and the value is its representation in binary code. | |
:rtype: dict | |
Example: | |
iterable_to_bin(range(4)) | |
> {0: '00', 1: '01', 2: '10', 3: '11'} | |
""" | |
largest = max(iterable) | |
large_len = len(int_to_bin(largest)) | |
bin_vals = {} | |
for i in iterable: | |
bin_vals[i] = int_to_bin(i, large_len) | |
return bin_vals | |
def iterable_to_gray_str(iterable): | |
gray_dict = iterable_to_gray_dict(iterable) | |
s = '' | |
for i in iterable: | |
s += gray_dict[i] | |
return s | |
def iterable_to_bin_str(iterable): | |
bin_dict = iterable_to_bin_dict(iterable) | |
s = '' | |
for i in iterable: | |
s += bin_dict[i] | |
return s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment